Get Value

  • Thread starter Thread starter bbawa1
  • Start date Start date
B

bbawa1

Hi,

I have following stored Procedure. I am writng following ado.net code
to get return value from stored procedure but it doesn't work.

String constr =
System.Configuration.ConfigurationManager.ConnectionStrings["twcsanConnectionString"].ConnectionString;
SqlConnection objConn = new SqlConnection(constr);
string sqlstmt = "usp_Update_tbtickets";
SqlCommand objCmd = new SqlCommand(sqlstmt, objConn);
objConn.Open();

//objCmd.ExecuteNonQuery();
SqlDataReader sqlDr;
sqlDr = objCmd.ExecuteReader();
while (sqlDr.Read())
{
Session["TCKid"] = Convert.ToInt32(sqlDr.GetValue(0));
}
sqlDr.Close();
objConn.Close();

CREATE PROCEDURE [twcsan].[usp_Update_tbtickets]
-- Add the parameters for the stored procedure here


AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE @id INT

SET NOCOUNT ON;
SET @id = (select top 1 ticketid from tbtickets where TcktStatusid
= 1)

update tbtickets
Set tcktopened = GetDate(), TcktAudited = GetDate(),TcktStatusid =
2
where ticketid = @id and TcktStatusid = 1

Return @id
END
 
You want ExecuteScalar I believe.

Or you can declare an output parameter , and get it.

ExecuteReader is for a resultSET. (Select a, b, c from MyTable) , not a
single item.
 
you want the proc return value, while you are trying to fetch the first
column of the resultset. as your resultset is empty there is nothing to
read, so you should use ExecuteNonQuery

add a parameter of ParameterDirection ReturnValue to the SqlCommand
parameter list, then you can read its value after calling ExecuteNonQuery.

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Duplicate keys 1
Pass values 1
urgent please help 3
Stored proc 5
<newbie> Problem with return value from SP 4
Binding problems to dropdowns 2
Getting Identity from SP 1
newbie: my code crashe, NULL value 5

Back
Top