Insert Error: Column name or number of supplied values does not match

dougancil

Well-known member
Joined
Jun 29, 2010
Messages
50
Programming Experience
Beginner
I have the following code:

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
 
Just out of curiosity, you say there are 4 fields, but in your insert statement you are only inserting 3 that I can see. If you are trying to insert less than the number of fields you need to specify the column names in your insert statement.

Example:
INSERT INTO exceptions (Column1, Column2, Column3) VALUES ('" & cell1 & "','" & cell2 & "','" & cell3 & "')".
 
Programgod,

Thats what I need help with, I need to determine is field 4 is true or not and then insert the value of "Holiday" into that field if it's checked.
 
I think I see your problem. I put your code into a new project and created a datagridview with 4 columns. The thing with datagrid columns is that they start with 0, not with 1. So you might be hitting an "Index out of range" error when you run your code. I have modified your code slightly below, I believe this is what you may be looking for.

VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] holidaysaveButton_Click([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Button1.Click[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] For[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] dr [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DataGridViewRow [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DataGridView1.Rows[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cell1 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cell2 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cell3 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] flag [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] dr.IsNewRow [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Exit [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE]
[SIZE=2]   flag = Convert.ToBoolean(dr.Cells([B][U][COLOR=red]3[/COLOR][/U][/B]).Value)[/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (flag = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]     dr.Cells(2).Value = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Holiday"[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#a31515] 
[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]     Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] str1 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = dr.Cells([B][U][COLOR=red]0[/COLOR][/U][/B]).Value.ToString[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]     Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] str2 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = dr.Cells([B][U][COLOR=red]1[/COLOR][/U][/B]).Value.ToString[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]     Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] str3 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = dr.Cells([B][U][COLOR=red]2[/COLOR][/U][/B]).Value.ToString[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]     'TODO: Warning!!! break;If[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   Else[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]     MessageBox.Show([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Select at least one checkbox"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]     'TODO: Warning!!! break;Else[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] dr.Cells(3).Value [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]IsNot[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]     Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] str [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = dr.Cells([B][U][COLOR=red]0[/COLOR][/U][/B]).Value.ToString()[/SIZE]
 
[SIZE=2]     cell1 = dr.Cells([COLOR=red][B][U]0[/U][/B][/COLOR]).Value.ToString()[/SIZE]
[SIZE=2]     cell2 = dr.Cells([B][U][COLOR=red]1[/COLOR][/U][/B]).Value.ToString()[/SIZE]
[SIZE=2]     cell3 = dr.Cells([B][U]2[/U][/B]).Value.ToString()[/SIZE]
 
[SIZE=2]     MessageBox.Show(str)[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] Next[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE]


If you notice I added a break in there "If dr.IsNewRow Then Exit For" because it was trying to evaluate a new row which would be empty and cause an error.
 
Programgod,

Ok I've altered the code for the page a bit:

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

            If dr.IsNewRow Then Exit For

            flag = Convert.ToBoolean(dr.Cells(3).Value)
            If (flag = True) Then
                dr.Cells(2).Value = "Holiday"
                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 the reason that I don't start at field 0 is because I am not inserting that field into my database, so I start with field 1. Secondly, I now get this error:

"DataErroroccured:\System.formatException\Holiday is not a valid value for DateTime.\DataErrorContext: Commit" and I'm pretty sure it's because of the values that I'm trying to pass correct? I think it's because for the datetime field in the form that it's passing both the date and the time when I just need the date to be passed. How can I do that?

Thank you,

Doug
 
ok so I figured out the date issue, so now I am only passing the date to the database, but I still get the error:

"DataErroroccured:\System.formatException\Holi day is not a valid value for DateTime.\DataErrorContext: Commit"

I changed this line of code:
Dim cell2 As String

to
Dim cell2 As Date
 
Sorry I haven't been able to respond sooner. It would seem that you are trying to pass the word "Holiday" into a field in the database that has a datatype of DateTime. So you would either need to change the datatype in the database to a char/varchar/nvarchar field. Otherwise, you would need to create another field to store that value. Changing the datatype in VB will also give you an error because you are trying to set "cell2 = dr.Cells(2).Value.ToString()" and dr.Cells(2) is equal to the word "Holiday".
 
Programgod,

So I've modified the code and I can see that it's saving all of the data I need but not saving the value of Holiday. Here is the code as it currently stands:

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 Date
            Dim cell3 As String
            Dim cell4 As String

            If dr.IsNewRow Then Exit For

            If dr.Cells(4).Value = "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
                cell4 = "Holiday"
            End If
            If dr.Cells(3).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 (employeenumber, exceptiondate, minutes) 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
            End If
            Me.Close()
            DailySummaryReview.holidayaddButton.Enabled = False
        Next
    End Sub
End Class
 
Its not saving the value because you are not telling it to. You would need to create a new column in your database that is a varchar datatype and then include it in your INSERT statement. Right now you are only saving the employee # and date information. You are setting a variable to the value "Holiday" but you never write that variable to the database.
 
Programgod,

So I have a column called "code" that is a varchar, so then to insert the value of "Holiday" do I need to add the cell4 value to my insert statemtent?
 
Yeppers :) That should take care of your issue

Also, the only statement you need in your IF statement that checks to see if the datagrid cell is checked is the setting of the cell4 variable to the word holiday. You don't ever use the str# variables in this routine so they are kinda useless.
 
So this is the code that I have now:

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 Date
            Dim cell3 As String
            Dim cell4 As String

            If dr.IsNewRow Then Exit For

            If dr.Cells(4).Value = "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
                Dim str4 As String = dr.Cells(4).Value.ToString
                cell4 = "Holiday"
            End If
            If dr.Cells(4).Value Is 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
                MessageBox.Show("Please select a checkbox")
                Try
                    con.ConnectionString = "Data Source=xxxxx;Initial Catalog=xx xxx;User ID=xxxxx;Password=xxxxx"
                    con.Open()
                    cmd.Connection = con
                    cmd.CommandText = "INSERT INTO exceptions (employeenumber, exceptiondate, minutes, code) VALUES ('" & cell1 & "','" & cell2 & "','" & cell3 & "','" & cell4 & "')"
                    cmd.ExecuteNonQuery()
                Catch ex As Exception
                    MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
                Finally
                    con.Close()
                End Try
            End If
            DailySummaryReview.holidayaddButton.Enabled = False
        Next
    End Sub
End Class

and intellisense is warning me that cell4 is used before it is assigned a value. But I thought that by adding the line
VB.NET:
Dim str4 As String = dr.Cells(4).Value.ToString
that I am assigning it a value. Evidently not though. Where do I assign it a value? Also are you saying to remove these lines of my if statement?
VB.NET:
  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
 
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 Date
        Dim cell3 As String
        Dim cell4 As String [COLOR=blue]= ""[/COLOR]   [COLOR=red][B]<-- You can set it here[/B][/COLOR]
 
        If dr.IsNewRow Then Exit For
        If dr.Cells(4).Value = "True" Then cell4 = "Holiday" [COLOR=blue]Else cell4 = ""[/COLOR]   [COLOR=red][B]<-- Or you can set it here, or both.[/B][/COLOR]
 
        If dr.Cells(4).Value Is Nothing Then
            Dim str As String = dr.Cells(1).Value.ToString()  [COLOR=red][B]<-- This line is not necessary either, it is never used.[/B][/COLOR]
 
            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
 
            MessageBox.Show("Please select a checkbox")
 
            Try
                con.ConnectionString = "Data Source=xxxxx;Initial Catalog=xx xxx;User ID=xxxxx;Password=xxxxx"
                con.Open()
                cmd.Connection = con
                cmd.CommandText = "INSERT INTO exceptions (employeenumber, exceptiondate, minutes, code) VALUES ('" & cell1 & "','" & cell2 & "','" & cell3 & "','" & cell4 & "')"
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
            Finally
                con.Close()
            End Try
        End If
 
        DailySummaryReview.holidayaddButton.Enabled = False
    Next
End Sub

You have to set a default value for a variable if you plan on using it later, so since you were only setting cell4 to holiday if the field was checked, it was never being set to any value.

Also, there is something that I just noticed about your code that is...well, confusing I guess would be the word. You have the statement
VB.NET:
        If dr.Cells(4).Value Is Nothing Then
since the fourth cell is a checkbox, it will never (or at least should never) be equall to nothing. Without recreating your program in my own environment, I am confused as to 1) how this code even fires and 2) what you are trying to accomplish. Basically, as I read it it sounds like you only want to insert into the database if the checkbox is not checked?
 
Programgod,

Thank you for pointing that line out .. I have changed the line of

VB.NET:
        If dr.Cells(4).Value Is Nothing Then

to

VB.NET:
       If dr.Cells(4).Value IsNot Nothing Then

and this code should insert values into the database if the checkbox is checked.
 
Ok so I have been working with this more and discovered that only the first line is getting added ... even if I click other check boxes. What's causing that?

Do I need to remove this line?

VB.NET:
   If dr.IsNewRow Then Exit For
 
Back
Top