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

SqlDataReader question

I'm having a problem where I exec a stored procedure (which SELECTs all rows
from a table, which has 100 rows) - each row has 8 columns.

When I exec the code (see below) I get 8 in oSQLDR.FieldCount (which I
expect but whenever I try and reference the data in the row
(oSQLDR.GetInt32(0);) I get an exception "invalid attempt to read when no
data is present". If I exec the SP in an ISQL window using: EXEC getEvents,
I get 100 rows returned.

What am I missing here ?
<snip>

oSqlCmd = new SqlCommand();
oSqlCmd.Connection = m_oSqlConnection; // A valid connection Object
is here
oSqlCmd.CommandText = "getEvents";
oSqlCmd.CommandType = CommandType.StoredProcedure;

SqlDataReader oSQLDR = oSqlCmd.ExecuteReader();
int iRows = oSQLDR.FieldCount;
iRowsAffected = oSQLDR.RecordsAffected;

int iID = oSQLDR.GetInt32(0);

<snip>

- peteZ
Nov 15 '05 #1
3 2997
You need to call the Read() method first on your SqlDataReader object, to
advance to the first row in your result set. Likewise for subsequent
records.

Use a loop to read through all the rows returned, like so:

while (SQLDR.Read())
{
// do something and access SQLDR.GetInt32(0)...
}

Hope that helps.

Cheers,
Wim Hollebrandse
http://www.wimdows.com
http://www.wimdows.net

"PeteZ" <pe***@aol.com> wrote in message
news:eL**************@tk2msftngp13.phx.gbl...
I'm having a problem where I exec a stored procedure (which SELECTs all rows from a table, which has 100 rows) - each row has 8 columns.

When I exec the code (see below) I get 8 in oSQLDR.FieldCount (which I
expect but whenever I try and reference the data in the row
(oSQLDR.GetInt32(0);) I get an exception "invalid attempt to read when no
data is present". If I exec the SP in an ISQL window using: EXEC getEvents, I get 100 rows returned.

What am I missing here ?
<snip>

oSqlCmd = new SqlCommand();
oSqlCmd.Connection = m_oSqlConnection; // A valid connection Object
is here
oSqlCmd.CommandText = "getEvents";
oSqlCmd.CommandType = CommandType.StoredProcedure;

SqlDataReader oSQLDR = oSqlCmd.ExecuteReader();
int iRows = oSQLDR.FieldCount;
iRowsAffected = oSQLDR.RecordsAffected;

int iID = oSQLDR.GetInt32(0);

<snip>

- peteZ

Nov 15 '05 #2
In addition to my last post. If you only want the first record:

if (SQLDR.Read())
{
Console.WriteLine("My value: {0}",SQLDR.GetInt32(0));
}
else
{
Console.WriteLine("No records found.");
}

Cheers,
Wim Hollebrandse
http://www.wimdows.com
http://www.wimdows.net

"PeteZ" <pe***@aol.com> wrote in message
news:eL**************@tk2msftngp13.phx.gbl...
I'm having a problem where I exec a stored procedure (which SELECTs all rows from a table, which has 100 rows) - each row has 8 columns.

When I exec the code (see below) I get 8 in oSQLDR.FieldCount (which I
expect but whenever I try and reference the data in the row
(oSQLDR.GetInt32(0);) I get an exception "invalid attempt to read when no
data is present". If I exec the SP in an ISQL window using: EXEC getEvents, I get 100 rows returned.

What am I missing here ?
<snip>

oSqlCmd = new SqlCommand();
oSqlCmd.Connection = m_oSqlConnection; // A valid connection Object
is here
oSqlCmd.CommandText = "getEvents";
oSqlCmd.CommandType = CommandType.StoredProcedure;

SqlDataReader oSQLDR = oSqlCmd.ExecuteReader();
int iRows = oSQLDR.FieldCount;
iRowsAffected = oSQLDR.RecordsAffected;

int iID = oSQLDR.GetInt32(0);

<snip>

- peteZ

Nov 15 '05 #3
wim

thanks mate

peteZ

"Wim Hollebrandse" <wimATwimdows.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
In addition to my last post. If you only want the first record:

if (SQLDR.Read())
{
Console.WriteLine("My value: {0}",SQLDR.GetInt32(0));
}
else
{
Console.WriteLine("No records found.");
}

Cheers,
Wim Hollebrandse
http://www.wimdows.com
http://www.wimdows.net

"PeteZ" <pe***@aol.com> wrote in message
news:eL**************@tk2msftngp13.phx.gbl...
I'm having a problem where I exec a stored procedure (which SELECTs all

rows
from a table, which has 100 rows) - each row has 8 columns.

When I exec the code (see below) I get 8 in oSQLDR.FieldCount (which I
expect but whenever I try and reference the data in the row
(oSQLDR.GetInt32(0);) I get an exception "invalid attempt to read when no data is present". If I exec the SP in an ISQL window using: EXEC

getEvents,
I get 100 rows returned.

What am I missing here ?
<snip>

oSqlCmd = new SqlCommand();
oSqlCmd.Connection = m_oSqlConnection; // A valid connection Object is here
oSqlCmd.CommandText = "getEvents";
oSqlCmd.CommandType = CommandType.StoredProcedure;

SqlDataReader oSQLDR = oSqlCmd.ExecuteReader();
int iRows = oSQLDR.FieldCount;
iRowsAffected = oSQLDR.RecordsAffected;

int iID = oSQLDR.GetInt32(0);

<snip>

- peteZ


Nov 15 '05 #4

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

Similar topics

2
by: Rod | last post by:
Thanks in advance. I have two listboxes on a single asp page. I am trying to use sqldatareader to populate both using two seperate sql stored procs. I can populate each box seperately using...
3
by: Ricola ! | last post by:
Why do I say: SqlDataReader dr; instead of SqlDataReader dr = new SqlDataReader();
7
by: Franck Diastein | last post by:
Hi, when I call ExportData I have this error: Invalid attempt to Read when reader is closed. Telling me that there's a problem with this line: while(_dataR.Read()){ Code:...
3
by: Neil Guyette | last post by:
Hello, Everyone, I'm trying to find information on how to populate a combo box using a SqlDataReader. I want to be able to set the value of the combo's value property different then the...
5
by: Wing | last post by:
Hi all, I execute a stored procedure in my C# code, assign the result to the SqlDataReader object and display it with datagrid. the question I like to ask, is possible to edit the datagrid...
1
by: Arvind P Rangan | last post by:
Hi All, How do you get all the values of a sqldatareader if it contains multiple resultset. Using sqldatareader.nextresult and sqldatareader.read e.g. While sqldatareader.read ' If not...
4
by: mimi | last post by:
Hi Please help me out, I can't find a way to close a sqldatareader when error occur at statement cmd.ExecuteReader(). I can't close it in catch because it is local in try scope and I can't...
2
by: SQL | last post by:
I'm new at this, so please forgive me if my question has a simple answer to it. I'm trying to get a value out of a SqlDataReader. Here is my code to do so: Dim mySQLCommand As New...
3
by: Frank Milverckowitz | last post by:
Hi, Newbie question about SqlDataReader column value access... In java jdbc code to get a value from a table column we can pass the column name (instead of an int index or offset): String...
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
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: 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...

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.