Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Con.Open()
Dim Str As String = "INSERT INTO db (ID,companyname,companyaddress,companycontact,contactperson,contactnumber,UserLogin,mDate) SELECT @ID,@companyname,@companyaddress,@companycontact,@contactperson,@contactnumber,@UserLogin,@mDate FROM db WHERE Userlogin = @Userlogin"
Dim Cmd As New OleDbCommand
With Cmd
.CommandText = Str
.Parameters.AddWithValue("@ID", f1.Text)
.Parameters.AddWithValue("@companyname", f2.Text)
.Parameters.AddWithValue("@companyaddress", f3.Text)
.Parameters.AddWithValue("@companycontact", f4.Text)
.Parameters.AddWithValue("@contactperson", f5.Text)
.Parameters.AddWithValue("@contactnumber", f6.Text)
.Parameters.AddWithValue("@UserLogin", f12.Text)
.Parameters.AddWithValue("@mDate", DateTimePicker1.Text)
.Connection = Con
.ExecuteNonQuery()
End With
If Cmd.ExecuteNonQuery() Then
Con.Close()
MessageBox.Show("New Record is Added successfully.", "Record Saved")
Call clear()
Else
MsgBox("Could Not Insert Record!!! ", "Already Entered")
Return
End If
End Sub
3 Answers
Do you get any errors when calling the ExecuteNonQuery() method?
I checked your code and I have two comments:
(1) You are calling the ExecuteNonQuery() twice in your code:
+------------------------+
'First call
.ExecuteNonQuery()
End With
'Second call
If Cmd.ExecuteNonQuery() Then
+------------------------+
Are you calling the method twice on purpose?
2. Make sure that the connection is opened before calling the Cmd.ExecuteNonQuery() method.
If you are still facing problems, I found a related issue here with some valuable information: How can I Insert data into SQL Server using VBNet
I hope this helps!
Comments
I don't unterstand why you are using the SELECT? I think you want to do this:
INSERT INTO db(ID,companyname,companyaddress,companycontact,contactperson,contactnumber,UserLogin,mDate) VALUES (@ID,@companyname,@companyaddress,@companycontact,@contactperson,@contactnumber,@UserLogin,@mDate)
.Parameters.Add("@param", SqlDbType).Value =and explicitly set eachSqlDataType.