1,105,391 Community Members

DropDownList.SelectedValue = Not Working

Member Avatar
J'Tok
Light Poster
41 posts since Jan 2008
Reputation Points: 10 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
ericstenson
Posting Whiz in Training
293 posts since Dec 2007
Reputation Points: 5 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 3 [?]
Team Colleague
 
0
 

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.

Member Avatar
ericstenson
Posting Whiz in Training
293 posts since Dec 2007
Reputation Points: 5 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 3 [?]
Team Colleague
 
1
 

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.

Member Avatar
J'Tok
Light Poster
41 posts since Jan 2008
Reputation Points: 10 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
J'Tok
Light Poster
41 posts since Jan 2008
Reputation Points: 10 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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(" ")

Question Answered as of 6 Years Ago by ericstenson
Member Avatar
ericstenson
Posting Whiz in Training
293 posts since Dec 2007
Reputation Points: 5 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 3 [?]
Team Colleague
 
0
 

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.

Member Avatar
J'Tok
Light Poster
41 posts since Jan 2008
Reputation Points: 10 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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,
J'Tok

Member Avatar
ericstenson
Posting Whiz in Training
293 posts since Dec 2007
Reputation Points: 5 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 3 [?]
Team Colleague
 
0
 

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!

Member Avatar
srinivasan_sk
Newbie Poster
1 post since Mar 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
veeruanu
Newbie Poster
1 post since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

Welcome veeruanu.

Please do not resurrect old threads. Read members rules at daniweb - http://www.daniweb.com/forums/faq.php?faq=daniweb_policies

Thread Closed.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article