views:

354

answers:

3

Good afternoon everyone,

I am having an issue with a stored procedure inserting an incorrect value. Below is a summarization of my stored procedure ...

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE PROCEDURE [dbo].[InsertDifferential]
    @differential int = null
AS
    BEGIN TRY
        BEGIN TRANSACTION
      UPDATE
       DifferentialTable
      SET
       differential = @differential
        COMMIT
    END TRY
    BEGIN CATCH
        IF @@TRANCOUNT > 0
        ROLLBACK
        DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int, @ErrorState INT
        SELECT @ErrMsg = ERROR_MESSAGE(), 
         @ErrSeverity = ERROR_SEVERITY(),
         @ErrorState = ERROR_STATE();
        RAISERROR(@ErrMsg, @ErrSeverity, @ErrorState);
    END CATCH

Below is the code I use to call the stored procedure ...

SqlConnection dbEngine = new SqlConnection(connectionString);
SqlCommand dbCmd = new SqlCommand("InsertDifferential", dbEngine);
SqlDataAdapter dataAdapter = new SqlDataAdapter(dbCmd);

dbCmd.CommandType = CommandType.StoredProcedure;

if (myobject.differential.HasValue)
{ dbCmd.Parameters.AddWithValue("@differential", myobject.differential); }
else
{ dbCmd.Parameters.AddWithValue("@differential", DBNull.Value); }

dbCmd.ExecuteNonQuery();

In the database table, the differential column is a nullable int with no default value.

The differential property of "myobject" is an int? data type set to null by default.

The issue is when I run the stored procedure, the differential column winds up with a 0 in place. Even if "myobject.differential" is null and I pass in the DBNull.Value the column still winds up with a 0 in place. I've tried not passing the @differential into the stored procedure and it still sets the column to 0.

I've tried a number of different solutions and nothing has worked.

Thank you in advance for any assistance,

Scott Vercuski

+1  A: 

I believe that when you set the default value on a parameter like

@differential int = null

You do not need to add it to your SQL Command.

Try the code with just this and do not include the else...

if (myobject.differential.HasValue)
{ 
   dbCmd.Parameters.AddWithValue("@differential", myobject.differential); 
}
RSolberg
RSolberg - This is one of the iterations I've tried in order to fix the issue, unfortunately it still didn't solve the issue.
Scott Vercuski
Without being able to completely reproduce, I am just guessing..... What is the value of the parameter before the command execute is called? Is it 0 at that point?
RSolberg
RSolberg - actually that is something I haven't checked ... I know the VALUE being passed it is null but I haven't actually checked the parameter once it has been set, let me check and repost. Thank you for the suggestion.
Scott Vercuski
RSolberg - I just ran the program and checked the debug info and the Value of dbCmd.Parameters["@differential"].Value is ""
Scott Vercuski
What would you expect that value to be?
RSolberg
RSolberg - I would expect that to be passed in as null, if not I'm not sure what "" translates to in an integer sense, but my guess would be "" translates to NULL, I could be wrong, my SQL knowledge to this degree is limited.
Scott Vercuski
RSolberg - Well hold the phone sir ... you are correct !!! I just ran this statement 'SELECT Convert(int, '')' and it came out with 0 as a result. Althought I don't know why DBNull.Value is passing in '' as a value from my code above.
Scott Vercuski
RSolberg - Thank you !! I greatly appreciate your help! I'll be able to correct things with this info in hand.
Scott Vercuski
@Scott: Great. Best of luck, glad to have helped.
RSolberg
A: 

Are you certian that the differential column does not have a Default Value Constraint on it of zero?

James
A: 

ALso check to see if there is a trigger on the table setting null values to zero.

HLGEM