Error: "The conversion of the nvarchar value '92012156148' overflowed an int column"

0

What I am trying to do is to capture in a textbox the identification number of a person, save the value in a variable, look for it in the database and show the name in label.

This is the code:

SqlConnection conexion = new SqlConnection("server=N ; database=CITISALUD ; integrated security = true");
            conexion.Open();
            string cc = TextBox1.Text;
            string cadena = "select Nombre1, Nombre2, Apellido1, Apellido2 from Pacientes where Documento=" + (cc);
            SqlCommand comando = new SqlCommand(cadena, conexion);
            SqlDataReader registro = comando.ExecuteReader();
            if (registro.Read())
            {
                Label1.Text = registro["Nombre1"].ToString();
                Label2.Text = registro["Nombre2"].ToString();
                Label3.Text = registro["Apellido1"].ToString();
                Label4.Text = registro["Apellido2"].ToString();
            }
            else
                Label5.Text = "No Existe";
            conexion.Close();

But I get this error:

Se produjo la excepción System.Data.SqlClient.SqlException.
HResult=0x80131904
Mensaje = The conversion of the nvarchar value ' 92012156148' overflowed an int column.
Origen = .Net SqlClient Data Provider
Seguimiento de la pila:
<No se puede evaluar el seguimiento de la pila de excepciones>

The field in the table is nvarchar type.

Thanks for your help!

  • Now modify the Data Source. ok
  • Change the code as you told me and this error comes out. reviewing try with this other and did not show me any error ¡¡
  •   

    Document = '+ cc' "

    I run the application and it does not show me any error, but it also does not filter me through the document that is what I'm trying to do, it enters the if because I have a message that if it does not find the document, it says it does not exist.


    I hope you can help me and thank you very much for what you have helped me.

        
    asked by Sley 24.07.2017 в 05:14
    source

    1 answer

    3

    The error you have is because you try to assign a value to an integer that is passed from its range.

    Use the bigInt type.

    rango del entero: 4 bytes: -2.147.483.648 a 2.147.483.647
    rango del bigInt: 8 bytes: -2^63 a 2^63-1 
    

    Your number is 92.012.156.148, which is the maximum value that an integer can save and that is why it gives you the error.

    You must change the data type to bigInt. If you think that the values in other columns may exceed the range, change their data type as well.

    Response taken from link proposal by @Mudassir Hasan

        
    answered by 24.07.2017 в 08:09