Can anyone tell me what is going on in this function??
In the following code snippet, user.Id = 0
, id.Value = 0
and id.SqlDbType = Int
.. as expected since user.Id
is an int field.
However, error.Value = null
and error.SqlDbType = BigInt
. What gives? If I use non-zero it detects an int and the correct value.
Note: the Value properties are the same before and after declaring the parameter direction.
public static long InsertUpdate(User user) {
SqlParameter id = new SqlParameter("@id", user.Id);
id.Direction = ParameterDirection.InputOutput;
cmd.Parameters.Add(id);
SqlParameter error = new SqlParameter("@error_code", 0);
error.Direction = ParameterDirection.Output;
cmd.Parameters.Add(error);
.... other stuff
}
As well, if @SET @error_Code = 0 in the sproc, error.Value = NULL and error.SqlDbType = NVarChar AFTER the procedure runs. If I set it to an integer I get an Int type.
UPDATE: After specifying SqlDbType.Int the parameter now has the correct SqlDbType before and after the command... however the stored procedure is still setting @error_code = null when I in fact set it to 0.
UPDATE: When the sproc executes the SELECT statement the @error_code parameter is always returned as null, regardless of when or not it has been set... this only happens when there's a select statement...
Here is the procedure to reproduce:
ALTER PROCEDURE [dbo].[usp_user_insert_v1]
@username VARCHAR(255),
@password VARCHAR(255),
@gender CHAR(1),
@birthday DATETIME,
@error_code INT OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @default_dt DATETIME
EXEC @default_dt = uf_get_default_date
DECLARE @dt DATETIME = GETUTCDATE()
INSERT INTO users(username, password, gender, birthday, create_dt, last_login_dt, update_dt, deleted)
VALUES(@username, @password, @gender, @birthday, @dt, @default_dt, @default_dt, 0)
SELECT * FROM users WHERE id = SCOPE_IDENTITY()
SET @error_code = 3
RETURN
END
SOLUTION?
Found this link on the ASP forums... apparently you can't read the output parameter until you have read all the results from the SqlDataReader... very unfortunate for me since I decide whether or not I even WANT to read the results based on the output param...