Populate DropDown in Master Page

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

I have a web application with a DropDownList at the top of the Master Page
that I want to populate via code rather than SqlDataSource. I am doing the
same process successfully in other pages in places like RowDatabound event
as I only need DropDowns in edit mode. I cannot get it to work on my
Page_Load event on my Master Page. Can someone tell me what I am doing
wrong? I want to get rid of the SqlDataSource control link to the
dropdownlist. Thanks.

Below is the code-behind on the Master Page and below that is the Class
function I am calling. I tested the SELECT from the database and got 1
record back successfully and I also checked the value in intStaffID and it
is fine.

If Not Request.Cookies("mystaffid") Is Nothing Then
Dim intStaffID As Integer =
Convert.ToInt32(Request.Cookies("mystaffid").Value)
Dim ddl As DropDownList =
Page.Master.FindControl("ddlProgramCode")
ddl.DataSource = LookupClass.GetStaffPrograms(intStaffID)
ddl.DataValueField = "ProgramID"
ddl.DataTextField = "ProgramName"
ddl.DataBind()
ddl.Items.Insert(0, New ListItem("All My Programs", "0"))
End If


Public Shared Function GetStaffPrograms(ByVal intStaffID As Int32) As
SqlDataReader
Dim conData As SqlConnection = New
SqlConnection(ConfigurationManager.ConnectionStrings("MCConnectionString").ConnectionString)
conData.Open()

Dim strSQL As String

strSQL = "SELECT [ProgramID], [ProgramName]" & _
" FROM [vw_StaffPrograms]" & _
" WHERE [StaffID] = " & intStaffID.ToString & _
" ORDER BY [ProgramName]"

Dim cmdSel As SqlCommand = New SqlCommand(strSQL, conData)
Dim dtr As SqlDataReader = cmdSel.ExecuteReader()
Return dtr
End Function
 
When I debugged it and traced through the code it was telling me that the
function was not returning any values. Yet when I run the query on the
database, I get 1 record back.

David
 
Yes and Yes. I have the exact same code in a RowDatabound event on this
same FormView and that is working correct. That is why I wondered if it had
something to do with being in the Page_Load event, but it is really
confusing...even when debugging. Not sure what else to do except I know for
a fact that the previous process using a SqlDatasource worked perfectly but
I don't want to do that.

David
 
I wish asp.net and VS 2008 had an Immediate Window so I could run just the
function.

David
 
Mark Rae said:
Well obviously not!

Run the app, put a breakpoint somewhere, and *THEN* run the function in
the immediate window...
Mark,
I think the problem is that during a Master Page_Load event the values I am
expecting are not yet available (and not sure why). I put them into visible
fields on the content page and the values did not show up until I did a
postback. Would this make a difference?

David
 
Back
Top