Click here to Skip to main content
15,888,271 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have datagridview have check boxes checked

but my problem how to update checked box values from datagridview in database .

C#
button1_click event
{   
        string subjId;   
        List<string> lines = new List<string>();   
        for (int i = 0; i < gvSubjectsList.Rows.Count; i++)   
        {   
       bool Ischecked =Convert.ToBoolean(gvSubjectsList.Rows[i].Cells["Select"].Value);    
            if (Ischecked == true)   
            {   
                subjId = gvSubjectsList.Rows[i].Cells["SubjectId"].Value.ToString();   
                lines.Add(subjId);   
            }   
    }
   //update courses set courseStatus = true where subjId found in lines

    }

my problem how to

//update courses set courseStatus = true where subjId found in lines

How to write query select in c# to get what numbers found in list ?

update courses set courseStatus = true where ??????

what i write in place of ?????????

thanks

What I have tried:

How to get values from list and put it in where condition
Posted
Updated 1-May-18 17:24pm

1 solution

Probably the easiest way would be to do updates individually, inside the loop. This would mean that you prepare the SQL statement and create the parameters outside the loop and inside the loop you set the values for parameters and execute the statement.

Another option would be to gather the keys inside the loop and when done, use an IN comparison in the SQL statement. In other words something like
SQL
UPDATE mytable SET column = value WHERE keycolumn IN (@key1, @key2, @key3...)

To make this easier you can create the parameters and set their values inside the loop.

For a concrete example for the first option, have a look at Properly executing database operations / Use prepare for repeated statements[^]
 
Share this answer
 

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