ExecuteReader Error

Can anyone tell me what is wrong with this code :

SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=Orders; User id=sa");
   
SqlCommand comm = new SqlCommand("Select * from cust", con);
con.Open();
SqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
      Response.Write(dr.GetSqlValue(0).ToString());
      Response.Write(dr.GetSqlValue(1).ToString());
}

 

 

Whenever i try to execute this code, it displays the following error:

 

Server Error in '/Orders' Application.

Unknown error.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Unknown error.

Source Error:


Line 44:    
Line 45:             SqlCommand comm = new SqlCommand("Select * from cust", con);
Line 46:    con.Open();
Line 47:    SqlDataReader dr = comm.ExecuteReader();
Line 48:   
 

Source File: c:\inetpub\wwwroot\orders\customerdetailsform.aspx.cs    Line: 46

Stack Trace:


[SqlException: Unknown error.]
   System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction)
   System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean& isInTransaction)
   System.Data.SqlClient.SqlConnection.Open()
   Orders.CustomerDetailsForm.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\orders\customerdetailsform.aspx.cs:46
   System.Web.UI.Control.OnLoad(EventArgs e)
   System.Web.UI.Control.LoadRecursive()
   System.Web.UI.Page.ProcessRequestMain()

 

Can anyone give me a solution for this.

 

Thanks,

Shailaja.



Answer this question

ExecuteReader Error

  • Saresh Naroji

    It sounds like your connection string is the problem.  Try this out:

    • Right-click on your desktop and select New -> Text Document
    • Name the document Test.udl
    • Double-click on Test.udl and walk through setting up the connection
    • Test the connection
    • Click OK
    • Right-click on Test.udl and select Open With... and choose Notepad

    This will show you the connection string that you need to use.

    Note:  You will need to remove the section that contains Provider= since ADO.NET already knows to use SQL Server since you are using the SQL Server data objects.

    PS - Are you using SQL Server



  • carsc

    Do check your connection string. By looking at the stack trace, you can detemine that the problem is with the con.open() line. Do include the "password=" attribute in the connection string or set the integrated security attribute to true. If still not works, change the (local) to the machine name.

    Hope this helps.


  • DHardy

    This code also doesn't work.

    can u pls suggest me someother code.

    Thanks,

    Shailaja.


  • Abbyy

  • Allan Braun

    I'm using SQL server as backend.

    Can u explain the previous solution in detail.
    how to set the connection from a notepad.

    Thanks,
    Shailaja.


  • Ben Waldron

    Hello,

     

    Thanks for your reply.

    But after making the specified changes also, the code doesn't work.

    can u pls suggest some other code.

    I'm new to ASP.net, so finding a bit tough to sort it out.

     

    Thanks,

    Shailaja.


  • ExecuteReader Error