1

I have a class with following property:

public char Status{get;set;}

This value is passed to T-SQL stored procedure to insert into a table.

The problem arises when no values is assigned to this property. It passes \0 to SP. In SP I am not able to figure out if value is present or not. Both of the following checks fail.

@value = '' OR @value IS NULL 

What is the right way to check if passed parameter (@value CHAR(1)) contains a valid value or not?

The answers to question What is the Null Character literal in TSQL? resolved my issue. Hence closing this question.

Community
  • 1
  • 1
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188

1 Answers1

0

char can not be null, because it is a value type. So something is setting your Status property to '\0'. You should check for that value before calling your stored procedure. If it is found send DBNull.Value in its place to you stored procedure.

if(Status == '\0')
    command.Parameters.AddWithValue("@value", DBNull.Value);
else
    command.Parameters.AddWithValue("@value", Status);

Then in your stored procedure you can simply check for

@value IS NULL

If you are not able to change the code, then inside the stored procedure you can check for the character value of 0 like so:

@value = CHAR(0)
Mike Hixson
  • 5,071
  • 1
  • 19
  • 24