Question

I'm trying to make SqlParameter and when I run the website error appear in SqlDataReader _dataReader = _command.ExecuteReader();

Here is my code:

private void goLogin()
{
   conn.ConnectionString = _connectionString;
   conn.Open();
   string username = txt_username.Text;
   _sqlCodes = "SELECT ACC_PASS,ACC_ID" +
   "FROM ACC WHERE ACC_USER = @username";
   SqlCommand _command = new SqlCommand(_sqlCodes);
   _command.Parameters.Add(new SqlParameter("@username",username));
   SqlDataReader _dataReader = _command.ExecuteReader();

   //more codes here...
   conn.Close();
}

ERROR:

Server Error in '/' Application.

ExecuteReader: Connection property has not been initialized.

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.InvalidOperationException: ExecuteReader: Connection property has not been initialized.

Source Error:

Line 31:             SqlCommand _command = new SqlCommand(_sqlCodes);
Line 32:             _command.Parameters.Add(new SqlParameter("username",username));
Line 33:             SqlDataReader _dataReader = _command.ExecuteReader();
Line 34:             conn.Close();
Line 35:         }

Source File: C:\Users\arnie.mateo\Desktop\GoWireless Project Leonel\GoWireless\GoWireless\account.aspx.cs Line: 33

Stack Trace:

[InvalidOperationException: ExecuteReader: Connection property has not been initialized.]
System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +5089605
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +87
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
System.Data.SqlClient.SqlCommand.ExecuteReader() +89
GoWireless.WebForm1.goLogin() in C:\Users\arnie.mateo\Desktop\GoWireless Project Leonel\GoWireless\GoWireless\account.aspx.cs:33
GoWireless.WebForm1.btn_login_Click(Object sender, EventArgs e) in C:\Users\arnie.mateo\Desktop\GoWireless Project Leonel\GoWireless\GoWireless\account.aspx.cs:52
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

Was it helpful?

Solution

You have to give connection name to SqlCommand.

Use SqlCommand _command = new SqlCommand(_sqlCodes, conn);

OTHER TIPS

The SqlCommand object doesn't know which connection to use. You can create the command with:

SqlCommand _command = conn.CreateCommand();

or set the connection on it:

_command.Connection = conn;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top