How to display the data of a query in a gridview

0

I'm doing a project and in a part of the project I need to see a gridview and select what I'm going to do. I already got him to show me the complete table.

protected void buscar_Click(object sender, EventArgs e)
{
    string s = System.Configuration.ConfigurationManager.ConnectionStrings["cadenaconexion1"].ConnectionString;
    SqlConnection conexion = new SqlConnection(s);
    conexion.Open();
    SqlCommand comando = new SqlCommand("select * from producto" +
                       " where id ='" + buscar.Text + "'", conexion);
    SqlDataReader registro = comando.ExecuteReader();

    if (buscar.Text == "")
    // ERROR TEXTBOX EMPTY
    {
        msg.Text = "Debe ingresar ID de producto para poder hacer una busqueda";
        conexion.Close();

    }
    else if (registro.Read())
    {
        //AQUI USO CADA TEXTBOX PARA IMPRIMIR CADA DATO INDEPENDIENTE EN OTRAS
        //CONSULTAS, PERO NECESITO IMPRIMIR LA CONSULTA EN EL GRIDVIEW PERO NO
        //HE PODIDO ENCONTRAR LA RESPUESTA.

        //this.ccliente.Text = "" + registro["cedula"];
        //this.ncliente.Text = "" + registro["nombre"];
        //this.ctelefono.Text = "" + registro["telefono"];
        //this.cdireccion.Text = "" + registro["direccion"];

        conexion.Close();
    }
    else
    {
        msg.Text = "El producto no existe";
        conexion.Close();
    }
    conexion.Close();
}

Here I have the html part

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
                <asp:BoundField DataField="nombre" HeaderText="nombre" SortExpression="nombre" />
                <asp:BoundField DataField="precio" HeaderText="precio" SortExpression="precio" />
                <asp:BoundField DataField="stock" HeaderText="stock" SortExpression="stock" />
                <asp:BoundField DataField="categoria" HeaderText="categoria" SortExpression="categoria" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:finalConnectionString %>" SelectCommand="SELECT * FROM [producto]"></asp:SqlDataSource>
    
asked by Edward Sc 09.11.2018 в 05:25
source

1 answer

0

Analyze this code

using System.Configuration; //define el using

protected void buscar_Click(object sender, EventArgs e)
{
    if (buscar.Text == "")
    {
        msg.Text = "Debe ingresar ID de producto para poder hacer una busqueda";
        return;
    }

    string s = ConfigurationManager.ConnectionStrings["cadenaconexion1"].ConnectionString;
    using(SqlConnection conexion = new SqlConnection(s))
    {
        conexion.Open();
        string query = @"select * from producto
                           where id = @id";
        SqlCommand comando = new SqlCommand(query, conexion);
        comando.Parameters.AddWithValue("@id",  buscar.Text);


        SqlDataAdapter da = new SqlDataAdapter(comando);
        DataTable dt = new DataTable();
        da.Fill(dt);

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

adapt it to load the data in the gridview, advice always use parameters

    
answered by 09.11.2018 в 11:53