Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all, I am using a DataGridView First column Checkbox, Regno, Name Like . When Checkbox is True means i Want To Add New Record. if have means i need to UPDATE

Here I use Code this code is working New Recored it not going to UPDATE..

Please Tell your Valuable Advice .
VB
Dim strda1 As SqlDataReader
        For Each row As DataGridViewRow In DGV1_SA.Rows
            Dim cell As DataGridViewCheckBoxCell = row.Cells(0)

            For ix As Integer = 0 To DGV1_SA.SelectedRows.Count - 1
                Dim strSQL12 As String = "select * from Attendance WHERE Regno='" & DGV1_SA.Rows(ix).Cells(1).Value & "' and On_Date='" & CDate(DTP1_SA.Value) & "'"
                Me.DaAp12 = New SqlDataAdapter(strSQL12, con)
                Dim Dset12 As New DataSet
                DaAp12.Fill(Dset12)

                If cell.Value = True Then
                    ' ADD NEW RECORED 
                    If strSQL12 <> "" Then
                        con.Open()
                        Dim VRegNo As String = row.Cells("Regno").Value
                        Dim VS_Name As String = row.Cells("sname").Value
                        cmd.CommandText = "INSERT INTO Attendance (Regno,Stud_Name,On_Date,I,II,III,IV,V,VI,VII,VIII,IX,X) VALUES ('" & VRegNo & "','" & VS_Name & "'," & _
                            "'" & CDate(DTP1_SA.Value) & "','" & CB1.Checked & "','" & CB2.Checked & "','" & CB3.Checked & "','" & CB4.Checked & "','" & CB5.Checked & "', " & _
                            "'" & CB6.Checked & "','" & CB7.Checked & "','" & CB8.Checked & "','" & CB9.Checked & "','" & CB10.Checked & "')"

                        cmd.ExecuteNonQuery()
                        con.Close()

                    Else ' UPDATE 
                        con.Open()
                        cmd.CommandText = ("UPDATE Attendance SET I='" & CB1.Checked & "',II='" & CB2.Checked & "',III='" & CB3.Checked & "', IV='" & CB4.Checked & "', V='" & CB5.Checked & "',  " & _
                                          "  VI='" & CB6.Checked & "' , VII='" & CB7.Checked & "' , VIII='" & CB8.Checked & "' , IX='" & CB9.Checked & "' , X='" & CB10.Checked & "' " & _
                                           " WHERE Regno='" & DGV1_SA.Rows(ix).Cells(1).Value & "' and On_Date='" & CDate(DTP1_SA.Value) & "'")

                        strda1 = cmd.ExecuteReader()
                        con.Close()
                    End If
                End If
            Next ix
        Next row
        MessageBox.Show("UpDate")

[Edit]Code block added[/Edit]
Posted
Updated 9-Mar-13 0:40am
v4
Comments
[no name] 8-Mar-13 12:05pm    
Move your "update" code to the If cell.Value = True code block.
Navas Khanj 8-Mar-13 12:17pm    
i move update but not work

Your code is kind of tough to follow, but I think your problem is on your check as to whether it is an add or an update.
If strSQL12 <> "" And Dset12.Tables(0).Rows.Count = 0 Then


First of all, strSQL12 is never going to be empty, so why are you checking it? You set it a few lines above and never seem to clear it out.

Second, your Dset12 is set before your loop through the rows. You've put data into this table that ONLY matches the first SELECTED row in the grid. It may or may not be a row that has a checkbox checked. It isn't the row that you are currently looking at in your loop.

You need to check, within the loop, if that record exists in the table already or not.
 
Share this answer
 
Comments
Navas Khanj 9-Mar-13 9:42am    
thanks for your Advice
VB
For i As Integer = 0 To DataGridView1.Rows.Count - 1
            '==========================================1=====================

            Dim strSQL12 As String = "select * from AttendanceD WHERE Regno='" & DataGridView1.Rows(i).Cells(1).Value & "' and On_Date='" & CDate(DTP1.Value) & "'"
            Me.DaAp12 = New SqlDataAdapter(strSQL12, con)
            Dim Dset12 As New DataTable
            DaAp12.Fill(Dset12)
            DataGridView2.DataSource = Dset12
            Dset12.AcceptChanges()
            '=================================================================
            Dim ch1 As New DataGridViewCheckBoxCell()
            ch1 = CType(DataGridView1.Rows(DataGridView1.Rows(i).Index).Cells(0), DataGridViewCheckBoxCell)

            Select Case ch1.Value
                Case "True"
                    If strSQL12 <> "" AndAlso Dset12.Rows.Count - 1 Then '1
                        con.Open()
                        Dim VRegNo As String = DataGridView1.Rows(i).Cells("Regno").Value
                        Dim VS_Name As String = DataGridView1.Rows(i).Cells("Sname").Value
                        cmd.CommandText = "INSERT INTO AttendanceD (Regno,SName,On_Date,I,II,III) VALUES ('" & VRegNo & "','" & VS_Name & "'," & _
                            "'" & CDate(DTP1.Value) & "','" & CB1.Checked & "','" & CB2.Checked & "','" & CB3.Checked & "')"
                        cmd.ExecuteNonQuery()
                        con.Close()
                    Else
                        con.Open()
                        cmd.CommandText = ("UPDATE AttendanceD SET I='" & CB1.Checked & "',II='" & CB2.Checked & "',III='" & CB3.Checked & "' " & _
                                           " WHERE Regno='" & DataGridView1.Rows(i).Cells(1).Value & "' and On_Date='" & CDate(DTP1.Value) & "'")
                        strda1 = cmd.ExecuteReader()
                        con.Close()
                    End If '1
                    ' MessageBox.Show("Add")
                Case "False"
                    '  ch1.Value = True
                    MessageBox.Show("U?pdate")
            End Select

        Next i
        MessageBox.Show("Add")
 
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