473,289 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

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

May 18 '06 #1
3 1951
With Me.ddlSendDepartment

.DataSource = tblScreen

.DataTextField = "DepartmentName"

.DataValueField = "DepartmentID"

.. DataBind()

End With

"Jerry" <je*******@gmail.com> wrote in message
news:11*********************@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

May 18 '06 #2
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

May 22 '06 #3
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

May 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nithin | last post by:
My code as an txt attachment. I have 2 drop down list boxes that on selection populate text boxes from my database table. I am able to display the correct values in these text boxes. I have 2...
2
by: Arielle | last post by:
I've argued with this for quite some time and I've seen a number of other people on various other sites asking for this same thing with no answer so I figured that now that I have solved it I would...
0
by: Chicagoboy27 | last post by:
I am wondering how to set an object datsource or any data source for a dropdown list that is a details view. Here is a brief set up. I have a details view that lists a bunch of information. when a...
0
by: Kay O'Keeffe | last post by:
Hello, I have written my own custom control and I want one of its properties to display as a dropdown list when clicked, so the user can select from the list, it would be similar to the asp...
11
by: eureka | last post by:
Hi All, I'm training in Servlets, JSP and JavaScript, I have a web page in which there's a "StudentName" textbox and below it is a "Names" Dropdown list. Initially the Textbox is empty and...
1
by: Mike P | last post by:
I am trying to populate a dropdown from a stored procedure, but I am getting the error 'Object doesn't support this property or method: 'Next' : <!--#include file="include_connection.asp"-->...
1
by: fran7 | last post by:
Hi, Anyone know how to populate a dropdown list with data from database. I have an interface that generates the following code for names in my database. ..asp <%If Trim(rsCard("Author"))<>""...
0
TonFrere
by: TonFrere | last post by:
Hello, I'm building a windows form application in Visual C# using VS 2005. On my form I need to populate a combobox with Invoices# linked to the current reccord's Order# value. This means that: -...
2
by: FatBear | last post by:
Sorry for cross-posting this to microsoft.public.inetserver.asp.db - I didn't know if that group saw much action. Hi All, I'm very new to ASP and am now working on an existing site that is...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.