title: SqlCommand result - Object cannot be cast from dbnull to other types
body: <p>I have got this <code>SqlCommand</code> that <code>Sum all payments for client</code>, but the issue is where there are no payments the result is null or 0 - I'm not sure. But I still receive the same exception: Object cannot be cast from dbnull to other types</p>
  
  <p>What Exception or if statement can fix it?</p>
  
  <pre><code> private void select_payments()
      {
  
          try
          {
  
              SqlCommand sc = new SqlCommand("SELECT SUM(payment) AS sumpayment FROM clientpayments WHERE subkey='" + selectid + "' AND year='" + selectedyear+ "'", con);
  
              con.Open();
              int result = Convert.ToInt32(sc.ExecuteScalar());
              con.Close();
  
              if (result != 0)
              {
                  Convert.ToDecimal(textBox20.Text = result .ToString().Replace(',', '.'));
              }
          }
  
          catch (Exception ex)
          {
              MessageBox.Show(" " + ex.Message.ToString());
  
          }
      }
  </code></pre>
  
  <p>I'm still receiving this exception: </p>
  
  <blockquote>
    <p>object cannot be cast from dbnull to other types</p>
  </blockquote>
  
  <p>I know that this is low-quality question but I'm not sure how can I fix this. </p>
  
  <p>Thank you for your time and answers.</p>