none
cmd.ExecuteNonQuery() Error RRS feed

  • Question

  • Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim connetionString As String, connection As SqlConnection, cmd As SqlCommand connetionString = "Server=MyServerNameHere;Database=MyDB;User Id=MyUserID;Password=MyPassword;" connection = New SqlConnection(connetionString) connection.Open() cmd = New SqlCommand("insert into MyTable (MyField) values (@MyField)") cmd.Parameters.AddWithValue("@MyField", Me.TextBox1.Text) cmd.ExecuteNonQuery() connection.Close() MsgBox("Detail sucussfully Added") End Sub

    I am getting error in the cmd.ExecuteNonQuery() line of code.  Not sure what I am doing wrong.  Please advice.



    • Edited by SixthS.e.n.s.e Tuesday, February 2, 2016 7:23 AM
    • Moved by Bob Beauchemin Tuesday, February 2, 2016 5:16 PM Moved to client-side SqlClient forum for best response
    Tuesday, February 2, 2016 7:23 AM

Answers

  • You haven't assign the connection to the command object; Change your code in this way by adding "Connection" as second Parameter to SqlCommand instanziation:

        connetionString = "Server=MyServerNameHere;Database=MyDB;User Id=MyUserID;Password=MyPassword;"
        connection = New SqlConnection(connetionString)
        connection.Open()
        cmd = New SqlCommand("insert into MyTable (MyField) values (@MyField)", connection)
        cmd.Parameters.AddWithValue("@MyField", Me.TextBox1.Text)
        cmd.ExecuteNonQuery()
        connection.Close()
        MsgBox("Detail sucussfully Added")


    Olaf Helper

    [ Blog] [ Xing] [ MVP]


    Tuesday, February 2, 2016 7:58 AM

All replies

  • Hello,

    And which error message are you getting?


    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Tuesday, February 2, 2016 7:40 AM
  • Thanks for the quick response.

    This is the error message.

    {"ExecuteNonQuery: Connection property has not been initialized."}


    Tuesday, February 2, 2016 7:44 AM
  • Wrong Code

    cmd = New SqlCommand("insert into MyTable (MyField) values (@MyField)")


    How silly it is...  I left the last argument as empty which cause the code to throw error.

    Solution Code

    cmd = New SqlCommand("insert into Test1 (MyField) values (@MyField)", connection)

    Olaf thanks for your assist and I will comeback soon with few more questions since I am a newbie with VB.Net and SQL server.

    Tuesday, February 2, 2016 7:56 AM
  • You haven't assign the connection to the command object; Change your code in this way by adding "Connection" as second Parameter to SqlCommand instanziation:

        connetionString = "Server=MyServerNameHere;Database=MyDB;User Id=MyUserID;Password=MyPassword;"
        connection = New SqlConnection(connetionString)
        connection.Open()
        cmd = New SqlCommand("insert into MyTable (MyField) values (@MyField)", connection)
        cmd.Parameters.AddWithValue("@MyField", Me.TextBox1.Text)
        cmd.ExecuteNonQuery()
        connection.Close()
        MsgBox("Detail sucussfully Added")


    Olaf Helper

    [ Blog] [ Xing] [ MVP]


    Tuesday, February 2, 2016 7:58 AM