Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incorrect format for an int

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:

enter image description here

I made "success" just for test thanks again for your help

like image 970
Lina Avatar asked Apr 24 '26 20:04

Lina


1 Answers

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!

like image 191
Porlune Avatar answered Apr 26 '26 09:04

Porlune