Error with ArrayList

G

Guest

Hi All,
I have a code below:-
That Binds a DropDownList to a database using ArrayList.
This code works well in ASP.NET webMatrix.
But when i use it below in VS.NEt it gives me the error:-
System.NullReferenceException: Object reference not set to an instance of an
object.
Can amybody tell me what 'm doing wrong in VS.NET i have been having problem
with this ArrayList when using it with VS.NET
Thanks


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 MyArrayList As ArrayList
Dim sItem As String

MyArrayList = New ArrayList
If Not Page.IsPostBack Then
Dim strConn As String =
"server=(local);database=Northwind;integrated security=true;"
Dim MySQL As String = "Select CategoryName from Categories"
Dim MyConn As New SQLConnection(strConn)
Dim objDR As SQLDataReader
Dim Cmd As New SQLCommand(MySQL, MyConn)
MyConn.Open()
objDR =
Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
While objDR.Read()
MyArrayList.add(objDR("CategoryName"))
End While
End If
ddl1.DataSource = MyArrayList
ddl1.databind()

End Sub
 
J

Joyjit Mukherjee

Hi,

put these statements
ddl1.DataSource = MyArrayList &
ddl1.databind()
inside the Page.IsPostBack scope to facilitate binding of data to the
dropdownlist when the page is requested for the first time. For next request
onwards, the ViewState will take care off.

HTH
Joyjit
 
G

Guest

HI Joyjit,
Thx for the reply.
I have changed the code to (As u requested)
But still gives the same ERROR! :-

Dim MyArrayList As ArrayList
Dim sItem As String

MyArrayList = New ArrayList
If Not Page.IsPostBack Then
Dim strConn As String =
"server=(local);database=Northwind;integrated security=true;"
Dim MySQL As String = "Select CategoryName from Categories"
Dim MyConn As New SQLConnection(strConn)
Dim objDR As SQLDataReader
Dim Cmd As New SQLCommand(MySQL, MyConn)
MyConn.Open()
objDR =
Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
ddl1.DataSource = MyArrayList
ddl1.DataBind()
While objDR.Read()
MyArrayList.Add(objDR("CategoryName"))
End While
End If
 
G

Guest

Hi patrick,
The code you pasted below seems to be working fine. And IMHO the code should
not work differently in WebMatrix and VS.Net. Do you have any other code in
the page which might raise this exception?
If so please post that code.
the best
srini
 
G

Guest

Hi,
This is the full code ..i don't have any other code with it..
Code below:-
Just wondering if the way i'm definfing ArrayList is ok in .Net!!
Let me know!
--------------------------------------------------

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 MyArrayList As ArrayList
Dim sItem As String

MyArrayList = New ArrayList
If Not Page.IsPostBack Then
Dim strConn As String =
"server=(local);database=Northwind;integrated security=true;"
Dim MySQL As String = "Select CategoryName from Categories"
Dim MyConn As New SQLConnection(strConn)
Dim objDR As SQLDataReader
Dim Cmd As New SQLCommand(MySQL, MyConn)
MyConn.Open()
objDR =
Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
ddl1.DataSource = MyArrayList
ddl1.DataBind()
While objDR.Read()
MyArrayList.Add(objDR("CategoryName"))
End While


End If
'ddl1.DataSource = MyArrayList
'ddl1.databind()

End Sub
 
G

Guest

Just curious to know how you could resolve it
and by the way the code for binding the dropdownlist should be after the
while loop.

the best
srini
 
G

Greg Burns

I'm sure you have your reasons, but why fill an arraylist from datareader
and bind to dropdownlist? Why not just bind dropdownlist to datareader
directly?


If Not Page.IsPostBack Then
Dim strConn As String =
"server=(local);database=Northwind;integrated security=true;"
Dim MySQL As String = "Select CategoryName from Categories"
Dim MyConn As New SQLConnection(strConn)
Dim objDR As SQLDataReader
Dim Cmd As New SQLCommand(MySQL, MyConn)
MyConn.Open()
objDR =
Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

ddl1.DataSource = objDR
ddl1.DataTextField = "CategoryName"
ddl1.DataValueField = "CategoryName"
ddl1.DataBind()

End If

Greg
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top