0

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
5
  • Are you checking to see if anything you are sending or returning is null? Commented Feb 27, 2013 at 22:32
  • Everything has a value, it updates correctly in the database. Just doesn't seem to register as a successful update. Commented Feb 27, 2013 at 22:32
  • 1
    OK, everything updates in the database, BUT does @Message have either the words "True" or "False" within it? Commented Feb 27, 2013 at 22:34
  • @KennethK. - beat me to the punch. If you aren't returning 'True' or 'False' this will throw an exception; as it is now. You need to set a breakpoint and check the value of @Message. Commented Feb 27, 2013 at 22:35
  • well, maybe i'm missing something here, but I'm going to state the obvious, where are you assigning a value to @Message??? I'd recommend you a nice ternary with dbnull.value , but you're straight up missing the line I'd recommend it on, pilot error? See Grant Clement's post for syntax. Commented Feb 27, 2013 at 22:42

3 Answers 3

3

Taking an educated guess, I think sqlComm.Parameters["@Message"].Value is returning System.DBNull.Value.

Is it possible that @@ROWCOUNT is equal to 1 then therefore @Message isn't being set?

Sign up to request clarification or add additional context in comments.

Comments

1
AS
BEGIN
Set @Message = 0
    BEGIN TRY
        BEGIN TRAN
            BEGIN
                UPDATE Customer
                SET
                    Password = @Password,
                    RecordTimeStamp = @NewRecordTimeStamp
                WHERE CustomerID = @CustomerID AND RecordTimeStamp = @OldRecordTimeStamp
            END
            IF @@ROWCOUNT = 1
                    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

Comments

0

Unless my T-Sql is terrible this should work.

BEGIN
    SET @Message = 0;

    BEGIN TRANSACTION

    BEGIN TRY
        UPDATE Customer
        SET Password        = @Password,
            RecordTimeStamp = @NewRecordTimeStamp
        WHERE CustomerID    = @CustomerID
        AND RecordTimeStamp = @OldRecordTimeStamp

        IF @@ROWCOUNT <> 1
            Set @Message = 1;

        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH

        DECLARE @Err nvarchar(500)
        SELECT @Err = ERROR_MESSAGE()
        RAISERROR(@Err, 16, 1)

        ROLLBACK TRANSACTION
    END CATCH
END

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.