473,698 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL, EXECUTEREADER

Hi

I'm programming a windows application. I store values on a SQL-
database.
Now I've a problem reading the values stored.
I want to read a whole column from a table. I tried my SQL Command in
the "Query" from the Microsoft Visual Studio. There I get exactly the
values, which I want.
When I use the same command with a ExecuteReader, the only value which
I get for "reader" is:
System.Data.Sql Client.SqlDataR eader
But I expect a table of values with float values. Does anybody has an
Idea where I made a mistake?

SqlDataReader reader;

try
{
SQLComm.Command Text = "SELECT r.valeur FROM
Resultats_mesur e_testeur ...

SQLComm.Connect ion = conn;
conn.Open();
reader = SQLComm.Execute Reader();
if (reader.HasRows )
{
reader.Read();
test = reader.GetFloat (1);
}
In test I expect a value like "10" or something like that. But when I
debug I got an Error, Indexoutofbound exception.

Thanks for any help in advance!
Cheers,
Marco

Jun 26 '07 #1
5 6434
The array is 0 based.

test = reader.GetFloat (0);
When in doubt , write DEVELOPMENT/DEBUG code like thisl
object o = reader.GetValue ( N ) ;

Where N is an ordinal number.

and put a watch on "o".

Do not do this for production code.



"Givisiez" <mw*****@contri nex.chwrote in message
news:11******** **************@ w5g2000hsg.goog legroups.com...
Hi

I'm programming a windows application. I store values on a SQL-
database.
Now I've a problem reading the values stored.
I want to read a whole column from a table. I tried my SQL Command in
the "Query" from the Microsoft Visual Studio. There I get exactly the
values, which I want.
When I use the same command with a ExecuteReader, the only value which
I get for "reader" is:
System.Data.Sql Client.SqlDataR eader
But I expect a table of values with float values. Does anybody has an
Idea where I made a mistake?

SqlDataReader reader;

try
{
SQLComm.Command Text = "SELECT r.valeur FROM
Resultats_mesur e_testeur ...

SQLComm.Connect ion = conn;
conn.Open();
reader = SQLComm.Execute Reader();
if (reader.HasRows )
{
reader.Read();
test = reader.GetFloat (1);
}
In test I expect a value like "10" or something like that. But when I
debug I got an Error, Indexoutofbound exception.

Thanks for any help in advance!
Cheers,
Marco

Jun 26 '07 #2
Thanks for your quick answer!!

I tried as you described:
object o = reader.GetValue ( N ) ;

for N=0, I get a value, in my case 36, that's great!!! I recive the
value which I should.
But for N=1, I've an exception "IndexOutOfrang eException". But it is a
table with more than 20 values..
So why haven't I access to the other Values?


Jun 26 '07 #3
Hi,

"Givisiez" <mw*****@contri nex.chwrote in message
news:11******** **************@ g4g2000hsf.goog legroups.com...
Thanks for your quick answer!!

I tried as you described:
object o = reader.GetValue ( N ) ;

for N=0, I get a value, in my case 36, that's great!!! I recive the
value which I should.
But for N=1, I've an exception "IndexOutOfrang eException". But it is a
table with more than 20 values..
So why haven't I access to the other Values?
The table maybe has 20 rows but you are returning one column only "valeur "

Modify your query from:
SQLComm.Command Text = "SELECT r.valeur FROM Resultats_mesur e_testeur ...

to

SQLComm.Command Text = "SELECT * FROM Resultats_mesur e_testeur ...
Jun 26 '07 #4

You need to ask yourself?

Am I on a ROW, or am I accessing a COLUMN.

GetString(N) GetDecimal(N) , etc ,etc are COLUMN accessors, now row
accessors.
the while loop will loop over the ROWS.

int rowCounter = 0;
while (dataReader.Rea d())
{

rowCounter ++;
}

Console.WriteLi ne( rowCounter.ToSt ring() ) ;

"Givisiez" <mw*****@contri nex.chwrote in message
news:11******** **************@ g4g2000hsf.goog legroups.com...
Thanks for your quick answer!!

I tried as you described:
object o = reader.GetValue ( N ) ;

for N=0, I get a value, in my case 36, that's great!!! I recive the
value which I should.
But for N=1, I've an exception "IndexOutOfrang eException". But it is a
table with more than 20 values..
So why haven't I access to the other Values?


Jun 26 '07 #5

I tried out earlier SELECT * FROM, but then I get the whole table. The
Select command is right.
The solution was the while(datareade r.Read()) -Loop!!!
Thanks a lot for your help!!

Jun 27 '07 #6

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

Similar topics

0
2522
by: James Hokes | last post by:
Hi All, We're using the 1.1. Framework against SQL Server 2000, and are having a strange issue where we don't get errors back from the stored procedure, i.e. the exception never gets thrown. The scenario is this: 1.) Set up SqlConnection and SqlCommand 2.) Set SqlDataReader equal to the return value from SqlCommand.ExecuteReader
1
25265
by: Mark | last post by:
It appears that you can only retrieve an Output parameter from a SQL Server stored procedure when using the ExecuteNonQuery of the SqlCommand class, and cannot use the ExecuteReader() method. In the code sample below, it bombs on the line with three *** when using the ExecuteReader, but works just fine when you use the ExecuteNonQuery. When using the ExecuteReader, the Output parameter appears to return a value of Null. The stored...
6
8802
by: BuddyWork | last post by:
Does anyone know if there are any articles explaining how I could debug into SqlCommand.ExecuteReader so I can see why I am getting a particular error. thanks
0
1387
by: BuddyWork | last post by:
Hello, The problem is that when using SqlCommand.ExecuteReader and the SQL statement raises an error of severity of 16 then ExecuteReader throws an exception, when you use OdbcCommand.ExecuteReader the exception is not raised but when you call Read() method of the OdbcDataReader you then get the exception which is correct, basically SqlClient does not let you call the Read method, the main problem is that if run a Sql statement of say
5
3104
by: orencs | last post by:
Hello, I am using Microsoft.Practices.EnterpriseLibrary.Data. I am running the following sqlCommand = "SELECT var1 FROM table1 WHERE var2 IN (4,5,6) ; SELECT var3 FROM table2 WHERE var2 IN (4,5,6)"; IDataReader dataReader = db.ExecuteReader(dbCommandWrapper); while
2
11596
by: MattB | last post by:
I'm trying to implement an example I found for displaying images stored in a SQL database. The example code looks like this (in page_load): Dim connstr As String = "Integrated Security=SSPI;Initial Catalog=Northwind;Data Source=SERVER\netsdk" Dim cnn As New SqlConnection(connstr) Dim cmd As New SqlCommand ("select * from images where id=" & Request.QueryString("id"), cnn) cnn.Open() Dim dr As SqlDataReader = cmd.ExecuteReader()
4
4065
by: phil | last post by:
Hi, With the code below, i get the error: ExecuteReader: Connection property has not been initialized. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: ExecuteReader: Connection property has not been initialized.
5
6551
by: Robert Bravery | last post by:
HI all, Whats the differe between useing Executereader() and Beginexecutereader()...endexecutereader() for asyncronys running of a SP Thanks Robert
1
4715
by: fniles | last post by:
I am using OLEDBDataReader to read from an Access database. I have 4 data source where I get my data from, and each data source is in its own thread. When receiving data, if it is a new commodity I am need to read from the database. Since I can receive data from different data source at the same time, it is possible that I read from the database at the same time.
7
13577
by: fniles | last post by:
I am using VB.Net 2003 and MS Access (connecting using OleDBConnection). I read using DataAdapter and DataSet, not DataReader. When many people try to access the database at the same time, I get the error "ExecuteReader requires an open and available Connection. The connection's current state is Open, Executing." I do not use ExecuteReader, why the error says ExecuteReader. What does it mean ? When I get this error, is there a way for me...
0
8676
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9161
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
9029
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...
0
8867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6522
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
4370
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
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
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.