473,785 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.FieldCou nt (which I
expect but whenever I try and reference the data in the row
(oSQLDR.GetInt3 2(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.Connect ion = m_oSqlConnectio n; // A valid connection Object
is here
oSqlCmd.Command Text = "getEvents" ;
oSqlCmd.Command Type = CommandType.Sto redProcedure;

SqlDataReader oSQLDR = oSqlCmd.Execute Reader();
int iRows = oSQLDR.FieldCou nt;
iRowsAffected = oSQLDR.RecordsA ffected;

int iID = oSQLDR.GetInt32 (0);

<snip>

- peteZ
Nov 15 '05 #1
3 3018
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.co m> wrote in message
news:eL******** ******@tk2msftn gp13.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.FieldCou nt (which I
expect but whenever I try and reference the data in the row
(oSQLDR.GetInt3 2(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.Connect ion = m_oSqlConnectio n; // A valid connection Object
is here
oSqlCmd.Command Text = "getEvents" ;
oSqlCmd.Command Type = CommandType.Sto redProcedure;

SqlDataReader oSQLDR = oSqlCmd.Execute Reader();
int iRows = oSQLDR.FieldCou nt;
iRowsAffected = oSQLDR.RecordsA ffected;

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.WriteLi ne("My value: {0}",SQLDR.GetI nt32(0));
}
else
{
Console.WriteLi ne("No records found.");
}

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

"PeteZ" <pe***@aol.co m> wrote in message
news:eL******** ******@tk2msftn gp13.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.FieldCou nt (which I
expect but whenever I try and reference the data in the row
(oSQLDR.GetInt3 2(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.Connect ion = m_oSqlConnectio n; // A valid connection Object
is here
oSqlCmd.Command Text = "getEvents" ;
oSqlCmd.Command Type = CommandType.Sto redProcedure;

SqlDataReader oSQLDR = oSqlCmd.Execute Reader();
int iRows = oSQLDR.FieldCou nt;
iRowsAffected = oSQLDR.RecordsA ffected;

int iID = oSQLDR.GetInt32 (0);

<snip>

- peteZ

Nov 15 '05 #3
wim

thanks mate

peteZ

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

if (SQLDR.Read())
{
Console.WriteLi ne("My value: {0}",SQLDR.GetI nt32(0));
}
else
{
Console.WriteLi ne("No records found.");
}

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

"PeteZ" <pe***@aol.co m> wrote in message
news:eL******** ******@tk2msftn gp13.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.FieldCou nt (which I
expect but whenever I try and reference the data in the row
(oSQLDR.GetInt3 2(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.Connect ion = m_oSqlConnectio n; // A valid connection Object is here
oSqlCmd.Command Text = "getEvents" ;
oSqlCmd.Command Type = CommandType.Sto redProcedure;

SqlDataReader oSQLDR = oSqlCmd.Execute Reader();
int iRows = oSQLDR.FieldCou nt;
iRowsAffected = oSQLDR.RecordsA ffected;

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
4750
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 two different methods but when I try to populate them both no the same page I get "InvalidOperationException: Invalid attempt to FieldCount when reader is closed" WHY???
3
536
by: Ricola ! | last post by:
Why do I say: SqlDataReader dr; instead of SqlDataReader dr = new SqlDataReader();
7
2363
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: ************************************************* public void Export2CSV(){
3
5889
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 combo's text property (what the user will see). Is this possible with a SqlDataReader? Thanks
5
12136
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 output (eg. delete a row in the datagrid) and update the change in the corresponding table in the database? thanks for your time.
1
3265
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 sqldatareader then sqldatareader.nextresult
4
2365
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 declare it outside try scope either since we have to call cmd.executeReader to create sqldatareader public string GetLogs(int logID) {
2
1626
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 SqlCommand("SELECT NextPrbKey FROM SystemAdmin", cn) Dim mySQLReader As SqlDataReader = mySQLCommand.ExecuteReader() intGetNextKey = mySQLReader.GetValue(0) This does not return a value. I thought that GetValue(0) would return the
3
2832
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 strLastName = rs.getString( "LastName" );
0
10350
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8983
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.