I am trying to update values in my database (whilst returning a status msg) and am getting the following InvalidCastError - Object cannot be cast from DBNull to other types. However, the information does update in the database. It just doesn't seem to want to return 'true' or success equivalent. Code shown below:
C#:
public bool Update(Customer pCustomer)
{
using (SqlConnection sqlConnect = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
using (SqlCommand sqlComm = new SqlCommand("Update_Customer", sqlConnect))
{
sqlComm.CommandType = CommandType.StoredProcedure;
//Other items to update (Not causing issues)
sqlComm.Parameters.Add("@Message", SqlDbType.Bit).Direction = ParameterDirection.Output;
sqlComm.Connection = sqlConnect;
try
{
sqlComm.Connection.Open();
sqlComm.ExecuteNonQuery();
return Convert.ToBoolean(sqlComm.Parameters["@Message"].Value);
}
catch(Exception)
{
throw;
}
finally
{
sqlComm.Connection.Close();
}
}
}
}
SQL:
//Unrelevent code removed
AS
BEGIN
BEGIN TRY
BEGIN TRAN
BEGIN
UPDATE Customer
SET
Password = @Password,
RecordTimeStamp = @NewRecordTimeStamp
WHERE CustomerID = @CustomerID AND RecordTimeStamp = @OldRecordTimeStamp
END
IF @@ROWCOUNT <> 1
BEGIN
Set @Message = 1
END
IF @@ERROR <> 0
ROLLBACK TRAN
ELSE
COMMIT TRAN
END TRY
BEGIN CATCH
DECLARE @Err nvarchar(500)
SET @Err = ERROR_MESSAGE()
RAISERROR(@Err, 16, 1)
END CATCH
END
GO
null?@Messagehave either the words "True" or "False" within it?@Message.