473,406 Members | 2,451 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.

Error with ArrayList

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.Clos eConnection)
While objDR.Read()
MyArrayList.add(objDR("CategoryName"))
End While
End If
ddl1.DataSource = MyArrayList
ddl1.databind()

End Sub
Nov 18 '05 #1
8 1358
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

"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
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.Clos eConnection)
While objDR.Read()
MyArrayList.add(objDR("CategoryName"))
End While
End If
ddl1.DataSource = MyArrayList
ddl1.databind()

End Sub

Nov 18 '05 #2
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.Clos eConnection)
ddl1.DataSource = MyArrayList
ddl1.DataBind()
While objDR.Read()
MyArrayList.Add(objDR("CategoryName"))
End While
End If

"Joyjit Mukherjee" wrote:
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

"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
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.Clos eConnection)
While objDR.Read()
MyArrayList.add(objDR("CategoryName"))
End While
End If
ddl1.DataSource = MyArrayList
ddl1.databind()

End Sub


Nov 18 '05 #3
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
"Patrick.O.Ige" wrote:
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.Clos eConnection)
While objDR.Read()
MyArrayList.add(objDR("CategoryName"))
End While
End If
ddl1.DataSource = MyArrayList
ddl1.databind()

End Sub

Nov 18 '05 #4
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.Clos eConnection)
ddl1.DataSource = MyArrayList
ddl1.DataBind()
While objDR.Read()
MyArrayList.Add(objDR("CategoryName"))
End While
End If
'ddl1.DataSource = MyArrayList
'ddl1.databind()

End Sub
"srini" wrote:
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
"Patrick.O.Ige" wrote:
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.Clos eConnection)
While objDR.Read()
MyArrayList.add(objDR("CategoryName"))
End While
End If
ddl1.DataSource = MyArrayList
ddl1.databind()

End Sub

Nov 18 '05 #5
Its ok Now i figured it out!
But VS.Net work funny sometimes!
Thx mate

"srini" wrote:
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
"Patrick.O.Ige" wrote:
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.Clos eConnection)
While objDR.Read()
MyArrayList.add(objDR("CategoryName"))
End While
End If
ddl1.DataSource = MyArrayList
ddl1.databind()

End Sub

Nov 18 '05 #6
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

"Patrick.O.Ige" wrote:
Its ok Now i figured it out!
But VS.Net work funny sometimes!
Thx mate

"srini" wrote:
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
"Patrick.O.Ige" wrote:
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.Clos eConnection)
While objDR.Read()
MyArrayList.add(objDR("CategoryName"))
End While
End If
ddl1.DataSource = MyArrayList
ddl1.databind()

End Sub

Nov 18 '05 #7
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.Clos eConnection)

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

End If

Greg

"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:70**********************************@microsof t.com...
Its ok Now i figured it out!
But VS.Net work funny sometimes!
Thx mate

"srini" wrote:
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
"Patrick.O.Ige" wrote:
> 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.Clos eConnection)
> While objDR.Read()
> MyArrayList.add(objDR("CategoryName"))
> End While
> End If
> ddl1.DataSource = MyArrayList
> ddl1.databind()
>
> End Sub

Nov 18 '05 #8
Yeah u are right!
But at the same time its interesting try out things.
Thx mate

"Greg Burns" wrote:
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.Clos eConnection)

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

End If

Greg

"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:70**********************************@microsof t.com...
Its ok Now i figured it out!
But VS.Net work funny sometimes!
Thx mate

"srini" wrote:
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
"Patrick.O.Ige" wrote:

> 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.Clos eConnection)
> While objDR.Read()
> MyArrayList.add(objDR("CategoryName"))
> End While
> End If
> ddl1.DataSource = MyArrayList
> ddl1.databind()
>
> End Sub


Nov 18 '05 #9

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

Similar topics

7
by: Philip Wagenaar | last post by:
I use a class to manage tiff's (written by someone else) to splitt multipage tiffs. However, when I run it, it fails, for other people it seems work ok. The method in the class is :public...
1
by: Justin | last post by:
Greetings all, I'm receiving this error when I attempt to pull data, in the form of an ArrayList out of a Session. ArrayList al = new ArrayList(); al = (ArrayList)Session; // error here ...
1
by: Zenobia | last post by:
How do I bind an array, arrayList or even a stack to a repeater containing hyperlinks. The data structure (array, arrayList or even a stack) has dates in ISO format "YYYY-MM-DD". The repeated...
18
by: Microsoft | last post by:
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object" Dim g As System.Drawing.Graphics g.DrawString("Test", New Font("Arial", 12,...
4
by: Alan Silver | last post by:
Hello, I'm trying to use an ArrayList to do data binding, but am getting an error I don't understand. I posted this in another thread, but that was all confused with various other problems,...
2
by: dotnetnoob | last post by:
i got this program that will fetch the data in the excel spreadsheet, it was working before then i make some adjustment and it now give me an error of "Object reference not set to an instance of an...
2
by: vijayrvs | last post by:
SearchCrawler.java The program search crawler used to search the files from the website. From the following program i got 7 compiler error. can any body clarify it and provide me solution. ...
2
by: almurph | last post by:
H ieveryone, Can you help me please? I am trying to sort a hashtable but get the error: "Cannot implicity convert type void to System.Collections.ArrayList" I am doing the following: ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.