Output:
Aspx Page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="VP_Industries.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
width:300px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvdata" runat="server" CssClass="Gridview" ShowHeaderWhenEmpty="true"AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField DataField="username" HeaderText="username"/>
<asp:BoundField DataField ="password" HeaderText="password" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Aspx.cs Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace VP_Industries
{
public partial class WebForm2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGridviewData();
}
}
protected void BindGridviewData()
{
da = new SqlDataAdapter("select * from login where id=1",con);
dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
gvdata.DataSource = dt;
gvdata.DataBind();
}
else
{
gvdata.DataSource = "";
gvdata.DataBind();
}
}
}
}
No comments:
Post a Comment