I have a Form that contains 3 TextBox,a comboBox,and a DatagridView,in which a client tape an int for TextBox1 or a part of a Text in TextBox2 or TextBox3 or for more specification he can choose an item of the comboBox1 the result is displayed in the dataGridView(numeo_cpte,intitulé_cpte).My problem is I get northig of the result in the dataGridview and an erreur of conversion of the int =>the format of entered data is incorrect
this is my code:
private void button5_Click(object sender, EventArgs e)
{
int a =Convert.ToInt32(textBox1.Text); //format of enetered data is incorrect
String b = textBox3.Text;
String c = comboBox1.SelectedItem.ToString();
String d = textBox4.Text;
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
req="select numero_cpte,intitulé_cpte from compte where numero_cpte='"+a+"' OR intitulé_cpte like '%"+b+"%' OR type_cpte='"+c+"' OR index_full_text_cpte like'%"+d+"%';";
SqlCommand sql = new SqlCommand(req,connection);
int o = sql.ExecuteNonQuery();
MessageBox.Show(o + " succès");
dr = new SqlDataAdapter(req, connection);
dr.Fill(ds, "compte");
compteDataGridView.DataSource = ds.Tables["compte"];
connection.Close();
I add the line
int n=0;
int a =int.TryParse(textBox1.Text,out n)?n:0;
and this is what I get,no errors but I didn't get any result displayed in the datagridView:
I made "success" just for test thanks again for your help
This error will occur if the string is not formatted as an integer. For example:
Convert.ToInt32("1") // returns 1
Convert.ToInt32("1.1") // is not in a recognizable format
You should try using a regular expression to apply some formatting first or use a try/catch statement. If you are still having trouble, could you set a debug break point at this position and provide us a copy of the offending string provided by textbox1.Text?
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With