ProfReynolds
Programmer
Okay, so the SQL SPROC looks like this:
Then I go to my C# code, and use the following:
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!
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!