ASP C# ExecuteScalar: Connection property has not been initialized.

0

Witam mam problem z dodawaniem danych do bazy a mianowicie po wciśnięciu przycisku dodaj wywala mi taki błąd :

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

Source Error:

Line 34: conn.Open();
Line 35: String id = null;
Line 36: id = (objSqlCommand.ExecuteScalar()).ToString();
Line 37:
Line 38: objSqlCommand = new SqlCommand();

Source File: d:\WebSite1\wykladowca\Accept.aspx.cs Line: 36

Stack Trace:

[InvalidOperationException: ExecuteScalar: Connection property has not been initialized.]
System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +5034133
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.ExecuteScalar() +139
wykladowca_Accept.Button1_Click(Object sender, EventArgs e) in d:\WebSite1\wykladowca\Accept.aspx.cs:36
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

Obsługa przycisku wygląda następująco :

 protected void Button1_Click(object sender, EventArgs e)
    {   
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True";
        SqlCommand objSqlCommand = new SqlCommand();

        objSqlCommand.Connection = conn;
        objSqlCommand = new SqlCommand();
        objSqlCommand.CommandType = CommandType.Text;
        objSqlCommand.CommandText = "SELECT UserId FROM aspnet_Users WHERE UserName = @UserName;";
        objSqlCommand.Parameters.AddWithValue("@UserName", "" + Session["User"]);

        conn.Open();
        String id = null;
        id = (objSqlCommand.ExecuteScalar()).ToString();
        
        objSqlCommand = new SqlCommand();
        objSqlCommand.CommandType = CommandType.Text;
        objSqlCommand.CommandText = "INSERT INTO Przedmiot (UserId, NazwaP, Typ, DataAktualizacji) values (@id, @nazwap, @rodzajp, @data);";
        objSqlCommand.Parameters.AddWithValue("@nazwap", "" + Session["Nazwa"]);
        objSqlCommand.Parameters.AddWithValue("@rodzajp", "" + Session["Rodzaj"]);
        objSqlCommand.Parameters.AddWithValue("@data", DateTime.Now.ToString());
        objSqlCommand.Parameters.AddWithValue("@id", id);
        objSqlCommand.ExecuteNonQuery();
        conn.Close();

    } 
0
        SqlCommand objSqlCommand = new SqlCommand();
 
        objSqlCommand.Connection = conn;
        objSqlCommand = new SqlCommand();
 

tworzysz command, przypisujesz mu connection i tworzysz nowy command.

1 użytkowników online, w tym zalogowanych: 0, gości: 1