hello everyone i just wanna ask some help regarding on how to delete all datas in the data grid view from search result.

i have a form which search data in specific date and show result in the datagridview, what i want is that when delete button is clicked all datas that in the datagridview will be deleted sameway in the database

Dani AI

Generated

asked how to delete every row shown in a DataGridView and have the same rows removed from the database. shared a reference and suggested a row‑by‑row delete, but hit "Parameters @myParam has already been defined." That error comes from re-adding a parameter with the same name to the same SqlCommand on every loop iteration.

Easiest fix (minimal change): add the parameter once and set its Value each iteration — don’t call AddWithValue repeatedly. Example (VB.NET):

Using cn As New SqlConnection(connString)
  cn.Open()
  Using cmd As New SqlCommand("DELETE FROM table1 WHERE RecID = @RecID", cn)
    cmd.Parameters.Add("@RecID", SqlDbType.Int)
    For Each row As DataGridViewRow In DataGridView1.Rows
      If Not row.IsNewRow Then
        cmd.Parameters("@RecID").Value = CInt(row.Cells("RecID").Value)
        cmd.ExecuteNonQuery()
      End If
    Next
  End Using
End Using

A better approach (recommended): perform a single DELETE that matches the same filter you used to populate the grid — e.g., delete by date range — or batch the keys into one command. Single-statement deletes are faster, atomic, and simpler to audit:

Using cn As New SqlConnection(connString)
  Using cmd As New SqlCommand("DELETE FROM table1 WHERE MyDate BETWEEN @Start AND @End", cn)
    cmd.Parameters.Add("@Start", SqlDbType.DateTime).Value = startDate
    cmd.Parameters.Add("@End", SqlDbType.DateTime).Value = endDate
    cn.Open()
    Dim rowsDeleted = cmd.ExecuteNonQuery()
  End Using
End Using

If the grid is data‑bound, prefer marking rows deleted in the DataTable and calling SqlDataAdapter.Update so the UI and DB stay in sync. Always verify with a SELECT first, wrap multi‑row deletes in a transaction, and refresh the grid after the operation.

Recommended Answers

All 3 Replies

hello !
you can perform your required function like this

dim myCon as new sqlconnection("connection string")
dim cmd as new sqlcommand
dim i as integer
myCon.open()
for i = 0 to datagrid1.rows.count -1 
   cmd.commandtext = "delete from table1 where RecID=@RecID"
   cmd.connection = myCon
   cmd.parameter.addwithvalue("@RecID",datagrid1.item(0,i).value)
   cmd.executenonquery()
next
myCon.close

i write this code here in posting thread textbox so may be there is some spelling mistake , but i hope even if this will not solve your prob but this will give you a idea to solve your prob.(sorry for bad english)

Regards

M.Waqas Aslam

hello !
you can perform your required function like this

dim myCon as new sqlconnection("connection string")
dim cmd as new sqlcommand
dim i as integer
myCon.open()
for i = 0 to datagrid1.rows.count -1 
   cmd.commandtext = "delete from table1 where RecID=@RecID"
   cmd.connection = myCon
   cmd.parameter.addwithvalue("@RecID",datagrid1.item(0,i).value)
   cmd.executenonquery()
next
myCon.close

i write this code here in posting thread textbox so may be there is some spelling mistake , but i hope even if this will not solve your prob but this will give you a idea to solve your prob.(sorry for bad english)

Regards

M.Waqas Aslam

it give me this error :

"Parameters @myParam has already been defined"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.