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
Else
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
Else
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.
Thanks,
J'Tok
5
Contributors
10
Replies
2 Years
Discussion Span
3 Years Ago
Last Updated
11
Views
Question Answered
Related Article:how to disable a dropdownlist in asp.net
is a solved ASP.NET discussion thread by vvashishta that has 8 replies and was last updated 2 years ago.
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.
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
Edit:
I'm actually doing it a little different, now that you showed me how to make it work.
Here:
If (dtrReader("OPENEDBY_EMPLOYEE_ID")) Is System.DBNull.Value Then
AddProblemTicketCreatedBy.SelectedValue = Nothing
Else
AddProblemTicketCreatedBy.SelectedValue = dtrReader("OPENEDBY_EMPLOYEE_ID")
End If
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.
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!
You cannot set the selected value directly to ddl like this
Dropdown.SelectedValue = Some value;
Y
Dropdown.SelectedIndex = Give the Index of the Item you want to be selected