Dropdown list

minn

Active member
Joined
Apr 10, 2006
Messages
37
Programming Experience
Beginner
Hello,

I have a dropdownlist and from it i wish to extract the currently selected item and store it in a variable as an integer so i can use it in an sql query.

for example, the ddl may have items; 1, 2, 3. if i select 2 from the dropdownlist i want to be able to store the value 2 in the variable.

I have tried something like:

Dim int1 As integer = ddl1.selecteditem.value and
Dim int1 As integer = ddl1.selecteditem.text

but neither is storing the value i require (i.e 2)

Can anyone please help??
 
DropDownList.SelectedIndex property seems to be what you ask for.
 
Thanks mate,

but still no luck, when i make a selection from the ddl and click the event handler button, it just reverts to the first item in the ddl and does not want to know about any of the other items. e.g. ddl shows 1, 2, 3; i select 2 and click a button, the ddl reverts to the first item in the ddl which is 1 and doesnt want to know about the others.

Could this possibly be a post back problem?

Here is my code:

VB.NET:
[SIZE=2][COLOR=#008000]'create a string that holds an sql statement that retrieves the copyID for a copy of a software where the application ID is that of the currently...
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'displayed application and copyno is that which is specified in the copyno dropdownlist...[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#008000]'create a string that holds an sql statement that retrieves the copyID for a copy of a software where the application ID is that of the currently...
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'displayed application and copyno is that which is specified in the copyno dropdownlist...
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sqlCopyID [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "SELECT CopyID FROM Copies Where ApplicationID = " & txtAppID.Text & " AND CopyNo = " & ddlCopies.SelectedIndex
[/SIZE][SIZE=2][COLOR=#008000]'create the commandtext of the command object and tell it to use 'sqlcopyID'... 
[/COLOR][/SIZE][SIZE=2]comBook.CommandText = sqlCopyID
[/SIZE][SIZE=2][COLOR=#008000]'open the connection to the database...
[/COLOR][/SIZE][SIZE=2]con.Open()
[/SIZE][SIZE=2][COLOR=#008000]'create an object that will store the value (the copyID) that is returned by our sql statement...
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objcopyID [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2] = comBook.ExecuteScalar
[/SIZE][SIZE=2][COLOR=#008000]'store the returned value (the copyID) in the integer variable 'copyID'...
[/COLOR][/SIZE][SIZE=2]CopyID = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](objcopyID, [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2])
lblA.Text = CopyID
 
[/SIZE][/COLOR][/SIZE]

what i have done is created a test where it uses a sql statement to get a record in a table where the applicationID is that displayed in another textbox and the copyNo is that which is selected from the ddl. It should then display the record it returned in a label.

However, as i have described, the selected copyNo from the ddl is not being retrieved and thus a wrong record is being displayed.

Any ideas?
 
What you describe about DDL reverting to index 0 after postback is typically something that happens if you fill the DropDownList on every postback.
 
I have checked this and it does not seem to be the case. In my pageload I have:

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'fills the members and copies dropdownlists only when the page loads for the first time...
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] Page.IsPostBack [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]MembersLoad()
CopiesLoad()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]

Also the autopostback property of the ddls is false.

Anymore ideas?
 
Back
Top