A method name is expected C # Load SQL data

3
using (SqlConnection cn = new SqlConnection ("Data Source = ACER-E5; Initial Catalog = mantenedorclientes; Integrated Security = True")) 
{
    cn.Open ();
    SqlCommand cmd = new SqlCommand ("Select rut,nombre,apellidop,apellidom from clientes where rut ='" + dato + "'");
    SqlDataReader leer = cmd.ExecuteReader ();

    if (leer.Read ()) {
        txt_rut.Text = Convert.ToString (leer ("rut")); //Leer muestra el error "Se espera un nombre de metodo "

    } else { }
    leer.Close ();

}

What I want is to take that data from the query and insert it into several textboxes.

    
asked by Sebastian Ismael 10.10.2018 в 03:03
source

1 answer

5

Your problem occurs because you are using your leer object as if it were a method, but it is not, it is an instance of the class SqlDataReader .

You can see it in the documentation of microsoft of the SqlDataReader class which are the methods and properties that it exposes, for this particular case, we are interested in:

Properties

Methods

Taking into account the aforementioned, we are going to your particular case

To apply the example, I'm going to assume that the column rut is of type Int32 , and the rest chains.

SqlCommand cmd = new SqlCommand("Select rut,nombre,apellidop,apellidom from clientes where rut ='"+dato+"'");
SqlDataReader leer = cmd.ExecuteReader();

if (leer.Read())
{
    //Obtengo el contenido de la primer columna de la query utilizando el método GetInt32(Int32)
    int rut = leer.GetInt32(0);
    //Obtengo el contenido de la segunda columna de la query utilizando el método GetString(Int32)
    string resultado = leer.GetString(1);
    //Obtengo el contenido de la tercer columna de la query utilizando la propiedad Item[String]
    string nombre = leer["nombre"];
}

In case it was not clear, the parameter that is passed to the methods GetString(Int32) and GetInt32(Int32) is the position (starting from zero) of the column that is intended to be obtained within the query

    
answered by 10.10.2018 / 06:07
source