By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

DropdownList control populated with a dataReader

P: n/a
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

May 23 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
"Carlos Lozano" <Ca**********@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
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

May 23 '06 #2

P: n/a
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 ------------

"Kerem OZMAN" wrote:
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.
"Carlos Lozano" <Ca**********@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
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


May 24 '06 #3

P: n/a
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

"Carlos Lozano" wrote:
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 ------------

"Kerem OZMAN" wrote:
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.
"Carlos Lozano" <Ca**********@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
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


May 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.