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
"EXEC procedure '{token}';"stuff is messy, error-prone, not great for plan cache/reuse, and potentially vulnerable to SQL injection.