473,406 Members | 2,281 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,406 software developers and data experts.

What is wrong with this code to populate a DropDownList?

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.CloseConne ction)
While myReader.Read()
'Console.WriteLine(myReader.GetString(0))
DropDownList1.Items.Add(myReader.GetString(0))
End While
myReader.Close()
myConnection.Close()
End Sub
End Class
Nov 19 '05 #1
2 1341
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

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Rui Macdonald" <ru**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.CloseConne ction)
While myReader.Read()
'Console.WriteLine(myReader.GetString(0))
DropDownList1.Items.Add(myReader.GetString(0))
End While
myReader.Close()
myConnection.Close()
End Sub
End Class

Nov 19 '05 #2
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
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.CloseConne ction)
While myReader.Read()
'Console.WriteLine(myReader.GetString(0))
DropDownList1.Items.Add(myReader.GetString(0))
End While
myReader.Close()
myConnection.Close()
End Sub
End Class


Nov 19 '05 #3

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

Similar topics

2
by: Andreas Klemt | last post by:
Hello, for what is the label good when programming in visual studio like this: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Label1: ...
11
by: Viktor Popov | last post by:
Hi, Could someone tell me how can be populated DropdownList from Data Base. I use ASP.NET/C#/MS SQL SERVER. Thanks! Viktor ---
2
by: Microsoft News Group | last post by:
I am populating a dropdown list from a DB Table and cannot get the list to populate! Can anyone see what I am missing? I am telling the dropdownlist to use 2 fields from the record set, then from...
1
by: mr2_93 | last post by:
Hi All, I am new to ASP.NET and I am looking for help with the dropdownlist data control. I wonder if someone could show me how to past the SelectedValue of a data-bind dropdownlist to...
5
by: Stimp | last post by:
I'm populating a dropdown list with non-consecutive values (well the last 3 values are non-consecutive anyway).. What's a shorter way of writing the following?... ddLetMaxPrice.Items.Insert(0,...
1
by: jimb | last post by:
I can get the dropdownlist into the datagrid, and I can populate it, but I can't read it. Anybody have a working example of a dropdownlist in an editable grid? Thanks. -- .....
7
by: Mark Rae | last post by:
This behaviour has existed since v1.x, and it's still there in v2, so I wonder if it is actually a bug or a "feature" :-) If the latter, I can't see the rationale behind it... Here's how to...
0
by: jobs at webdos | last post by:
I have a formview that will either edit or insert records into a database. when I call the page with a query string ?x= its goes to edit mode and loads data from a datasource in the form and load...
6
by: peerraghu | last post by:
hi i am developing website which is a online shoping i want to populate two dropdownlist, in which the first dropdownlist should place the list of a paticular product and the next dropdownlist...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.