Skip to content

Delete multiple rows on button click

Sandhya Bhosale

Asked Oct 19, 2006, 10:07 PM EDT2 answers
Share
LinkedInX
Hi,

I have created selectall checkbox(item template) in the datagrid and want to delete one or more rows depending on if the user clicks on individual checkbox or selectall checkbox.

My logic is  : delete from table where id=@ id   (for one row) or

delete from table where id IN(1,2)      (multiple rows)


If selectall checkbox is checked, all the rows in the particular page will be selected.

But I am not getting the correct syntax to delete the rows.

The code I have written is:



protected void btndelete_Click(object sender, EventArgs e)

{

foreach (DataGridItem dgi in MyDataGrid.Items)

{

CheckBox checkall;

checkall = (CheckBox)dgi.FindControl("chkselect");

string topicid = MyDataGrid.DataKeys[dgi.ItemIndex].ToString();

if (checkall.Checked==true)

{

 string delrecord="delete from Admin_Add_Newsletter_Topic where Topic_Id=" & topicid;

SqlConnection con=new SqlConnection(ConnectionString);

SqlCommand cmd=new SqlCommand(delrecord, con);

con.Open();

cmd.ExecuteNonQuery();

con.Close();

}

}

}


But I receive the following error:


Error 1 Operator '&' cannot be applied to operands of type 'string' and 'string'

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    In C#, you cannot use & operator; Use + operator instead. Moreover, what you are doing is firing DB operation for each item. You should instead take the IDs of the selected items & then fire only one DB Query.
    0
  • 0
    Thanks a lot.

    Ur solution worked.
    0