I have the following code:
and when I debug my code, I get the error: Insert Error: Column name or number of supplied values does not match.
I have verified that the structure of the database and the form are the same. The values that I'm trying to pass to the database are:
2 Strings, a date and a decimal. Those values in the database are varchar, varchar, datetime, varchar. So this should work. What's supposed to happen is when a user fires the button_click event, if field 4 values is "true" (if the checkbox is checked) then the other 3 fields are updated in the database, and field 4 should add the value "Holiday" in the next field. This code was given to me and it's obviously not correct. I can see the correct values are being passed, but I also need for field 4 to be "Holiday." Any assistance would be appreciated.
Thanks,
Doug
VB.NET:
Private Sub holidaysaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles holidaysaveButton.Click
For Each dr As DataGridViewRow In DataGridView1.Rows
Dim cell1 As String
Dim cell2 As String
Dim cell3 As String
Dim flag As Boolean
flag = Convert.ToBoolean(dr.Cells(3).Value)
If (flag = True) Then
Dim str1 As String = dr.Cells(1).Value.ToString
Dim str2 As String = dr.Cells(2).Value.ToString
Dim str3 As String = dr.Cells(3).Value.ToString
'TODO: Warning!!! break;If
Else
MessageBox.Show("Select at least one checkbox")
'TODO: Warning!!! break;Else
End If
If dr.Cells(4).Value IsNot Nothing Then
Dim str As String = dr.Cells(1).Value.ToString()
cell1 = dr.Cells(1).Value.ToString()
cell2 = dr.Cells(2).Value.ToString()
cell3 = dr.Cells(3).Value.ToString()
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Data Source=xxxxx;Initial Catalog=xxxxx;User ID=xxxxx;Password=xxxxx"
con.Open()
cmd.Connection = con
cmd.CommandText = "insert into exceptions Values('" & cell1 & "','" & cell2 & "','" & cell3 & "')"
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
Finally
con.Close()
End Try
MessageBox.Show(str)
End If
Next
End Sub
End Class
and when I debug my code, I get the error: Insert Error: Column name or number of supplied values does not match.
I have verified that the structure of the database and the form are the same. The values that I'm trying to pass to the database are:
2 Strings, a date and a decimal. Those values in the database are varchar, varchar, datetime, varchar. So this should work. What's supposed to happen is when a user fires the button_click event, if field 4 values is "true" (if the checkbox is checked) then the other 3 fields are updated in the database, and field 4 should add the value "Holiday" in the next field. This code was given to me and it's obviously not correct. I can see the correct values are being passed, but I also need for field 4 to be "Holiday." Any assistance would be appreciated.
Thanks,
Doug