Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating dropdown list from Access database

Status
Not open for further replies.

Brito

Technical User
Oct 28, 2002
27
GB
I am currently tryin to construct a webpage using Visual Studio.net and am having real problems in populating a dropdown list from an access database.

I have used an OleConnection and OleCommand to connect to the access database and the connection apears to work, but try as I may I cannot understand how I then stipulate the connection of the dropdown list to the database or the column I wish to have displayed.

Articles I have read either only concentrate on an SQL connection or say click in the Datasource filed within the properties window of the Dropdown list to start a wizaed, but none starts for me.

What am I missing or failing to do?

 
here is the sample code:

Code:
Imports System.Data
Imports System.Data.OleDb

Sub Page_Load(sender as Object, e as EventArgs)

  Const sConnStr as String="Provider=Microsoft.Jet.OLEDB.4.0;" & _
                "Data Source=C:\mydatabase.mdb"
  Dim objConn as New OleDbConnection(sConnStr)
    

  objConn.Open()


   Const strSQL as String = "put your sql query here"
  Dim objCmd as New OleDbCommand(strSQL, objConn)

  Dim objDR as OleDbDataReader
  objDR = objCmd.ExecuteReader()
    
  yourdropdownctl.DataSource = objDR
  yourdropdownctl.DataBind()

End Sub

-DNG
 
FYI,

In my opinion using wizards doesn't give you full control over your app. using your own connection strings and your own select statements (as stated above) give you much more control over your app. if you go with the above, just make sure and close your connections after grabbing your records

e.g. objConn.Close()

open your connection, get your results, then close your connection.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top