how to handle exception when a field in database does not allow nulls
<p>I have a field in the database that is nvarchar and it cannot be null.
I am having a problem to display the specific message when an error occurs.
When inserting in my database,I tried two things.
first:</p>

<pre><code>if (string.IsNullOrWhiteSpace(textBoxCity.Text))
    MessageBox.Show("This cannot be empty");
else  
    cmd.Parameters.Add("@city", SqlDbType.NVarChar).Value = textBoxCity.Text;
</code></pre>

<p>second:</p>

<pre><code>try
{
    if(string.IsNullOrWhiteSpace(textBoxCity.Text))
    {
        MessageBox.Show("Cannot be empty");

    }
    else
    {
        cmd.Parameters.Add("@city", SqlDbType.NVarChar).Value = textBoxCity.Text;
    }
}
catch (Exception ex)
{
    if (ex is FormatException)
    {
        MessageBox.Show("Cannot be empty");
        return;
    }
    else if (ex is SqlException)
    {
        MessageBox.Show("Cannot be empty");
    }
    throw;
}
</code></pre>

<p>the second one gives me the right message but it also gives me an exception where it says the scalar must be declared.how can I handle that?
I've tried to give it a db.null,but because a field does not allow nulls,the it gives me another exception,again that is not format,or sql.Can you tell me what kind of exception is this,or how can I handle this?</p>

<p>EDIT: At first I had a mistake,where it was supposed to be nvarchar sqldbtype,there was int.</p>