Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I have a db which has EmployeeID,Designation,Basicsallery,coveyanceRate and Housereant field is available. Now within a gridview i have to show designation,EmployeeId and Totalsallery field.To calculate total sallery field i have written a method in the aspx.cs file and called it within the gridview. My code is showing an exception saying "DataBinding: 'System.Data.DataRowView' does not contain a property with the name"

My code in aspx:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewPage.aspx.cs" Inherits="labTest02.ViewPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" align="center" runat="server" ForeColor="#333333" ShowFooter="True" GridLines="None" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID" />
                    <asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName" SortExpression="EmployeeName" />
                    <asp:BoundField DataField="Designation" HeaderText="Designation" SortExpression="Designation" />
                    <asp:TemplateField HeaderText=" Total sallery">
                        <ItemTemplate>
                            <asp:Label runat="server" Text='<%# GetTotalSallery(Convert.ToDouble(Eval("BasicSallery")),Convert.ToDouble(Eval("convRate")),Convert.ToDouble(Eval("houseRent"))) %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>


                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>
            <asp:Label runat="server" Text="Label" ID="lebelforTotal"></asp:Label>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:labtest02_dbConnectionString %>" SelectCommand="SELECT [EmployeeID], [EmployeeName], [Designation] FROM [EmployeeSallery]"></asp:SqlDataSource>
        </div>
    </form>
</body>
</html>




aspx.cs code:
C#
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace labTest02
{
    public partial class ViewPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
       }

        public double GetTotalSallery(double BasicSallery, double convRate, double houseRent)
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["EmployeeConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(connectionString);

            string query = "SELECT BasicSallery, convRate, houseRent FROM EmployeeSallery";

            SqlCommand command = new SqlCommand(query, connection);

            connection.Open();



            SqlDataReader sdr = command.ExecuteReader();

            while (sdr.Read())
            {
                string basic = sdr["BasicSallery"].ToString();
                string conv = (sdr["convRate"].ToString());
                string houserent = (sdr["houseRent"].ToString());


                EmployeeSallery aSallery = new EmployeeSallery();
                aSallery.BasicSallery = Convert.ToDouble(basic);
                aSallery.ConvRate = Convert.ToDouble(conv);
                aSallery.HouseRent = Convert.ToDouble(houserent);
                aSallery.GetTotalSallery();

            }
            sdr.Close();
            connection.Close();
            return BasicSallery + ((BasicSallery * convRate) / 100) + ((BasicSallery * houseRent) / 100);

        }
    }
}



my class file:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace labTest02
{
    public class EmployeeSallery
    {
        public string EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string Designation { get; set; }
        public string Email { get; set; }
        public double BasicSallery { get;  set; }
        public double ConvRate { get;  set; }
        public double HouseRent { get; set; }

        public double GetConveyance()
        {
            return (BasicSallery * ConvRate) / 100;
        }

        public double GetHouseRent()
        {
            return (BasicSallery * HouseRent) / 100;
        }

        public double GetTotalSallery()
        {
            return BasicSallery + GetConveyance() + GetHouseRent();
        }

    }
}



its a vry urgent issue. i have deadline to live upto..Thnx in advance
Posted
Updated 22-Mar-15 13:16pm
v2
Comments
F-ES Sitecore 22-Mar-15 19:04pm    
There are a few things wrong with your code I think, but to address your immediate question, you are doing things like Eval("BasicSallery") when the query the grid is bound to only has EmployeeID, EmployeeName and Designation. Add all the fields you want to use to the SELECT then you might be able to work on any other issue you have, though I'm surprised the code even compiles as your code-behind seems to be referencing non-existing variables.
star_tasneem 23-Mar-15 1:17am    
ur reply can be marked as answer.thnx a millions of ton

Most of your errors are due to spelling and mismatching upper and lower case:

Text='<%# GetTotalSallery(Convert.ToDouble(Eval("BasicSallery")),Convert.ToDouble(Eval("convRate")),Convert.ToDouble(Eval("houseRent"))) %>'


It is important to follow style and naming conventions.
For example: all variables are start with lower case and methods with upper case.

Also, it is not good to call methods inside UI pages, instead use the code behind. That will eliminate errors.

SQL Queries should be either in the Object class or in a controller class, not in the UI.
 
Share this answer
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:labtest02_dbConnectionString %>" SelectCommand="SELECT [EmployeeID], [EmployeeName], [Designation],[BasicSallery],[convRate],[houseRent] FROM [EmployeeSallery]"></asp:SqlDataSource>
 
Share this answer
 

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