nscott23 nscott23 - 3 months ago 14
C# Question

how to handle the parameterized query expects the parameter which was not supplied

I am inserting data on a button click.I am trying to handle exceptions for the field.My code is:

try
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO omotnica (id,name,date,number)" +
"VALUES(@id,@naziv_predmeta,@date,@number)";

cmd.Parameters.Add("@id", SqlDbType.Int).Value = idInt;
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 300).Value = this.textBoxName.Text;
try
{
DateTime datt = DateTime.Parse(textBoxDatumUpisa.Text);
textBoxDatumUpisa.Text = datt.ToString("d/M/yyyy");
cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = datt;
}
catch (FormatException)
{
MessageBox.Show("Date must be dd/MM/yyyy");
}
try
{
cmd.Parameters.Add("@number", SqlDbType.Int).Value = Convert.ToInt32(textBoxNumber.Text);

}
catch (SqlException e) when (e.Number == 8178)
{
MessageBox.Show("U must enter something");
}
catch (FormatException)
{
MessageBox.Show("It must be an integer");
}

con.Close();
MessageBox.Show("Saved!");

}
catch (Exception ex)
{
MessageBox.Show(" error" + ex.Message + ex.Source );

}try
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO omotnica (id,name,date,number)" +
"VALUES(@id,@naziv_predmeta,@date,@number)";

cmd.Parameters.Add("@id", SqlDbType.Int).Value = idInt;
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 300).Value = this.textBoxName.Text;
try
{
DateTime datt = DateTime.Parse(textBoxDatumUpisa.Text);
textBoxDatumUpisa.Text = datt.ToString("d/M/yyyy");
cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = datt;
}
catch (FormatException)
{
MessageBox.Show("Date must be dd/MM/yyyy");
}
try
{
cmd.Parameters.Add("@number", SqlDbType.Int).Value = Convert.ToInt32(textBoxNumber.Text);

}
catch (SqlException e) when (e.Number == 8178)
{
MessageBox.Show("U must enter something");
}
catch (FormatException)
{
MessageBox.Show("It must be an integer");
}

con.Close();
MessageBox.Show("Saved!");

}
catch (Exception ex)
{
MessageBox.Show(" error" + ex.Message + ex.Source );

}


When I enter some string in the textBoxNumber,it fires up a FormatException and also an exception he parameterized query expects the parameter which was not supplied.When I leave the textBox blank it fires the parameterized query expects the parameter which was not supplied.
How to change that?Didn't I cover that exception with the SQLException catch?
I need it to fire a message box when it's blank or it's not integers inside the textbox.

Answer

Convert.ToInt32(String.Empty) will raise a FormatException because it cannot parse an empty string to a Int32 Value.

Converting an invalid string to Int32 will raise a FormatException but also a SQLException because the conversion fails and also the parameterized query, because it expects Int32.

You can catch System.Exception and switch on possible errors, instead of trying to catch two exceptions:

catch (Exception ex)            
{                
    if (ex is FormatException)
    {
        if (string.IsNullOrEmpty(this.textboxName.Text){ 
            MessageBox.Show("U must enter something");
        } else {
            MessageBox.Show("It must be an integer");
        }
        return; 
     } 
     else if (ex is SqlException) {
        MessageBox.Show(ex.Message);
        return;
     }
   throw;
}
Comments