Click here to Skip to main content
14,300,692 members
Rate this:
Please Sign up or sign in to vote.
See more:
i am used 4 diffrent checkbox i want save them in database on button click
how done this.Pease Help me.anyone Thank you..

What I have tried:

i am not use this code i dont know how do this i am paste this code for only formalty.
Please see my simple question and help me

for (int i = 0; i <=CheckBoxList1.Items.Count-1; i++)
   {

       if (CheckBoxList1.Items[i].Selected)
       {

           if (str == "")
           {
               str = CheckBoxList1.Items[i].Text;
           }
           else
           {
               str += "," + CheckBoxList1.Items[i].Text;

           }

       }
   }
   con.Open();
   SqlCommand cmd = new SqlCommand("insert into aa(a)values('" +str + "')", con);
   cmd.ExecuteNonQuery();
Posted
Updated 19-Feb-19 22:14pm
v2
Comments
Richard MacCutchan 11-Feb-19 12:17pm
   
Look at all the suggested answers on the right hand side ----------->
Gerry Schmitz 11-Feb-19 13:01pm
   
A "check box" is either true or false; what do you expect to save?

YYYYYNNNN?
FFFFFTTTT?

Or?
Member 14083059 12-Feb-19 1:01am
   
i want save checkboxs values in database.suppose i have 4 CheckBox like 1st is Football 2nd is Cricket 3rd one Boxing ...so on
Rate this:
Please Sign up or sign in to vote.

Solution 1

So what do you want to save for the value? A bit field (good idea), the words "True" or False, "Yes" or "No" (bad ideas)

The code you posted is not doing a great deal of good, you are clearly heading down the road to SQL Injection land - SQL Injection - OWASP[^]. Use the correct techniques - Injection Prevention Cheat Sheet - OWASP[^]. Use parameterised queries!

You are also building up a comma-separated list of the checkbox text not the values. Even if you were using the right property storing anything as a comma-separated list in a database is a Bad Idea. Give each text box it's own column.. it is much, much easier to get information in and out of the database that way.

In the bottom right corner of this page is a list of "Related Questions" - try the solutions there first (that is partly what we mean by "do some research"), write some proper code and try it out. If you are still having problems after that come back (with your code) and we will try to help you further.

When you do come back avoid using terms like "plzz hlp me". The words are "please" and "help" - not everyone has English as their first language and using text-speak just makes it harder for them to understand your question - and that means they won't be able to (or inclined to) help you
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Here the solution for using multiple checkBox to save values in your databse table

String activity = "";

   for (int i = 0; i < CheckBoxList1.Items.Count; i++)
   {
       if (CheckBoxList1.Items[i].Selected)
       {
           activity += CheckBoxList1.Items[i].Value + ",";
       }
   }
   activity = activity.TrimEnd(',');


use this code on your button before insert query.
"String activity" is define for your Checkbox.you can use any name instance of "activity" then you use this difened activity in your insert query.
Like this
String update = "update StdRegistration_db set Name= @name,ParentsName= @parentsname,DOB= @DOB,Gender=@Gender,ContactsNo= @contacts ,EmailID= @email,Address=address @,<big>Hobbies= @activity</big> where id=@id";
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100