Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void btnUpdate_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.Cells[5].Value != null && Convert.ToBoolean(row.Cells[5].Value))
        {
            query = "update  Client_feedback set OKS_Status = 'true' where ALID = '" + row.Cells[2].Value + "'";
        }
    }
}

I have a datagridview checkbox column and I want if the checkbox is checked "TRUE" value will be updated in sql database.. and if the checkbox is unchecked "false "value will be updated in sql database.. everything is fine..but data is not getting updated in sql.
Thanks for any help.
Posted
Updated 21-Dec-14 22:58pm
v3
Comments
/\jmot 22-Dec-14 5:02am    
did you use command /connection etc, which required to run the sql??
KaushalJB 22-Dec-14 5:05am    
After all what do you do with "query" ?? where is the sqlconnection usage ?
rajshree748 22-Dec-14 5:11am    
i have a class called clsmain where ,connection, command,query is defined..and connecttion is all okay.
DamithSL 22-Dec-14 5:21am    
you need to execute the update statement, please include all related code, if you only provide part of the actual code.
what are the ALID and OKS_Status column data types?
put debug point after you build query and get the runtime generated sql statement. execute it on the sql management studio and check for any errors.

First of all, change OKS_Status data type to bit[^], which accepts 0, 1 or null values.
Secondly, use query like:
SQL
UPDATE  Client_feedback SET OKS_Status = CASE WHEN OKS_Status = 1 THEN 0 ELSE 1 END
WHERE ALID = @alid

Where @alid is proper value (i believe it's integer value).

Complete sample:
SQL
DECLARE @Client_feedback TABLE(ALID INT, OKS_Status BIT)

INSERT INTO @Client_feedback (ALID, OKS_Status)
VALUES(1,0)

SELECT *
FROM @Client_feedback
--returns: 1	0

UPDATE  @Client_feedback SET OKS_Status = CASE WHEN OKS_Status = 1 THEN 0 ELSE 1 END
WHERE ALID = 1

SELECT *
FROM @Client_feedback
--returns: 1	1
UPDATE  @Client_feedback SET OKS_Status = CASE WHEN OKS_Status = 1 THEN 0 ELSE 1 END
WHERE ALID = 1

SELECT *
FROM @Client_feedback
--returns: 1	0


Of course, you can use the same logic with 'boolean' version ;)

If you use SQL server database, i suggest to use stored procedure.
 
Share this answer
 
Comments
Thanks7872 22-Dec-14 8:23am    
:thumbsup: for effort...
Maciej Los 22-Dec-14 8:28am    
Thank you, Rohan ;)
Only writing an Update Query won't update things in Database.

You need to connect to the Database using proper ConnectionString. Then with the help of Command Object, you can define the Query to execute. Then using the Execute Methods of Command Object, you will execute the command, so that the query gets executed and your update will take place.
 
Share this answer
 
Comments
rajshree748 22-Dec-14 5:55am    
database part is fine.
But there is no code for that inside the loop?
Thanks7872 22-Dec-14 6:25am    
Its top secret. :EvilLaugh:
C#
private void btnUpdate_Click(object sender, EventArgs e)
        {


            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[5].Value != null && Convert.ToBoolean(row.Cells[5].Value))
                {
                    query = "update  Client_feedback set OKS_Status = 'true' where ALID = '" + row.Cells[2].Value + "'";
                    ClassMain.ExecuteNonQuery(query);

                }
                else
                {
                    query = "update  Client_feedback set OKS_Status = 'false' where ALID = '" + row.Cells[2].Value + "'";
                    ClassMain.ExecuteNonQuery(query);

                }

            }


//it worked..Thank you all..
 
Share this answer
 
Comments
Maciej Los 22-Dec-14 7:15am    
Why this way?
Please, see my 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