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

ExecuteReader() v. ExecuteNonQuery()

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 procedure
was not changed inbetween.

Suggestions? Thanks in advance.
Mark

String strConn =
ConfigurationSettings.AppSettings["myConnection"].ToString();
SqlConnection sqlConn = new SqlConnection(strConn);

SqlCommand sqlComm = new SqlCommand("p_my_proc", sqlConn);
sqlComm.CommandType = CommandType.StoredProcedure;

sqlComm.Parameters.Add(new SqlParameter("@bitSomeResult", SqlDbType.Bit));
sqlComm.Parameters["@bitSomeResult"].Direction = ParameterDirection.Output;

sqlConn.Open();
SqlDataReader dr = sqlComm.ExecuteReader();
//sqlComm.ExecuteNonQuery();
String strSomeResult =
sqlComm.Parameters["@bitSomeResult"].Value.ToString(); //***

if (strSomeResult.ToLower() == "True".ToLower())
{
//Do something
}
else
{
//Do something else
}

sqlConn.Close();
Nov 13 '05 #1
1 25232
I believe to retrieve output parameters after an executereader call, you
have to get to the end of the reader, and I think close it.

"Mark" <fi**************@umn.edu> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
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 procedure
was not changed inbetween.

Suggestions? Thanks in advance.
Mark

String strConn =
ConfigurationSettings.AppSettings["myConnection"].ToString();
SqlConnection sqlConn = new SqlConnection(strConn);

SqlCommand sqlComm = new SqlCommand("p_my_proc", sqlConn);
sqlComm.CommandType = CommandType.StoredProcedure;

sqlComm.Parameters.Add(new SqlParameter("@bitSomeResult", SqlDbType.Bit));
sqlComm.Parameters["@bitSomeResult"].Direction = ParameterDirection.Output;
sqlConn.Open();
SqlDataReader dr = sqlComm.ExecuteReader();
//sqlComm.ExecuteNonQuery();
String strSomeResult =
sqlComm.Parameters["@bitSomeResult"].Value.ToString(); //***

if (strSomeResult.ToLower() == "True".ToLower())
{
//Do something
}
else
{
//Do something else
}

sqlConn.Close();

Nov 13 '05 #2

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

Similar topics

0
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. ...
10
by: Mike | last post by:
I know this sounds strange but I am at a loss. I am calling a simple funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement. private void...
6
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
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...
1
by: Francis | last post by:
Hello, My question is: is it better to use DataSets than the simple ExecuteNonQuery() methods. I thought the DataSets would be a better structured way of programming, but it seems to result in...
1
by: shahi | last post by:
Hi all, i have a sp that produce an error when execute by some special parameters. i write a program in vb.net that execute it, but i come accross a strange problem: when i execute the sp...
3
by: keithb | last post by:
What can I put in a stored procedure to control what gets returned by command.ExecuteNonQuery()? I already tried this: param = comm.CreateParameter(); param.ParameterName = "@Success"; ...
2
by: Jason Huang | last post by:
Hi, I am not clear about the difference between the SqlCommand's ExecuteReader and ExecuteNonQuery. Would somebody show me an example to demonstrate the difference? Thanks for help. Jason
2
by: Tony Johansson | last post by:
Hello! If you do an update in the database you will use ExecuteNonQuery but the database update will work just as good if you instead use ExecuteReader or executeScalar. So my question is how...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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
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...
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
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...

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.