I have a stored procedure that insert into a table and return a value indicating if the insert really occured.
If the a insert occurs then it returns the identity of that insert.
I have slimmed down my code here, so you can see.
My problem is that a NullValueReference exceptions occurs when trying to reference the returnvalue.
Is it not possible to get a return value along with a recordset ?
publicint RegisterCustomer(Customer customer)
{
SqlCommand myCommand =newSqlCommand("ins_RegistrerKunde", myConnection);
myCommand.CommandType =CommandType.StoredProcedure;
SqlParameter Epost =newSqlParameter("@.Epost",SqlDbType.VarChar, 25);
SqlParameter Passord =newSqlParameter("@.Passord",SqlDbType.VarChar, 25);
SqlParameter ReturnValue =newSqlParameter("@.RETURN_VALUE",SqlDbType.Int, 4);
ReturnValue.Direction =ParameterDirection.ReturnValue;
Epost.Value = customer.Email;
Passord.Value = customer.Password;
myCommand.Parameters.Add(ReturnValue);
myCommand.Parameters.Add(Epost);
myCommand.Parameters.Add(Passord);
myConnection.Open();
SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.SingleRow);
int isRegistered = (int)myCommand.Parameters["@.RETURN_VALUE"].Value;
myConnection.Close();
if (isRegistered == 1)
{
result.Read();
returnConvert.ToInt32(result["Kunde_ID"]);
result.Close();
}
returnConvert.ToInt32(isRegistered);
}
//Stored Procedure ins_RegistrerKunde
create procedure%PROC% (@.Epost varchar(25), @.Passord varchar(25))
as
begin
IF EXISTS( SELECT EpostFROM tbl_KundeWHERE Epost = @.Epost)
RETURN 0
ELSE
INSERT INTO tbl_Kunde( Epost, Passord)
VALUES (@.Epost, @.Passord)
SELECT @.@.IDENTITY AS Kunde_ID
RETURN 1
end
go
Also, instead of @.@.IDENTITY, use SCOPE_IDENTITY(), as this will returnthe the last Identity value in the current scope. SeeIdentity Crisis andUnderstanding Identity Columns for an explanation.|||
I see.
Thank you.