What is wrong with this code to populate a DropDownList?


R

Rui Macdonald

What is wrong with this code to populate a DropDownList?

Can somebody Help me?

Tnx

RMac
=====================================================================================
WebForm.aspx.vb

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Threading.Thread
Imports System.Web.UI.WebControls.DropDownList
Public Class WebForm2
Inherits System.Web.UI.Page
Protected WithEvents DropDownList1 As
System.Web.UI.WebControls.DropDownList

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Public dbRead
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim sql
Dim myConnection As New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" &
Server.MapPath("/db/ProdutosFotografia.mdb"))

sql = "SELECT Distinct Marca FROM Produtos"
Dim myCommand As New OleDbCommand(sql, myConnection)
myCommand.Connection.Open()
Dim myReader As OleDbDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
While myReader.Read()
'Console.WriteLine(myReader.GetString(0))
DropDownList1.Items.Add(myReader.GetString(0))
End While
myReader.Close()
myConnection.Close()
End Sub
End Class
 
Ad

Advertisements

K

Karl Seguin

Are you getting an error?

You could just use

dropdownlist1.DataSource = dr
dropdownlist1.DataTextField = "Marca"
dropdownList1.DataBind()

but it'll do the same..null reference? not displaying what you thought it
hsould?

Karl
 
Ad

Advertisements

B

Brock Allen

Is the problem that you're getting is that on postbacks you're not getting
the rigth item selected? If so, then only build the DDL if it's not a postback.
You shouldn't repopulate the list when there is a postback as that removes
all the entires (and the one the user selected). So wrap all your code in
Page_Load in something like this:

Sub Page_Load()
If Not IsPostBack Then
' all of your code here
End If
End Sub

-Brock
DevelopMentor
http://staff.develop.com/ballen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top