Click here to Skip to main content
15,938,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts

I have a c# website application and its database is sql server.
In that I had to get user information about in which field they work. So i used a list box and had some initial data into it. User choose some items from this list box and the data is stored in just ONE column of database table separated by comma.
Listbox has values: category-subcatagory
school hostel-cbse
school hostel-icse
movie-english etc
now if user selects 1st and 2nd option then the column in database table will have value in
"school hostel-cbse,school hostel-icse" form.
I was able to do that!

Now the problem is I want to show these selected fields from database in website in different labels.
so How can I abstract single field from the column? and show it in a label.
Can I create Dynamic Label control according to the number of fields in the column?

I don't know where to start :(

Please Help!!
Vinay Mistry 26-Aug-14 4:31am    
You can use Repeater control.
Ashi0891 26-Aug-14 4:48am    
thanks for suggesting. I will try using a repeater. please help me with "How can I abstract single field from the column? and show it in a label." part as well.
ChintanShukla 26-Aug-14 5:29am    
Do you want some thing like this ?
Column value : abc,pqr,xyz
Label 1 : abc
Label 2 : pqr
Label 3 : xyz
Ashi0891 26-Aug-14 7:08am    
yes sir
ClimerChinna 26-Aug-14 9:24am    
check solution2

Check out this in your case
string strColumn = "school hostel-cbse,school hostel-icse"; // In your case your column
string[] strSplitValues = strColumn.Split(','); //Whatever separator you use e.g comma 

label1.text = strSplitValues[0].ToString();
label2.text = strSplitValues[1].ToString();

//You can also use for loop for assigning 
Share this answer
Ashi0891 27-Aug-14 4:02am    
thank you. your solution helped me to code my problem. I have posted solution3 as my used code. :)
in aspx page

                <asp:Repeater ID="Repeater1" runat="server">

                                <asp:Label ID="lblval" runat="server" Text='<%#Eval("your fieldname") %>'></asp:Label>


in CS Page

protected void Page_Load(object sender, EventArgs e)
 SqlConnection conn = new SqlConnection(your connection string);
        SqlDataAdapter da = new SqlDataAdapter("select command", conn);
        DataSet ds = new DataSet();
        Repeater1.DataSource = ds.Tables[0];
Share this answer
Ashi0891 26-Aug-14 10:50am    
how is this splitting fields from one column?
ClimerChinna 27-Aug-14 0:21am    
did you get what you required or not??
Ashi0891 27-Aug-14 3:59am    
no sir this is simply a repeater control and its databind. My question is entirely something else. anyways, i have solved my problem as in solution3.
I was able to solve my problem with the help of solution1.

this is the required code.

protected void Page_Load(object sender, EventArgs e)
    string data = "school hostel-cbse,school hostel-icse";
    string[] values = data.Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries);

    foreach(string v in values)
        Label l = new Label();
        l.Text = v;
        Panelr1.Controls.Add(new Literal { Text = "<br/>" });
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