Click here to Skip to main content
15,901,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using vs 2010.
I have one problem. When I delete more then one row from backend database using datagridview's checkbox it maintains a my field NO in backend.
But suppose, when I delete a row 2 and 4 then it can not maintain a backend. My programming is that when I delete a row then all then getherthan of that rows No field Number is like n=n-1
My code is here. Can anyone solve it?

VB
Dim i, j, n As Integer
       i = 0
       j = 0
       Dim x As Integer = 0
       Dim a(1000) As String
       a(x) = ""
       Try
           While i < DataGridView1.Rows.Count
               If DataGridView1.Rows(i).Cells(0).Value() Then
                   n = DataGridView1.Rows(i).Cells(1).Value()
                   Call openconnection()
                   'Here It Will Delete your Row From The Database Depending On Your Id....
                   str = "DELETE FROM Assets_Detail Where   No=" & n & ""
                   cmd = New SqlCommand(str, cn)
                   cmd.ExecuteNonQuery()
                   a(x) = n.ToString

                   cn.Close()
                   x = x + 1
               End If
               i = i + 1
           End While
       Catch ex As Exception
           MsgBox(ex.ToString())
       End Try
       While j <= Val(a(x))
           Dim temp As Integer
           temp = Val(a(x))
           Call openconnection()
           str = "update Assets_Detail set No=No-1 where No > " & temp & ""
           cmd = New SqlCommand(str, cn)
           cmd.ExecuteNonQuery()
           cn.Close()

           j = j + 1
       End While
Posted
Updated 16-Dec-10 21:42pm
v2
Comments
Neeil 17-Dec-10 3:58am    
@JF2015:- thank u...sir

1 solution

What is the problem?
It seems like you want to maintain then sequence number after deleting the rows in the middle using:
VB
While j <= Val(a(x))
    Dim temp As Integer
    temp = Val(a(x))
    Call openconnection()
    str = "update Assets_Detail set No=No-1 where No > " & temp & ""
    cmd = New SqlCommand(str, cn)
    cmd.ExecuteNonQuery()
    cn.Close()
    j = j + 1
End While


And that is not working.

If this is the case then:
You have to sort the values in a() in descending order and then loop through a() and use your update query.
BTW, normally use a number as primary key, and don't bother about the items being deleted.
 
Share this answer
 
Comments
Neeil 17-Dec-10 4:41am    
Can u tell me the code for how to sort a() in decending order ??
Prerak Patel 17-Dec-10 5:20am    
Sort it ascending using Array.Sort and loop in reverse order

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