I am trying to save my textboxes in SQL server.
But i'm getting some errors.
Here is my code:
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim strconnection As String = "Data Source=EASMAR-PC;Initial Catalog=DatabaseConnection;Integrated Security=True;"
Dim _cn As SqlConnection = New SqlConnection(strconnection)
Dim cmd As New SqlCommand
_cn.Open()
cmd.CommandText = "INSERT INTO tblCustomer([ID],[Name], [Email], [Address], [DeptID]) VALUES (@ID, @Name, @Email, @Address, @DeptID);"
cmd.Connection = _cn
cmd.Parameters.AddWithValue("@ID", Me.txtIDD.Text)
cmd.Parameters.AddWithValue("@Name", Me.txtName.Text)
cmd.Parameters.AddWithValue("@Email", Me.txtEmail.Text)
cmd.Parameters.AddWithValue("@Address", Me.txtAddress.Text)
cmd.Parameters.AddWithValue("@DeptID", Me.txtIDD.Text)
cmd.ExecuteNonQuery()
Me._DataAdapter.Fill(Me._DataSet)
End Sub
The error is:
invalid column DeptID.
And if i took out this line of code
(cmd.Parameters.AddWithValue("@DeptID", Me.txtIDD.Text))
another error comes up:
pimary key violation./cannot insert duplicate...
i have 2 tables:
- tblCustomer: ID-Name-Address-Email
- tblDept: DeptID-Namee
And if i did this:
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim strconnection As String = "Data Source=EASMAR-PC;Initial Catalog=DatabaseConnection;Integrated
Security=True;"
Dim _cn As SqlConnection = New SqlConnection(strconnection)
Dim cmd As New SqlCommand
_cn.Open()
cmd.CommandText = "INSERT INTO tblCustomer([ID],[Name], [Email], [Address],[DeptID]) VALUES (@ID, @Name, @Email,
@Address,@DeptID);"
cmd.Connection = _cn
cmd.Parameters.AddWithValue("@ID", Me.tt.Text)
cmd.Parameters.AddWithValue("@Name", Me.txtName.Text)
cmd.Parameters.AddWithValue("@Email", Me.txtEmail.Text)
cmd.Parameters.AddWithValue("@Address", Me.txtAddress.Text)
cmd.Parameters.AddWithValue("@DeptID", Me.txtIDD.Text)
cmd.ExecuteNonQuery()
Me._DataAdapter.Fill(Me._DataSet)
End Sub
I get this error:
invalid column name ID.
IDcolumn isvarchar?tblCustomerdoes not have a columnDeptIdthat is intblDept. From what I can see there is not key between the two either. The second error is probably because you have a unique key onIDin thetblCustomerand are trying to insert an ID that already exists.