Delete selected value from gridview and database
hi, i am using gridview and how can i delete my selected row of value from the database?
i got 4 column of data from database which is code behind
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 4, 2009, 10:39 PM EST
You haven't changed '2' as I asked. It should be 0 as name is the first column in your gird.
change your code as follows.
Dim row As GridViewRow = GridView1.SelectedRow
cmd.CommandText = "DELETE FROM user_profile WHERE name='" & row.Cells(0).Text & "'"
If you still getting the error do a null check for row and row.Cells(0).
Soh Kok Ting11 reputationPosted Nov 4, 2009, 8:15 PM EST
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 4, 2009, 8:36 PM EST
Soh Kok Ting11 reputationPosted Nov 4, 2009, 8:42 PM EST
Soh Kok Ting11 reputationPosted Nov 4, 2009, 8:44 PM EST
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 4, 2009, 9:04 PM EST
Dim row As GridViewRow = GridView1.SelectedRow
cmd.CommandText = "DELETE FROM user_profile WHERE name='" & row.Cells(2).Text & "'"
Here, 2 shuld be changed to the index of your column 'name' in the gridview. (starting from 0)
Soh Kok Ting11 reputationPosted Nov 4, 2009, 9:12 PM EST
Soh Kok Ting11 reputationPosted Nov 4, 2009, 10:49 PM EST