0

I want to send NULL values to stored procedure trought C#, so I have something like:

var projName = chkByName.Checked ? sFilter : "NULL";
var designFolio = chkByDesign.Checked ? sFilter : "NULL";

var sql = $"exec getDesignListByProject  @ProjName ='{projName}', @Folio ='{designFolio}'

Once I have the SQL query, I just execute SQL method as:

 var tvTable = db.GetDataSet(sql, "tvTable").Tables["tvTable"];

GetDataSet method:

public DataSet GetDataSet(string sql, string TableName)
{
    this._Errors = false;

    SqlDataAdapter da = new SqlDataAdapter(sql.ToString(), this.dbconn);
    da.SelectCommand.CommandTimeout = 0;

    DataSet ds = new DataSet();

    try
    {
        da.Fill(ds, TableName);
        return ds;
    }
    catch (SqlException e)
    {
        this.HandleSQLError(e, "GetDataSet", sql);
        return null;
    }
}

But if I send parameters with NULL string as above I send to SQL something like

exec getDesignListByProject  @ProjName ='NULL',@Folio='NULL'

But is a string instead NULL SQL value

So I try to change my variable to:

var designFolio = chkByDesign.Checked ? sFilter : DBNull.Value.ToString();

But I just receive null string like:

exec getDesignListByProject  @Folio=''

How can I send NULL T-SQL values? Regards

4
  • 1
    no need to convert "DBNull.Value.ToString();". you can simply send DBNull.Value Commented Oct 17, 2018 at 23:44
  • I try it but I get = Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'System.DBNull' @Gauravsa Commented Oct 17, 2018 at 23:48
  • can you do (object)DBNull.Value Commented Oct 17, 2018 at 23:52
  • 1
    You should be using a SqlCommand object with a stored procedure call and properly typed parameters. This "EXEC procedure '{token}';" stuff is messy, error-prone, not great for plan cache/reuse, and potentially vulnerable to SQL injection. Commented Oct 17, 2018 at 23:55

3 Answers 3

1

Change your SP call to.

var projName = chkByName.Checked ? $"'{sFilter}'" : "NULL";
var designFolio = chkByDesign.Checked ? $"'{sFilter}'" : "NULL";

var sql = $"exec getDesignListByProject  @ProjName ={projName}, @Folio ={designFolio}`
Sign up to request clarification or add additional context in comments.

Comments

0

Assign null to string variable

string str = null;
var designFolio = chkByDesign.Checked ? sFilter : str ;

or

string str = DBNull.Value 
var designFolio = chkByDesign.Checked ? sFilter : str ;

1 Comment

If I send null as you say I just sending it like my second case: exec getDesignListByProject @Folio='' , instead exec getDesignListByProject @Folio= NULL, If I try your second recomendation in str string I just get error: convert type 'System.DBNull' to 'string'
0

You can simply do this:

var projName = chkByName.Checked ? sFilter : (object)DBNull.Value;
var designFolio = chkByDesign.Checked ? sFilter : (object)DBNull.Value;

Your stored procedure can look like this:

create procedure getDesignListByProject
(@projName int = NULL,
@folio int = null)

So, even if you don't pass anything, it will have null value. Then you can use NULL functions like Coalesce and ISNULL etc.

Also,

SqlDataAdapter da = new SqlDataAdapter(sql.ToString(), this.dbconn);

No need to convert sql.ToString(). Its already a string.

2 Comments

I try exactly that, but it's same result as my last try I just get something like exec getDesignListByProject @Folio='' instead exec getDesignListByProject @Folio=NULL
edited the answer to provide some more info which can help

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.