title: how to pass null value as a datetime to sql server (DAL Layer)
body: <p>i have got a column (nullable but it is Datetime)in sql server ..</p>
  
  <p>if user is not selected any date i need to pass the null value for this field for that purpose I have done like this below</p>
  
  <pre><code>    protected void btnSubmit_Click(object sender, EventArgs e)
      {
          DateTime expiryDate;
          DateTime? expDate = null;
           if (chkExpDate.Checked == true)
          {
              expiryDate = Convert.ToDateTime(txtExpiryDate.Text);
          }
          else if (expDate.HasValue)
          {
              expiryDate = expDate.Value;
          }
          else
              expiryDate = Convert.ToDateTime(DBNull.Value);
           // here I am getting error like "object cannot be cast from DBNULL or value
  </code></pre>
  
  <p>what i need to do if i have to overcome this problem .. I need to pass null value if user is not selected any date how can i do this..</p>
  
  <p>would any one please help on this.. Many thanks</p>
  
  <p>DAL for this </p>
  
  <pre><code>public bool ReAssignLicense(string certificateID, string serialNumber, string newEmail, string ticketID, string backupBy, string customerName, DateTime expDate)
      {
          List&lt;SqlParameter&gt; ParaList = new List&lt;SqlParameter&gt;();
          ParaList.Add(new SqlParameter("@certificate", certificateID));
          ParaList.Add(new SqlParameter("@certSN", serialNumber));
          ParaList.Add(new SqlParameter("@newemail", newEmail));
          ParaList.Add(new SqlParameter("@ticket", ticketID));
          ParaList.Add(new SqlParameter("@bkpBy", backupBy));
          ParaList.Add(new SqlParameter("@customer_name", customerName));
          ParaList.Add(new SqlParameter("@exp_date", expDate));
  
          return SqlHelper.ExecuteNonQuery(new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString),CommandType.StoredProcedure,"sp_Update",ParaList.ToArray()) &gt; 0;
      }
  </code></pre>