0

I have a checkbox list in my page and try to databind it. Here is what i do:

SqlCommand cmd=new SqlCommand("Select subj,numb,section from Courses where term=@term",CommonFunctions.con);
cmd.Parameters.AddWithValue("@term",CommonFunctions.currentTerm);
SqlDataAdapter da=new SqlDataAdapter(cmd);
da.Fill(dt);
checkboxlist_courses.DataSource = dt;
checkboxlist_courses.DataTextField = "subj";
checkboxlist_courses.DataBind();

This works fine. The problem is, i can only show the column "subj" in the checkbox list. What i want to do is, i want to show "subj" + "numb" + "section" in the checkbox list. So, what can i do about this?

I tried

checkboxlist_courses.DataTextField = "subj" + "numb" + "section";

but it did not work.

I also tried

SqlCommand cmd=new SqlCommand("Select subj,numb,section as fullname from Courses where term=@term",CommonFunctions.con);
checkboxlist_courses.DataTextField = "fullname";

But this time only "section" column is displayed. Thanks

2
  • Well yeah, is there a field name called sujnumsection? Because all you are doing is combining the string, then assigning it to the property. Commented May 19, 2014 at 16:42
  • @gunr2171 yes, the compiler exactly says the same thing :)) does not work this way Commented May 19, 2014 at 16:42

1 Answer 1

1

Combine fields in SQL query instead:

SqlCommand cmd=new SqlCommand("Select subj + ' ' + numb + ' ' + section as [combined] from Courses where term=@term",CommonFunctions.con);
cmd.Parameters.AddWithValue("@term",CommonFunctions.currentTerm);
SqlDataAdapter da=new SqlDataAdapter(cmd);
da.Fill(dt);
checkboxlist_courses.DataSource = dt;
checkboxlist_courses.DataTextField = "combined";
checkboxlist_courses.DataBind();
Sign up to request clarification or add additional context in comments.

1 Comment

i was thinking about replying my own question because that is exactly what i did just now. But i will accept your answer. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.