C# string with comma separated numbers

377 views Asked by At

I have a series of numbers, comma separated, stored in SQL server. These numbers are the IDs of a Listbox. How can I select the Listboxes in C# based on the stored comma separated string?

CheckBoxList1.DataValueField = "id";
CheckboxList1.DataTextField = "location_name";
CheckBoxList1.DataSource = ds.Tables[0];
ChekBoxList1.DataBind();

The string is stored in SQL server table location, field toegang and the string looks like 34,656,43

1

There are 1 answers

0
abolfazl  sadeghi On

It is better to enter it as create a table in the database and split data into new table

create table Locations(Id int identity(1,1),location_name varchar(100))
Id location_name
1 34
2 656
3 43

If you can't, you can split both in the sql server and in C#

C#(Split)

string data = "123,254,147,741";
var _list= data.Split(',').ToList();

sql server(function STRING_SPLIT) example 1:

SELECT value FROM STRING_SPLIT('123,254,147,741', ',');

example 2:

select id,value as location_nameNew,location_name
from Locations
cross apply (
SELECT value FROM STRING_SPLIT(location_name, ',')
)a