tags:

views:

20

answers:

2

i have a grid view where each row has a check box so for all those roes which are checked i want to save in data base how can i achieve this?

do i need to perform separate database operation for each datarow or i can send all row to database at once do i need to write some special query?

or i can have some temporary table then i can insert all at once please enlighten me on this

+1  A: 

It probably depends on what database engine you are using.

If you want to send all the rows at once, SQL Server supports XML input parameters. You could turn the selected rows into an XML document, send that to a stored procedure, and have that procedure insert the rows from there.

SQL Server 2008 even supports table-value parameters (http://msdn.microsoft.com/en-us/library/bb510489.aspx). You could then pass a DataTable object to your stored procedure (http://msdn.microsoft.com/en-us/library/bb675163.aspx).

svanryckeghem
no i am using simple parameterised query, it's sql server 2005 and fram work 2.0 My problem using foreach i will find all control but how will i save all checked rows to data base at once
NoviceToDotNet
Query text from .NET can contain multiple statements if needed - so this can be one more way.Yet another way would be to create change-set within a dataset and then flush those changes together using DataAdapter.
VinayC
A: 

You can use a for statement or a foreach statement to do this.

Iterate through each of your rows

foreach (DataGridViewRow dr in myDataGridView.Rows)
{
      if(dr.Cells[0].Value != null) //Cells[0] Because in cell 0th cell we have added checkbox
      {
          //Do something with the row
      }     

}

taken from http://www.dotnetspark.com/kb/151-add-checkbox-inside-datagridview-windows.aspx

But if we are talking about more than 100 or 200 rows, you should use a mechanism svanryckeghem has mentioned or a BULK INSERT.

Ranhiru Cooray