I am using Visual Studio 2005 to build a website using ASP.NET and VB.NET. It is accessing data on SQL Server 2005.
I have a form (actually it is in a wizard) that needs data populated from a sqldatareader.
This all works for the first two steps in the wizard, but doesn't populate all of the fields for the third. In fact, the only fields it doesn't populate are the dropdownlists.

Here is a sample of what I'm having it do on page load:

Dim AddSystemOS As DropDownList = AddSystem.ContentTemplateContainer.FindControl("ddlOS")

Dim AddProblemTicketCreatedBy As DropDownList = AddProblem.ContentTemplateContainer.FindControl("ddlTicketCreatedBy")

' This first one works fine, but is on the second step of the wizard
                If (dtrReader("OS_ID")) Is System.DBNull.Value Then
                    AddSystemOS.SelectedValue = Nothing
                    Dim OS As New TextBox
                    OS.Text = dtrReader("OS_ID")
                    AddSystemOS.SelectedValue = OS.Text
                End If

'This one doesn't work at all, and is on the third step
                If (dtrReader("OPENEDBY_EMPLOYEE_ID")) Is System.DBNull.Value Then
                    AddProblemTicketCreatedBy.SelectedValue = Nothing
                    Dim CreatedBy As New TextBox
                    CreatedBy.Text = dtrReader("OPENEDBY_EMPLOYEE_ID")
                    AddProblemTicketCreatedBy.SelectedValue = CreatedBy.Text
                End If

I would also like to point out that other controls on the third step are working (checkboxes, and textboxes). It is only the dropdownlists that are not.

Also, something else strage I noticed once I started doing this, the wizard no longer starts on the first step. Instead it starts on the second step.


Hi J Tok... I take it from

AddSystemOS.SelectedValue = OS.Text

That you are trying to set the selected value of the dropdown list to whatever the value is from your database.

1. The dropdownlist has 2 properties. A DataTextField and a DataValueField. Is your DataValue field properly bound to the datasource?

2. If so, set the selected value AFTER DropdownlistX.databound happens. If you are trying to set the value on page load, I am almost 100% sure VS will kick an error.

double click on your dropdownlist and change the end text of the sub from .selectedindexchanged to .databound

then write your code that sets the selected value to the cell value from the database.

if you are doing it on page load, call a dropdownlistX.databind() to make this code function on load.

commented: very helpful +1

Awesome! That pretty much fixed it. However, I still have one dropdownlist that isn't working still. The catch is that this one isn't databound. Of course, in this case, it still needs to be populated by the database's value. I am using the same type of statement as above.

- J'Tok

I'm actually doing it a little different, now that you showed me how to make it work.

If (dtrReader("OPENEDBY_EMPLOYEE_ID")) Is System.DBNull.Value Then
                    AddProblemTicketCreatedBy.SelectedValue = Nothing
                    AddProblemTicketCreatedBy.SelectedValue = dtrReader("OPENEDBY_EMPLOYEE_ID")
                End If

Good news! I figured it out. I had to trim the spaces from the end of what the datareader was returning.

I used the following:

dtrReader("OPENEDBY_EMPLOYEE_ID").ToString.TrimEnd(" ")

You can also update your database type to nvarchar(XXX). This is basically a floating length and will not have any spaces at the end.

You can also update your database type to nvarchar(XXX). This is basically a floating length and will not have any spaces at the end.

ya, I debated doing that, but using nvarchar wasn't worth the resource cost for what I was doing. Setting a static length in this scenario uses much less since I only allow for up to three characters, and storing the extra data for each field to have it reduce it for the few that are only 2 seemed a waste.

Long story short, I was going for a certain amount of performance/efficiency.

Thanks for the input,

i am not even sure you would realistically notice the difference! :) hey-- can you help my dw profile out a little and give me some bonus points? thanks!

Please check if you have set DataTextField and DataValueField. This is required for a data bound object

You cannot set the selected value directly to ddl like this
Dropdown.SelectedValue = Some value;
Dropdown.SelectedIndex = Give the Index of the Item you want to be selected