DropdownList control populated with a dataReader

G

Guest

Hello,

I am having a problem getting the selectedValue from a dropdownlist that is
populated with a dataReader and just can't see the problem.

I did the following:

dim dr as DataReader
dr = DataReader(sSQLcmd)
Me.DropDownList1.DataSource = dr
Me.DropDownList1.DataTextField="DataSourceField"
Me.DropDownList1.DataValueField="DataValueField"

The above populates okay the control.

I am trying to get the selectedValue as below:

dim SelValue as String
SelValue = Me.DropDownList1.SelectedValue

SelValue always has "".

What am I doing wrong?

Thank you,

Carlos
 
K

Kerem OZMAN

Hi Carlos,

Could you please send the exact code. Because it is essential where you
declared the SelValue variable and where you are assigning SelValue's actual
value in your code. Remember that - since HTTP is a stateless protocol -
ASP.NET can not implicitly retain (class global) variable values between
page postbacks. To be more specific. If you defined SelValue globally inside
your page class and assigning its value inside a class method (which is not
called between every postback), then SelValue might be losing its value
after a postback. Try to use DropDownList.SelectedValue directly since
controls can retain their state (and property values) using ViewState and
ControlState.
 
G

Guest

Hello Kerem,

Below you will find all the code behind containing the dropdown control with
the problem.
As additional information, there are two other dropdown controls that work
okay but their values were entered manually using the control properties.

Why the control populated with a datareader does not work?

I really appreciate your help.

Carlos

------------ Here is the code -------
Public Class ParmsRedirect
Inherits System.Web.UI.Page

Public sRedirectPage As String
Public sTitle As String
Public sType As String
Private sMonthYear As String
Private sSwitchboard As String
Private sRedirect As String
Public sCustomerId As String

Protected WithEvents MonthSel As System.Web.UI.WebControls.DropDownList
Protected WithEvents yearSel As System.Web.UI.WebControls.DropDownList
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents lblTitle As System.Web.UI.WebControls.Label
Protected WithEvents Label4 As System.Web.UI.WebControls.Label
Protected WithEvents sFileNum As System.Web.UI.WebControls.TextBox
Protected WithEvents Submit1 As System.Web.UI.WebControls.Button
Protected WithEvents cmdCancel As System.Web.UI.WebControls.Button
Protected WithEvents CmdCancel1 As System.Web.UI.WebControls.Button
Protected WithEvents CustomerSel As System.Web.UI.WebControls.DropDownList
Protected WithEvents Label5 As System.Web.UI.WebControls.Label
Protected WithEvents Submit As System.Web.UI.WebControls.Button

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
sRedirectPage = Request.QueryString("RedirectPage")
sTitle = Request.QueryString("Title")
sType = Request.QueryString("Type")
sSwitchboard = Request.QueryString("switchboard")
Me.lblTitle.Text = "<strong>" & sTitle & "</strong>"
Call fillCustomer() ' This populates the dropdown control
End Sub

Private Sub fillCustomer()
Dim sSQL As String
Dim cDb As New DBNet.cDBNet
Dim dr As SqlClient.SqlDataReader

sSQL = "select RTRIM(lastname) + ', ' + RTRIM(firstname) + ' ' + " & _
"CASE WHEN middlename is null THEN '' ELSE middlename END as
applicantname, file_num " & _
"from Applicant " & _
"where Not lastname Is null And Not firstname Is null and
lastname like 'a%'" & _
"order by lastname, firstname"

dr = cDb.getDataReader(sSQL)
If dr.HasRows Then
Me.CustomerSel.DataSource = dr
Me.CustomerSel.DataTextField = "ApplicantName"
Me.CustomerSel.DataValueField = "File_Num"
Me.CustomerSel.DataBind()
Me.CustomerSel.Items.Insert(0, "")
End If
cDb.closeConnection()
End Sub

Private Sub CustomerSel_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles CustomerSel.SelectedIndexChanged
' The event is fired and gets here, but sCustomerId always gets "".
' The selectedValue has "" (?????)
sCustomerId = Me.CustomerSel.SelectedValue
End Sub

Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Submit.Click

' MothSel and yearSel are dropdown controls too with values entered
manually. They work okay.
sMonthYear = Me.MonthSel.SelectedValue & "/" &
Right(Me.yearSel.SelectedValue, 2)

' The value got from the dropdown event is used below, but never has a value.
Response.Redirect(Server.UrlDecode(sRedirectPage) & "&MonthYear=" &
sMonthYear & "&CustomerId=" & sCustomerId.ToString())

' The problem is related to the dropdown control that was populated with the
' dataReader. Any ideas? Than kyou.
End Sub

End Class

--------------- End of code ------------
 
G

Guest

I just found the problem. I was not checking if the page was post back, so
the control was being cleared.

I added:
If not IsPostBack() then
populatetheDropDownList()
end if

Hope this helps someone in the future.

Carlos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top