C#.net split string and then check appropriate checkboxlist

dkilby
dkilby used Ask the Experts™
on
I have a checkboxlist with 5 options, when saved it is in one database column, with each option seperated by a semi-colon, when the user goes back into the page to edit how do i take the value as a string and make sure it checks the right checkboxes in the list.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
do you want to do it in Window Form or ASP.NET application? do you have some sample codes to share here?
IT Professional
Top Expert 2015
Commented:
As you said, it stored in one column with comma separated; You will get back as a string!  
You simply split into array and manipulate it!
var foos = "Foo1,Foo2,Foo3";
var fooArray = foos.Split(','); 

Open in new window

Use value attribute every checkbox to check and set as selected! Please check complete code (CheckBoxList control)
var foos = "Foo1,Foo2,Foo3";
var fooArray = foos.Split(',');

for (int i = 0; i < cblCourses.Items.Count; i++)
{
	for(int j=0 j<fooArray.Count;j++)
	{
		if (cblCourses.Items[i].Value==fooArray[j]){
			cblCourses.Items[i].Selected = true;
		}
	}
}

Open in new window

Top Expert 2016

Commented:
why not using 5 columns? you always will have troubles with this field and it violates 1NF (first normal form) without any benefit.

Sara

Author

Commented:
thanks for the help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial