hi friend.......
frist you have to make gridView in the aspx page like this
there are two ways of Gridview for the getting the Data:
(1)only true the property of gridView for the column which is AutoGenerateColumns
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
style="margin-top: 0px" Width="529px">
</asp:GridView>
(2)make Column inyour way like this but set the AutoGenerateColumns="False"...
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" Style="margin-top: 0px"
Width="529px" EmptyDataText="No data availble..">
<Columns>
<asp:BoundField DataField="EmpName" HeaderText="Employee Name" ReadOnly="true" />
<asp:BoundField DataField="DepartmentName" HeaderText="Department Name" ReadOnly="true" />
<asp:BoundField DataField="Salary" HeaderText="Salary" ReadOnly="true" />
<asp:BoundField DataField="DeductionAmount" HeaderText="Deduction Amount" ReadOnly="true" />
<asp:BoundField DataField="NetSalary" HeaderText="Net Salary" ReadOnly="true" />
</Columns>
</asp:GridView>
now go in the aspx.cs page
make one method like this..
public void GetEmpDetails()
{
try
{
SqlConnection con = new SqlConnection("Data Source=RUDRA-PC\\SQLEXPRESS;Initial Catalog=Testing Database;Integrated Security=True");
con.Open();
string strQuery = "SELECT EmpName,DepartmentName,Salary,DeductionAmount,(Salary-DeductionAmount)AS NetSalary FROM Employee INNER JOIN Department ON Employee.DepId = Department.DepId ";
SqlCommand cmd = new SqlCommand(strQuery, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
Gridview1.DataSource = dt;
Gridview1.DataBind();
con.Close();
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(GetType(), "fnCall", "<script language='javascript'>alert('" + ex.Message.ToString() + "');</script>");
}
}
and call this Method in the PageLoad like this
protected void Page_Load(object sender, EventArgs e)
{
GetEmpDetails();
}
also add three namespace in the aspx.cs page...
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
Hope Fully, now you get the data in the grid view...
Best Of Luck....