473,382 Members | 1,204 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,382 software developers and data experts.

Looping through a datareader in .net 1.1 reading the data into an array

I am trying to loop through the fields in a datareader so that i can read the data into an array. It works the first time around i.e the first column, but when i=1 a get an "Object reference not set to an instance of an object" error. Please tell me what i am doing wrong!

heres the code:

If rdr.HasRows Then
rdr.Read()
For i = 0 To rdr.FieldCount
arrValues(i) = CStr(rdr.Item(i))
Next
End If
Jun 11 '07 #1
6 2027
nateraaaa
663 Expert 512MB
I am trying to loop through the fields in a datareader so that i can read the data into an array. It works the first time around i.e the first column, but when i=1 a get an "Object reference not set to an instance of an object" error. Please tell me what i am doing wrong!

heres the code:

If rdr.HasRows Then
rdr.Read()
For i = 0 To rdr.FieldCount
arrValues(i) = CStr(rdr.Item(i))
Next
End If
You may want to try something like this.

Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer = 0
  2.  
  3. While rdr.Read
  4.     arrValues(i) = CStr(rdr.Item(i))
  5.     i = (i + 1)
  6.  
  7. End While
Nathan
Jun 12 '07 #2
Frinavale
9,735 Expert Mod 8TB
You may want to try something like this.

Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer = 0
  2.  
  3. While rdr.Read
  4.     arrValues(i) = CStr(rdr.Item(i))
  5.     i = (i + 1)
  6.  
  7. End While
Nathan
Nathan's code should work for you but I still recommend putting it within a Try Catch block just in case.

-Frinny
Jun 12 '07 #3
That doesnt work - i am still getting the same error. Here is the full code snippet. the datareader should contain 1 row with 35 columns in it:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim i
  3. Dim rdr As SqlDataReader
  4. Dim cn As New SqlConnection(ConfigurationSettings.AppSettings("sConnectionString"))
  5. cn.Open()
  6.  
  7. Dim cmd As New SqlCommand("SELECT * FROM tblUser WHERE UserID=" & Session("UserID"), cn)
  8. rdr = cmd.ExecuteReader
  9. Dim arrValues() As String
  10.  
  11. If rdr.HasRows Then
  12. rdr.Read()
  13. For i = 0 To rdr.FieldCount
  14. arrValues(i) = CStr(rdr.Item(i))
  15. Next
  16. End If
  17. cn.Close()
  18. For i = LBound(arrValues) To UBound(arrValues)
  19. Response.Write(arrValues(i))
  20. Next
Jun 12 '07 #4
Frinavale
9,735 Expert Mod 8TB
Check out this .Net Article on how to use a database in your program....there's an example there on how to retrieve your values.

Try it out and let me know if you still have problems :)

-Frinny
Jun 12 '07 #5
it was the array that was causing the problem - i had to resize it for each iteration of the loop. the following code sample now operates as expected:

Expand|Select|Wrap|Line Numbers
  1. If rdr.HasRows Then
  2. rdr.Read()
  3. For i = 0 To rdr.FieldCount - 1
  4. ReDim Preserve arrValues(i)
  5. arrValues(i) = CStr(rdr.Item(i))
  6. Next
  7. End If
  8.  
thanks for your help!
Kamran
Jun 13 '07 #6
Frinavale
9,735 Expert Mod 8TB
it was the array that was causing the problem - i had to resize it for each iteration of the loop. the following code sample now operates as expected:

Expand|Select|Wrap|Line Numbers
  1. If rdr.HasRows Then
  2. rdr.Read()
  3. For i = 0 To rdr.FieldCount - 1
  4. ReDim Preserve arrValues(i)
  5. arrValues(i) = CStr(rdr.Item(i))
  6. Next
  7. End If
  8.  
thanks for your help!
Kamran

Good Stuff!

-Frinny
Jun 13 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Yasutaka Ito | last post by:
Hi, My friend had a little confusion about the working of DataReader after reading an article from MSDN. Following is a message from him... <!-- Message starts --> I was going thru DataReader...
6
by: Filip | last post by:
Hello all, Is there a way to dump a DataReader into an array, or get the number of rows so I can get my array sized. At the moment I'm converting my DataReader into a DataTable so I can get the...
5
by: Jason Huang | last post by:
Hi, Is it possible to bind DataReader to a DataGrid in C# windows form? And how? And can we update data in a DataSet by using the DataReader? Thanks for help. Jason
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
14
by: Bihn | last post by:
I was reading about datareader which is said to be slimmer & faster then dataset. Since the datareader have to go fetching the dat from the database every time it need it, the data it gets then...
17
by: Alan Silver | last post by:
Hello, I have a generic method in a utility class that grabs an sqldatareader and returns it. Due to the fact that (AFAIK), you can't close the database connection before you've read the data,...
11
by: ^MisterJingo^ | last post by:
Hi all, I have a form with 4 dropdownlist controls which I populate with data from DB tables. I have a class with a method which constructs a dataset, putting each DB table into a dataset table....
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
2
by: =?Utf-8?B?ZGJhMTIz?= | last post by:
Note, I'm using C# I am wondering what type of control I should use for iterating through the set of records returned by callin this method below, so that I can then do some stuff with each...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.