Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have the following code:
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 = "Holiday"

      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
      End If
      If dr.Cells(4).Value IsNot Nothing Then
        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, code) VALUES ('" & cell1 & "','" & cell2 & "','" & cell3 & "','" & cell4 & "')"
          cmd.ExecuteNonQuery()
          MessageBox.Show("Your data has been updated")
        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
      Me.Close()
    Next
  End Sub

and I want users to be able to click on the checkbox in field4 and add all of the data plus the word Holiday to my database when they click on the box in that field, but right now it's not allowing me to click on the checkbox, can someone please assist? Also, I did have this working for that field but it would only allow me to add the first line of data, what in my code would not allow for the additional lines to not add their data as well?
Thank you
Posted
Updated 17-May-11 18:42pm
v3
Comments
hemantwithu 18-May-11 0:49am    
@Walter Thanks for Updating
parmar_punit 18-May-11 1:08am    
I don't have more idea about VB.Net but as i know index should be start with 0, if you have four field and you want access fourth field, then you should use like this....
If dr.Cells(3).Value IsNot Nothing Then
cell1 = dr.Cells(0).Value.ToString()
cell2 = dr.Cells(1).Value.ToString()
cell3 = dr.Cells(2).Value.ToString()
Steven.Pinto2000 18-May-11 1:11am    
Can you show the aspx as well

1 solution

You must check the Enable Editing checkbox in the DataGridView Task list.

See the screen shot here
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900