Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am Using GridView and I have a CheckBox in it as Template Field I want to give the checked checkbox's value as 1 and the one's not checked as 0 so I want to store all checked one's in one column in DB Table and all unchecked one's in another column how do I do it

here is my code:

C#
string chk;
            for (int i = 0; ChkBoxMarkPr.Items.Count; i++)
            {
                if (ChkBoxMarkPr.Items[i].Selected == true)
                {
                    insert into tablename (columnname) values (i); // or what do I type in here in the insert query.
                }
            }


ChkBoxMarkPr is a instance created for the checkBox in .cs file is this correct?
Posted
Comments
Maciej Los 6-Oct-15 15:30pm    
Wrong approach! You should store both in a single column.

If you store the ones which are checked in database, then the other options are automatically can be considered as unchecked. No need to store that.

You have the ones which are checked. So retrieve that and do whatever you want to do. Other items are not checked. Simple.
 
Share this answer
 
Well, it'd be good if you store both the value in one column only. Because it'd be easy to insert & retrieve.
Make sure you have field of type-of bit for stoing the checkbox values in DB.
C#
string chk;
SqlCommand cmd = null;
for (int i = 0; ChkBoxMarkPr.Items.Count; i++)
{
    cmd = new SqlCommand("INSERT INTO [TABLE_NAME] (COLUMN_NAME) VALUES ("+ ChkBoxMarkPr.Items[i].Selected + ")", yourSqlConnectionObject);
    cmd.ExecuteNonQuery();
}

And yeah, agree with what Tadit said (Solution #1) as well. You can go that way too. Then you have to allow the null values for column. :)

-KR
 
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