Search This Blog

Friday, 6 November 2015

To show grid view data using sql queries in browser

To show grid view data using sql queries in browser

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=siva-pc;Initial Catalog=master;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "insert into resv1 values('" + TextBox1.Text + "'," + TextBox2.Text + ")";
        cmd.ExecuteNonQuery();
        con.Close();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from resv1", con);
        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        
    }
}



Grid view source
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            style="z-index: 1; left: 210px; top: 222px; position: absolute; height: 133px; width: 187px">
            <Columns>
                <asp:TemplateField HeaderText="name">
                <ItemTemplate>
                            <%#Eval("name")%>
                        </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="phno">
                <ItemTemplate>
                            <%#Eval("phno")%>
                        </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Click:-> edit columns


Add template and write column name:






No comments:

Post a Comment