I have a drop down list that needs to show content that has been put into a datatable using the SqlHelper.FillDataTable. I use a for each statement to put each value into a list item and then add that item to the drop down list values. I would like to add this item to the drop down list only if the first letter of the name(these are names from a database) is between A and K. The current if statement I wrote results in no values being put into the drop down list. Here is the current code:
ListItem NewItem = new ListItem();
NewItem.Text = dr_UsersToShow["ViewValue"].ToString();
NewItem.Value = dr_UsersToShow["DataValue"].ToString();
string str = Convert.ToString(NewItem.Text[0]);
if (String.Compare("str", "A", true) >= 0 && String.Compare("str", "K", true) <= 0)
{
this.dlNewUserList.Items.Add(NewItem);
}
You are comparing the literal “str” to “A” and “K”. You want to compare the string
strto “A” and “K”. Simply remove the quotes from “str”.Change this
to this