1

I have a CheckBoxList that needs to be populated from a SQL Server database that is separated by :.

aspx page

<asp:CheckBoxList ID="chkTopics" runat="server" CssClass="ChkList">
 <asp:ListItem Value="ABC">ABC</asp:ListItem>
 <asp:ListItem Value="D-E-F">D-E-F</asp:ListItem>
 <asp:ListItem Value="GHI">GHI</asp:ListItem>
 <asp:ListItem Value="J,K,L">J,K,L</asp:ListItem>
 <asp:ListItem Value="MNO">MNO</asp:ListItem>
 <asp:ListItem Value="PQR">PQR</asp:ListItem>
</asp:CheckBoxList>

.cs page on what how I am getting the values:

SqlConnection conn = null;
conn = new SqlConnection(conn_string);

SqlCommand sqlCommand = new SqlCommand("aspnet_GetAnswers", conn);
sqlCommand.CommandType = CommandType.StoredProcedure;

conn.Open();

SqlDataReader rdr = sqlCommand.ExecuteReader();

if (rdr.Read())
{
  FirstName.Text = rdr.GetValue(2).ToString();
  LastName.Text = rdr.GetValue(3).ToString();
 --> Putting the Code here to read the values and check the values that match.
}

rdr.Close();
conn.Close();
conn.Dispose();

From the Database:

rdr.GetValue(9).ToString();

ABC:GHI:J,K,L:

1
  • 1
    So, what problem are you having? Are you saying that you don't know how to "split" a "string"? That's a hint. Commented Jan 16, 2014 at 16:25

3 Answers 3

1

You can do a string.Split() on the colon, then create each LineItem and add them directly to the CheckBoxList:

var result = rdr.GetValue(9).ToString();
chkTopics.Items.AddRange(result.Split(':')
                               .Select(x => new ListItem { Text = x, Value = x})
                               .ToArray());

If there's a chance you'll have extra colons that would result in empty items, you can remove them using the StringSplitOptions.RemoveEmptyEntries option:

.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries
Sign up to request clarification or add additional context in comments.

1 Comment

Checked this one out and it does not select anything.
1

Trim the last colon:

string trimmedString = optionsStringFromDb.TrimEnd(':');

Get options list:

List<string> myOptions = trimmedString.Split(':').ToList();

Then populate your CheckBoxList:

foreach (string option in myOptions)
{
   chkTopics.Items.Add(new ListItem(option));
}

2 Comments

@mrmcg What do you mean by "doesnt seem to flow" ? You will have to be more specific.
@mrmcg Is it better now? 'optionsStringFromDb is just a generic name (replace by whatever string you hold your query result)
0

Like This

 string str = GetValue(9).ToString().TrimEnd(':');
 string[] strList = str.Split(':');


 foreach (string s in strList)
        {
            foreach (ListItem item in chkTopics.Items)
            {
                if (item.Value == s)
                {
                    item.Selected = true;
                    break;
                }

            }

        }

1 Comment

But the other ones are not showing up. The ones in the Database needs to be Selected.

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.