Thursday, August 15, 2019

How do I hand a nullable value into a stored procedure as a parameter from the C# side?

This won't work:

public List<DivisionStatus> GetDivisionsByCds(int? reportId, string connectionString)
{
   var sqlParameter = new SqlParameter()
   {
      SqlDbType = SqlDbType.Int,
      ParameterName = "@CFTCReportID",
      Value = reportId
   };

 
 

This will:

public List<DivisionStatus> GetDivisionsByCds(int? reportId, string connectionString)
{
   var sqlParameter = new SqlParameter()
   {
      SqlDbType = SqlDbType.Int,
      ParameterName = "@CFTCReportID"
   };
   if (reportId == null)
   {
      sqlParameter.Value = DBNull.Value;
   }
   else
   {
      sqlParameter.Value = reportId;
   }

 
 

This could kinda be a sister blog posting to this.

No comments:

Post a Comment