Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding the returned value from a SQL query

Status
Not open for further replies.

ProfReynolds

Programmer
Sep 12, 2001
96
US
Okay, so the SQL SPROC looks like this:
Code:
ALTER PROCEDURE [dbo].[UserLogIn] 
	@UserID int = 0,
	@HistoryID int OUTPUT 
AS

Begin
SET NOCOUNT ON;
INSERT INTO UserHistory
   (LOGINID, LogInDateTime)
VALUES (@UserID, GETDATE())

Set @HistoryID = SCOPE_IDENTITY()

End

Then I go to my C# code, and use the following:

Code:
public int AddLogInDateTime(int USERID)
{
   string connectionStr = 
       ConfigurationManager.AppSettings["tdssql"];
   SqlConnection connectObj = new 
       SqlConnection(connectionStr);
   SqlCommand commandObj = new SqlCommand("UserLogIn", 
       connectObj);
   commandObj.CommandType = CommandType.StoredProcedure;
   commandObj.Parameters.Add
       ("@UserID", SqlDbType.Int).Value = 1;
   commandObj.Parameters.Add
       ("@HistoryID", SqlDbType.Int).Direction = 
          ParameterDirection.ReturnValue;


   try
   {
      commandObj.Connection.Open();
      commandObj.ExecuteNonQuery();
      HistoryID = Convert.ToInt32
         (commandObj.Parameters["@HistoryID"].Value);
      commandObj.Connection.Close();
      return returnValue;
   }

   catch
   {
      commandObj.Connection.Close();
      return 0;
   }
}

My problem is that when I include the return value for the identity, the ExecuteNonQuery fails, and it goes in to the catch.

If I remove the identity value (from the C# and the SQL), the insert statement works fine.

I need that History ID, as it helps me track user log-ins, and log-outs, throughout the program.

Thanks!
 
Here's what I've got for ExecuteScalar:

C# Code
Code:
   try
   {
      commandObj.Connection.Open();
      int HistoryID = 
         Convert.ToInt32(commandObj.ExecuteScalar());
      commandObj.Connection.Close();
      return HistoryID;
   }

SPROC Code
Code:
ALTER PROCEDURE [dbo].[UserLogIn]
    @UserID int = 0,
    @HistoryID int OUTPUT
AS

Begin
SET NOCOUNT ON;
INSERT INTO UserHistory
   (LOGINID, LogInDateTime)
VALUES (@UserID, GETDATE())

Select Scope_Identity()

End

Problem is, it renders the same problem. It doesn't finish the try, and goes in to the Catch during the execution of the query.
 
Hi,
Using your first stored proc, change the direction type.
Code:
   commandObj.Parameters.Add
       ("@HistoryID", SqlDbType.Int).Direction =
          [COLOR=blue]ParameterDirection.Output[/color];
To get the value, try this...
Code:
    obj value = (int)commandObj.Parameters["@HistoryID"].Value;

I've gotten rusty with SQL and ADO, but I hope this will help [wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top