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
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