I get a "input string was not in a correct format" error when using the value from a textBox through a boxing to insert in a mysql database.
using(MySqlConnection conn = new MySqlConnection(Datacon.con))
{
MySqlCommand com = new MySqlCommand(Datacon.query, conn);
f2 f21 = new f2();
com.Parameters.AddWithValue("@Projet", f21.textBox1.Text);
com.Parameters.AddWithValue("@N-facture", int.Parse(f21.textBox2.Text));
com.Parameters.AddWithValue("@Total-HT", float.Parse(f21.textBox3.Text));
com.Parameters.AddWithValue("@Retenue-garantie-10%", float.Parse(f21.textBox4.Text));
com.Parameters.AddWithValue("@Retenue-avance", float.Parse(f21.textBox5.Text));
com.Parameters.AddWithValue("@Montant-HT", float.Parse(f21.textBox6.Text));
com.Parameters.AddWithValue("@TVA", float.Parse(f21.textBox7.Text));
com.Parameters.AddWithValue("@TTC", float.Parse(f21.textBox8.Text));
com.Parameters.AddWithValue("@Payement1", float.Parse(f21.textBox9 . Text));
com.Parameters.AddWithValue("@Date1", Convert.ToDateTime(f21.textBox10.Text));
com.Parameters.AddWithValue("@Payement2", float.Parse(f21.textBox11.Text));
com.Parameters.AddWithValue("@Date2", Convert.ToDateTime(f21.textBox12.Text));
com.Parameters.AddWithValue("@Payement3", float.Parse(f21.textBox13.Text));
com.Parameters.AddWithValue("@Date3", Convert.ToDateTime(f21.textBox14.Text));
com.Parameters.AddWithValue("@Payement4", float.Parse(f21.textBox15.Text));
com.Parameters.AddWithValue("@Date4", Convert.ToDateTime(f21.textBox16.Text));
conn.Open();
com.ExecuteNonQuery();
conn.Close();
}
and this is the line where the exception is thrown:
com.Parameters.AddWithValue("@N-facture", int.Parse(f21.textBox2.Text));
I have tried to solve this with TryParse
and replacing .Text
with the Tostring()
method but neither seem to work