Click here to Skip to main content
15,891,926 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The error goes like this:

Column named pID cannot be found
Parameter name: columnName


It lets me delete for the first time. Then the error pops out after the 1st attempt.

Code:

VB
'check for the selected item in list
       If Form7.DataGridView1.Rows.Count > 0 Then
           If Form7.DataGridView1.SelectedRows.Count > 0 Then
               Dim intStdID As Integer = Form7.DataGridView1.SelectedRows(0).Cells("pID").Value
               'open connection
               If Not cnn.State = ConnectionState.Open Then
                   cnn.Open()
               End If

               'delete data
               Dim cmd As New OleDb.OleDbCommand
               cmd.Connection = cnn
               cmd.CommandText = "DELETE FROM NTIdb WHERE pID=" & intStdID
               cmd.ExecuteNonQuery()
               'refresh data
               Me.RefreshData()

               'close connection
               cnn.Close()
           End If
       End If
Posted

I rather guess that something goes wrong in Me.RefreshData().
So could you add some debugging code in the function you showed us: Check the number of Cells in SelectedRows(0), and find out all their names. That might point to what went wrong.
 
Share this answer
 
you might have deleted the last record and there might be no more data either in the datagrid or the database
or instead of me.refreshdata try datagridview1.refresh
or try passing the index instead of "pID"
 
Share this answer
 
You have already deleted the record at Form7.DataGridView1.SelectedRows(0).Cells("pID").Value.
It cannot be found a second time.
 
Share this answer
 
Comments
Shraa1 20-Mar-13 7:52am    
but even if the record is deleted it wont give an error a second time... the question clearly states an error

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