Why comes out this error Input string was not in a correct format in c#?

Good Morning: Peace with all...

Error search textbox not checked Fromexception, Input string was not in a correct format.

The folio is type numeric, looking in my textbox if it does, but removing everything you type in the textbox marks me that error, could someone tell me a solution???

private void txtBusqueda_TextChanged(object sender, System.EventArgs e){
        if ((txtBusqueda.Text) != null){
            conexSQL.Open();
            SqlCommand cmd = new SqlCommand("select * from _producto where folio=@folio", conexSQL);

            cmd.Parameters.Add("@folio",  SqlDbType.Int).Value = Convert.ToInt32 (txtBusqueda.Text);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read()){
                cmbNom.Text = dr["nombre"].ToString();
                cmbDes.Text = dr["descripcion"].ToString();
            }
            else{

            }
            dr.Close();
            conexSQL.Close();
        }
      }
 0
Author: Juan Pinzón, 2016-09-23

1 answers

It may be that the TextBox is NOT NULL in its content so enter in the if

The right thing would be

if (!string.IsNullOrEmpty(txtBusqueda.Text)){ ...

You could also use

int busqueda = 0;

if(int.TryParse(txtBusqueda.Text, out busqueda)){

    //si ingresa es un valor nro valido

}

Of course you would use the variable busqueda in the query parameter

 1
Author: Leandro Tuttini, 2016-09-23 15:00:58