1. Create a new webpage on your project.
Replace this code from the form tag.
use this code on your .aspx file
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
</div>
</form>
------------------------------------------------------------------------------------------------------------
Now on code file (.cs)
------------------------------------------------------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("data source=.; database=a; integrated security=SSPI");
SqlCommand cmd = new SqlCommand("select * from demo", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
con.Close();
}
}
here in sqlconnection
1. data source =.;(. for local system)
2. database (name of the database)
3. and integrated security(for windows authentication)
Now create a new database from SQL server by just Right Clik on Database folder and provide the name of the database.
After that create table use these below code:
------------------------------------------------------
create table demo
(
name nvarchar(200),
rollno int
)
insert into demo values('daljit',12345)
insert into demo values('gurpreet',67890)
After inserting data run the webpage
On running it shows data in grid view as like that:
No comments:
Post a Comment