Why does this error occur? Input string was not in a correct format in c #?

0

Good morning: Peace with everyone ...

  

Error Searching in textbox is not controlled FromException, Input string was   not in a correct format.

the folio is numeric type, when searching in my textbox if it does, but when you remove everything you write in the textbox, you mark 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();
        }
      }
    
asked by AraceLi Torres 23.09.2016 в 16:29
source

1 answer

1

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 parameter of the query

    
answered by 23.09.2016 / 17:00
source