Hi! I have a dropdown that is populated by a hashtable. THis part is working fine. But, I have to select an option in this dropdown according to information I get from a SQL Query.

It's like this:

The field in the table can have 0 - 3
0 First
1 Second
2 Third
3 Fourth

I need to display a drop down with all options, which I did through using a hashtable.

Hashtable ht = new Hashtable;
ht.Add("0", "First");
ht.Add("0", "Second");
ht.Add("0", "Third");
ht.Add("0", "Fourth");

lstPlace.DataSource = ht;
lstPlace.DataTextField = "value";
lstPlace.DataValueField = "key";
lstPlace.DataBind;

I was wondering how to have lstPlace now have a certain option selected according to info in the DB. I tried a SQLDataSource, but that didn't seem to work. I am really new to C# so any help would be appreciated. Or if I'm headed the wrong way, letting me know the correct patht would be appreciated, too.

Thanks,

D

well you can do this with bounding your combobox,
that will be a easy way for you.............:)

In ASP.net I know of is

lstPlace.selectedValue=yourValue;

as well as

lstPlace.selectedIndex=yourIndex;

depending upon your requirement will select a particular entry in the drop down list.
Note that C# is case sensitive and my code above might be incorrect for that reason.

As far as hashtable goes, you will need different keys, in your example you are using only "0".

You can have data sources such as datatable or arraylist or dataset. In case of arraylist, the objects in the arraylist at runtime, need the public properties named as your strings in DataTextField / DataValueField.

In case of table, the DataTextField/DataValueField column names.

Please close thread if this helps.

well you can do this with bounding your combobox,
that will be a easy way for you.............:)

Since I have it bound to the hashtable, it gives an error when I try to bind it to the datasource. I assume it can't be bound to two different things, which makes sense. But I need the option that is in the record to display as selected in the drop down, while populating the dropdown with the list (ID and Description). Is this possible. I know it was fairly easy with Classic ASP, I just have no idea how to do it in ASP.NET.

In ASP.net I know of is

lstPlace.selectedValue=yourValue;

as well as

lstPlace.selectedIndex=yourIndex;

depending upon your requirement will select a particular entry in the drop down list.
Note that C# is case sensitive and my code above might be incorrect for that reason.

As far as hashtable goes, you will need different keys, in your example you are using only "0".

You can have data sources such as datatable or arraylist or dataset. In case of arraylist, the objects in the arraylist at runtime, need the public properties named as your strings in DataTextField / DataValueField.

In case of table, the DataTextField/DataValueField column names.

Please close thread if this helps.

Thanks so much for your reply.

I apologize, I do have different keys (0 - 3). What I'm not sure how to do is find the data I need from the SQLDataSource. I know it runs, I just don't know how to access the vcalue I need. I'm familiar with recordsets in Classic ASP. Is there something similar here?

I found out how to do it without the hashtable

<asp:DropDownList ID="lstPlace" runat="server" SelectedValue='<%# Bind("Place")%>'>
     <asp:ListItem Value="0" Text="First Place" />
     <asp:ListItem Value="1" Text="Second Place" />
     <asp:ListItem Value="2" Text="Third Place" />
     <asp:ListItem Value="3" Text="Fourth Place" />
<asp:DropDownList>

As I had said, I am new to C# and I really appreciate everyone's help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.