Hi Guys,

im facing problem at cmd.ExecuteNonQuery()
kindly correct me wer is the mistake? n how to correct?

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim sqlinsert As String
        ' We use the INSERT statement which tells our program to add the information
        ' from the Forms Text fields into the Databases columns.

        sqlinsert = "INSERT INTO Contacts(FirstName, LastName, Address, CompanyName, CompanyAddress, PersonalAC, CompanyAC, PhoneNumber, Remarks, IDNum)" & _
      "VALUES(@FirstName, @LastName, @Address, @CompanyName, @CompanyAddress, @PersonalAC, @CompanyAC, @PhoneNumber, @Remarks, @IDNum)"
        Dim cmd As New OleDbCommand(sqlinsert, con1)
        ' This assigns the values for our columns in the DataBase.
        ' To ensure the correct values are written to the correct column
        cmd.Parameters.Add(New OleDbParameter("@FirstName", txtFirst.Text))
        cmd.Parameters.Add(New OleDbParameter("@LastName", txtLast.Text))
        cmd.Parameters.Add(New OleDbParameter("@Address", txtAddress.Text))
        cmd.Parameters.Add(New OleDbParameter("@CompanyName", txtCompanyN.Text))
        cmd.Parameters.Add(New OleDbParameter("@CompanyAddress", txtCompanyA.Text))
        cmd.Parameters.Add(New OleDbParameter("@PersonalAC", txtPersonalAC.Text))
        cmd.Parameters.Add(New OleDbParameter("@CompanyAC", txtCompanyAC.Text))
        cmd.Parameters.Add(New OleDbParameter("@PhoneNumber", txtPhone.Text))
        cmd.Parameters.Add(New OleDbParameter("@Remarks", txtRemarks.Text))
        cmd.Parameters.Add(New OleDbParameter("@IDNum", txtID.Text))
        ' This is what actually writes our changes to the DataBase.
        ' You have to open the connection, execute the commands and
        ' then close connection.
        con1.Open()
        cmd.ExecuteNonQuery() ' ERROR AT HERE
        con1.Close()
        ' This are subs in Module1, to clear all the TextBoxes on the form
        ' and refresh the DataGridView on the MainForm to show our new records.
        ClearTextBox(Me)
        RefreshDGV()
        Me.Close()
    End Sub

try

cmd.parameters.addwithvalue("@param",value)

may be this will help you , but it is better to tell us what error you got at cmd.executenonquery()

Regards

I am getting the following error at cmd.ExecuteNonQuery()

SqlException was unhandled by user

Dear rponraj please open a new thread for your problem. do not post in some one else post.

Use a Try..Catch Block and capture the error.

Try
    ' the procedure
Catch Ex as Exception
    MsgBox (ex.toString)
End Try

You can also try to print out the parameters.

For Each p As OleDb.OleDbParameter In cmd.Parameters
    Console.WriteLine("Name:{0} Value{1}", p.ParameterName, p.Value.ToString)
Next

Be sure that if a parameter is blank, empty, or is nothing, it must be set to DBNull.

cmd.Parameters.Add(New OleDbParameter("@FirstName", IIf(txtFirst.Text = "", DBNull.Value, txtFirst.Text)))
This article has been dead for over six months. Start a new discussion instead.