Saturday, March 24, 2012

Populate Dropdown from SQL Server

Hello everyone!

I'm trying to populate a dropdown list from SQL Server but it's not
being populated. I've tried all sorts of suggestions and examples that
I found from Google but I just can not get it to work. I am able to
access the database from other pages and display a list of records, but
I just can't get the data to go into the dd list.

Below is my code:

ASPX
<%@dotnet.itags.org. Page Language="VB" AutoEventWireup="false"
CodeFile="addApplication.aspx.vb" Inherits="addApplication" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlAgency" runat="server"
DataTextField="agName" DataValueField="agID" />
</div>
</form>
</body>
</html
VB (code behind)

Partial Class addApplication
Inherits System.Web.UI.Page

Public Sub Page_click(ByVal sender As Object, ByVal e As EventArgs)
'Create a connection string

Dim conStr As String =
ConfigurationManager.ConnectionStrings("connString").ConnectionString

' connString is defined in web.config and works on other pages within
the website project

'Open a connection
Dim objConnection As OleDbConnection
objConnection = New OleDbConnection(conStr)
objConnection.Open()

'Specify the SQL string
Dim strSQL As String = "SELECT * FROM tblAgency;"

'Create a command object
Dim objCommand As OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)

'Get a datareader
Dim objDataReader As OleDbDataReader
objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConn ection)

ddlAgency.DataSource = objDataReader
ddlAgency.DataBind()

'Close the datareader/db connection
objDataReader.Close()
End Sub
End Class

Is there something I'm doing wrong here?

thanks,

--
JerryWith Me.ddlSendDepartment

.DataSource = tblScreen

.DataTextField = "DepartmentName"

.DataValueField = "DepartmentID"

.. DataBind()

End With

"Jerry" <jerryalan@.gmail.com> wrote in message
news:1147987219.226284.39470@.j55g2000cwa.googlegro ups.com...
> Hello everyone!
> I'm trying to populate a dropdown list from SQL Server but it's not
> being populated. I've tried all sorts of suggestions and examples that
> I found from Google but I just can not get it to work. I am able to
> access the database from other pages and display a list of records, but
> I just can't get the data to go into the dd list.
> Below is my code:
> ASPX
> <%@. Page Language="VB" AutoEventWireup="false"
> CodeFile="addApplication.aspx.vb" Inherits="addApplication" %>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head runat="server">
> <title>Untitled Page</title>
> </head>
> <body>
> <form id="form1" runat="server">
> <div>
> <asp:DropDownList ID="ddlAgency" runat="server"
> DataTextField="agName" DataValueField="agID" />
> </div>
> </form>
> </body>
> </html>
> VB (code behind)
> Partial Class addApplication
> Inherits System.Web.UI.Page
> Public Sub Page_click(ByVal sender As Object, ByVal e As EventArgs)
> 'Create a connection string
> Dim conStr As String =
> ConfigurationManager.ConnectionStrings("connString").ConnectionString
> ' connString is defined in web.config and works on other pages within
> the website project
> 'Open a connection
> Dim objConnection As OleDbConnection
> objConnection = New OleDbConnection(conStr)
> objConnection.Open()
> 'Specify the SQL string
> Dim strSQL As String = "SELECT * FROM tblAgency;"
> 'Create a command object
> Dim objCommand As OleDbCommand
> objCommand = New OleDbCommand(strSQL, objConnection)
> 'Get a datareader
> Dim objDataReader As OleDbDataReader
> objDataReader =
> objCommand.ExecuteReader(CommandBehavior.CloseConn ection)
> ddlAgency.DataSource = objDataReader
> ddlAgency.DataBind()
> 'Close the datareader/db connection
> objDataReader.Close()
> End Sub
> End Class
> Is there something I'm doing wrong here?
> thanks,
> --
> Jerry
Thanks for the reply Jeff. I tried your suggestion but I wasn't
successfull.

With Me.ddlAgency
.DataSource = objDataReader
.DataTextField = "agName"
.DataValueField = "agID"
.DataBind()
End With

'ddlAgency.DataSource = objDataReader
'ddlAgency.DataBind()

Did I do it wrong?

--
Jerry
Doh! I caught the mistake. I had the code in the click event handler
and not the page load handler.

Thanks Jeff, your suggestion does work.

--
Jerry

0 comments:

Post a Comment