Click here to Skip to main content
16,018,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Respected Sir,
I am using the two sql table like Employee & Department.
1)In Employee table fields are Empid,Empname,Deptid,Salary etc.
2)In Department table fields are Deptid,Departmentname,Deduction_amount etc.
3)We insert record manually in department table.
4)I am create the web page "employeedetails.aspx"
& write the code for insert record in employee table.
5)I am displaying all above field of table employee and department in grid view column then
I want to add one another column to gridview for netsalary calculating from salary of employee table subtract deduction_amount from department table.

I am taken the gridview field like
Empname,Departmentname,Salary,Deduction_amount,Net Salary(Salary-deduction_amount)

Please given me the proper solution.

Thanks,
Posted
Comments
Tom Marvolo Riddle 29-Oct-13 9:12am    
netsalary for all employees?

1 solution

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...
C#
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....
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900