0

Please help as i am new in c#

i have facing the below error

"DataBinding: 'Sherserve.CustomTypeLayer.EmployeeLeave' does not contain a property with the name 'empId' "

here is my code :

protected void Button1_Click(object sender, EventArgs e)
{
    LMSManager leavestatus = new LMSManager();
    int emplid = Convert.ToInt32(Session["EmployeeID"]);
    List<EmployeeLeave> leaverecord = leavestatus.GetLeaveRecord(emplid);

    GridView1.DataSource = leaverecord;
    GridView1.DataBind();
}

here is EmployeeLeave :

public class EmployeeLeave
{
    public LeaveReason LeaveType { get; set; }
    public int EmployeeId { get; set; }
    public DateTime DateFrom { get; set; }
    public DateTime DateTo { get; set; }
    public string Reason { get; set; }
}

here is aspx :

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True"
  PageSize="3" OnPageIndexChanging="GridView1_PageIndexchanging" CellPadding="4"
  ForeColor="#333333" Height="16px" 
  Style="margin-bottom: 0px; margin-right: 0px;">
  <%-- DataSourceID="SqlDataSource1"--%>
    <AlternatingRowStyle BackColor="White" />
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />

    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblEmpId" runat="server" Text='<%#Eval("empId")%>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblLeaveType" runat="server" Text='<%#Eval("LeaveType") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblempid" runat="server" Text='<%#Eval("EmployeeId") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblDateFrm" runat="server" Text='<%#Eval("DateFrom") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblDateTo" runat="server" Text='<%#Eval("DateTo") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblRsn" runat="server" Text='<%#Eval("Reason") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
Ammar Asjad
  • 2,920
  • 6
  • 29
  • 42
  • Is this WinForms or WPF? – Daniel Hilgarth Aug 31 '12 at 10:36
  • Your question can't be answered as you haven't shown us `EmployeeLeave` or the data binding... – Daniel Hilgarth Aug 31 '12 at 10:37
  • I assume that this is ASP.NET, show us the aspx markup of the grid as well. – Tim Schmelter Aug 31 '12 at 10:37
  • What about your [previous question](http://stackoverflow.com/questions/12209819/int-does-not-contain-a-definition-and-no-extension-method/12209850#12209850) its the same problem, you should resolve it first rather than asking question without complete information – Habib Aug 31 '12 at 10:38
  • You are missing a basic thing, you need to get an object of `EmployeeLeave` `with emplid` in your method `GetLeaveRecord` – Habib Aug 31 '12 at 10:41

1 Answers1

2

It looks like your databinding is referring to empId while the field on EmployeeLeave is actually called EmployeeId.

Try changing

<ItemTemplate>
    <asp:Label ID="lblEmpId" runat="server" Text='<%#Eval("empId")%>'></asp:Label>
</ItemTemplate>

to

<ItemTemplate>
    <asp:Label ID="lblEmpId" runat="server" Text='<%#Eval("EmployeeId")%>'></asp:Label>
</ItemTemplate>
Pieter Müller
  • 4,573
  • 6
  • 38
  • 54
  • 1
    No i am getting empid from session. – Ammar Asjad Aug 31 '12 at 10:43
  • Each row on the `DataGrid` binds to an instance of `EmployeeLeave`, which does not have the `empId` property. Are you trying to bind to the `int emplid = Convert.ToInt32(Session["EmployeeID"]);` value from your first code block? – Pieter Müller Aug 31 '12 at 10:53