Dropdownlist not showing all from datasource and default is not bl

J

JB

Hello

I have a dropdownlist that I load from a datasource. The problem is 2
fold. When the page loads the dropdownlist default should be blank. Only
when the user clicks the carat on the side of the dropdownlist, then all of
the selections
should show and then they can select one of those choices from the list.
Instead:

1-When the page loads the dropdownlist shows the first choice in the list
in the dropdownlist box.

The other problem is that not all of the choice are showing in the
dropdownlist box. The datasource is the results of a query that has 5 rows
but:

2-The dropdownlist only shows 4 of the 5 rows that the query returned to
the datasource.

So My questions are:

1-How do I make the dropdownlist default value not show any of the choices
(blank) until the user clicks the carat on the side of the dropdownlist to
making
it reveal all of the choices in the dropdownlist then let them choose one?

2-How do I make all of the 5 rows in the datasource ( query result) show in
the
dropdownlist instead of just 4 of the 5 rows?

a)Below is how the dropdownlist is declared in the aspx page:

<td><asp:DropDownList ID="ddlCity" Width="150px" height="28px"

font-size="15px" runat="server">

b)Below is how the dropdownlist is declared in the codebehind:

SqlConnection ctyConn = new SqlConnection(ConnStr);
string ctySql = @"select Description from tblCity where ID =
'1' ot 'ID' = '2' ";
SqlCommand ctyComm = new SqlCommand(ctySql, ctyConn);
SqlDataReader reader;
ctyConn.Open();
reader = ctyComm.ExecuteReader();
while (reader.Read())
{
ddlCity.DataSource = reader;
ddlCity.DataValueField = "Description";
ddlCity.DataBind();
}
 
G

Gregory A. Beamer

1-How do I make the dropdownlist default value not show any of the
choices (blank) until the user clicks the carat on the side of the
dropdownlist to making
it reveal all of the choices in the dropdownlist then let them choose
one?

Either add a "non choice" to the data source you are binding or
explicitly add the "non choice" to the drop down list after it is bound.
You then need validation code to ensure that value is not selected when
you try to submit the value.

2-How do I make all of the 5 rows in the datasource ( query result)
show in the
dropdownlist instead of just 4 of the 5 rows?

Your problem is here:
while (reader.Read())
{
ddlCity.DataSource = reader;
ddlCity.DataValueField = "Description";
ddlCity.DataBind();
}

You are using a loop to add values, but then you are binding every time
you add a single value. I, personally, am not sure the Reader is the
best option here. It certainly is not as you are attempting to use it.

I still don't understand why it only drops one value, however.

Realistically, you should do one of the following:

1. Distill the reader into objects and then bind
2. Use a DataSet (or Entity or LINQ to SQL) to bind, as they represent a
list

You are looping and adding and then binding with each loop. I would
actually think you would only get one item bound this way, so it has me
a bit shocked that it is actually working at all.

Peace and Grace,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top