I
am creating a webpage using ASP.net(c#)
I have inserted a CheckBoxList inside the webpage like below
Interested in
Friends
Dating
Business
Activity Partners
where multiple values can be selected
when I click on the submit button,I want to store multiple selected values (text) in the database in a single coulmn.
and when next time the page_Load event fires,I want the multiple values to be fetched using select statement & the stored values must be selected in the CheckBoxList
My idea is to use a String datatype to store multiple values of CheckBoxList by concating & to use substring to fetch & select the multiple items
But I can't do it
Please help me...
Basically, you shouldn't have stored these values in one column. You should use Many-To-Many relationship to store them in another table that has userId and InterestId.
If you want to store them in your way, then I suggest you create another table that stores the interest items with a unique id. Then, use comma delimit to store the id in the column.
"Object reference not set to an instance of an object"
Your database design is not efficient. If you manage to put all that into one column while saving, it is still a trouble for you to extract when you retrieve data. Change your database table to have multiple column. One to represent 1) Friends, 2) Dating,
3) Business and 4) Activity Partners. Populate all with 0. If the checkbox is checked, turn appropriate column (for particular row) to 1.
Please click 'Mark as Answer' if my reply has assisted you
Its Depend on use of that field. You can store it in single field by some inserting some field like , or ~ Or you can use M TO M relationship and create another table which should contain Id and store the selected value in that file with some identity like
user wise.
Hemant Yadav Software Developer
Please remember to click “Mark as Answer” on the post that helps you
As mentioned above, it is not ideal to store the concatenated string in one column. With the foremost reason that you can't easily reference individual interestst. A table 'Interests' with unique entries would make more sense.
However, sometimes the do it quick and dirty way is the best way to solve a (simple) problem. Having that said, you could go for the following:
Loop through the CheckBoxList, get the value of each checkbox which is 'checked' and seperate each item with a comma. In pseudo code:
I would prepare the INSERT statement, instead of doing the INSERT on each checked item. In case you have, for example, 50 values, this approch would get very database connection heavy!
binay00713
Member
18 Points
8 Posts
How to store multiple values of CheckBoxList into Database
Apr 25, 2011 04:07 PM|LINK
I am creating a webpage using ASP.net(c#)
I have inserted a CheckBoxList inside the webpage like below
Interested in
where multiple values can be selected
when I click on the submit button,I want to store multiple selected values (text) in the database in a single coulmn.
and when next time the page_Load event fires,I want the multiple values to be fetched using select statement & the stored values must be selected in the CheckBoxList
My idea is to use a String datatype to store multiple values of CheckBoxList by concating & to use substring to fetch & select the multiple items
But I can't do it
Please help me...
che3358
Star
7941 Points
1357 Posts
Re: How to store multiple values of CheckBoxList into Database
Apr 25, 2011 05:23 PM|LINK
Basically, you shouldn't have stored these values in one column. You should use Many-To-Many relationship to store them in another table that has userId and InterestId.
If you want to store them in your way, then I suggest you create another table that stores the interest items with a unique id. Then, use comma delimit to store the id in the column.
kaiclassic
Member
541 Points
196 Posts
Re: How to store multiple values of CheckBoxList into Database
Apr 26, 2011 07:38 AM|LINK
Your database design is not efficient. If you manage to put all that into one column while saving, it is still a trouble for you to extract when you retrieve data. Change your database table to have multiple column. One to represent 1) Friends, 2) Dating, 3) Business and 4) Activity Partners. Populate all with 0. If the checkbox is checked, turn appropriate column (for particular row) to 1.
hemant.yadav
Contributor
2261 Points
682 Posts
Re: How to store multiple values of CheckBoxList into Database
Apr 26, 2011 09:21 AM|LINK
Hi,
Its Depend on use of that field. You can store it in single field by some inserting some field like , or ~ Or you can use M TO M relationship and create another table which should contain Id and store the selected value in that file with some identity like user wise.
Please remember to click “Mark as Answer” on the post that helps you
jpderooy
Member
459 Points
106 Posts
Re: How to store multiple values of CheckBoxList into Database
Apr 26, 2011 10:09 AM|LINK
As mentioned above, it is not ideal to store the concatenated string in one column. With the foremost reason that you can't easily reference individual interestst. A table 'Interests' with unique entries would make more sense.
However, sometimes the do it quick and dirty way is the best way to solve a (simple) problem. Having that said, you could go for the following:
Loop through the CheckBoxList, get the value of each checkbox which is 'checked' and seperate each item with a comma. In pseudo code:
string interests = string.Empty; for(int i=0; i<CheckBoxList.Items; i++) { If(CheckBoxList.Items[i].Checked) interests += CheckBoxList.Items[i].Value + ","; }Is this helping you?
Jean-Pierre
Feel free to follow me on Twitter!
ganaparthi
Participant
1266 Points
330 Posts
Re: How to store multiple values of CheckBoxList into Database
Apr 26, 2011 11:38 AM|LINK
Hi ,
as all are suggesting its not good practice to store all interests in single column but you can do it like this
For Inserting selected values in database.
string interests = string.Empty; foreach (ListItem item in this.CheckBoxList1.Items) if (item.Selected) interests += item + ","; SqlCommand cmd = new SqlCommand("Insert into Demo values('" + interests + "')", cn); //Write Your Query to insert into database table cn.Open(); int iCount = cmd.ExecuteNonQuery(); cn.Close();For selecting on page load Event
void LoadCheckList() { string intersts = ""; SqlCommand cmd = new SqlCommand("Select Interests from Demo", cn); // Write your Query i have taken as sampleSqlDataReader dr; cn.Open(); dr = cmd.ExecuteReader(); if (dr.Read()) { intersts = dr["Interests"].ToString(); } cn.Close(); string[] arr = intersts.Split(','); foreach (ListItem item in this.CheckBoxList1.Items) { foreach (string s in arr) { if (item.Text== s) { item.Selected = true; } } } }
Hope It Helps you.
Please Mark as Answer if it Helps You.
Srinivas Ganaparthi,
'All things are difficult before they are easy'
My Blog
jpderooy
Member
459 Points
106 Posts
Re: How to store multiple values of CheckBoxList into Database
Apr 26, 2011 12:24 PM|LINK
I would prepare the INSERT statement, instead of doing the INSERT on each checked item. In case you have, for example, 50 values, this approch would get very database connection heavy!
Jean-Pierre
Feel free to follow me on Twitter!
ganaparthi
Participant
1266 Points
330 Posts
Re: How to store multiple values of CheckBoxList into Database
Apr 26, 2011 01:02 PM|LINK
i am also preparing single Insert statement,iam not inserting each cheked item,
its looking like that because i have not used Braces.
string interests = string.Empty; foreach (ListItem item in this.CheckBoxList1.Items) { if (item.Selected) { interests += item + ","; } } SqlCommand cmd = new SqlCommand("Insert into Demo values('" + interests + "')", cn); //Write Your Query to insert into database table cn.Open(); int iCount = cmd.ExecuteNonQuery(); cn.Close();ThanksSrinivas Ganaparthi,
'All things are difficult before they are easy'
My Blog
binay00713
Member
18 Points
8 Posts
Re: How to store multiple values of CheckBoxList into Database
Apr 26, 2011 03:46 PM|LINK
thank you ganaparathi,that worked
ganaparthi
Participant
1266 Points
330 Posts
Re: How to store multiple values of CheckBoxList into Database
Apr 27, 2011 05:31 AM|LINK
Srinivas Ganaparthi,
'All things are difficult before they are easy'
My Blog