Ragwan
Ragwan

Reputation: 90

Unable to cast object of type System.Web.UI.WebControls.Label to type System.IConvertible

This is the gridview code

<asp:GridView ID="gvwSearch" runat="server" 
    AutoGenerateColumns="False" 
    BackColor="White"
    BorderColor="#999999" 
    BorderStyle="Solid" 
    BorderWidth="1px" 
    CellPadding="3" 
    DataSourceID="SearchSqlDataSource"
    ForeColor="Black" 
    GridLines="Vertical" 
    Width="100%" 
    Visible="False" 
    AllowSorting="True"
    AllowPaging="True" 
    OnSelectedIndexChanged="gvwSearch_SelectedIndexChanged" 
    OnPageIndexChanging="gvwSearch_PageIndexChanging">
    <AlternatingRowStyle BackColor="#CCCCCC" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkbtnEdit" runat="server" OnClick="lnkbtnEdit_Click">
                    Edit</asp:LinkButton>
                &nbsp;|
                    <asp:LinkButton ID="nkbtnView" runat="server" OnClick="nkbtnView_Click">
                        View</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Employee Code" SortExpression="Employee Code">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" DataField="EmployeeCode" runat="server" 
                    Text='<%# Bind("[Employee Code]") %>'>
                </asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblCode" DataField="EmployeeCode" runat="server" 
                    Text='<%# Bind("[Employee Code]") %>'>

                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField="Full Name" HeaderText="Full Name" SortExpression="Full Name" />
        <asp:BoundField DataField="CPR" HeaderText="CPR" SortExpression="CPR" />
        <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
        <asp:BoundField DataField="Designation" HeaderText="Designation" SortExpression="Designation" />
        <asp:BoundField DataField="Nationality" HeaderText="Nationality" SortExpression="Nationality" />
        <asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
        <asp:BoundField DataField="Joining Date" HeaderText="Joining Date" SortExpression="Joining Date" />
        <asp:BoundField DataField="Local Phone" HeaderText="Local Phone" SortExpression="Local Phone" />
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
        <asp:BoundField DataField="End of Service" HeaderText="End of Service" ReadOnly="True"
            SortExpression="End of Service" />
    </Columns>
    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="Gray" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="Gray" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>

This is my c# code:

protected void lnkbtnEdit_Click(object sender, EventArgs e)
{
    GridViewRow gridViewRow = (GridViewRow)(sender as Control).Parent.Parent;
    int index = gridViewRow.RowIndex;

    Label Code_Label = (Label)(gvwSearch.Rows[index].FindControl("lblCode"));

    int CodeLabel = Convert.ToInt32(Code_Label);//At thiline error occurs.
    New_Employee Selected_Employee = new New_Employee(CodeLabel);
    Session["Selected_Employee"] = Selected_Employee;
    Response.Redirect("~/Forms/New_Employee/Employees/Edit_Employee.aspx");
}

Yes, I have tried by changing this line

int CodeLabel =Convert.ToInt32(Code_Label);

to

int CodeLabel =Convert.ToInt32(Code_Label.Text);

This removes the error but no data of the employee is carried from this page to Edit_Employee.aspx When the Edit button clicked in the GridView Any help would be appreciated. Thanks in Advance.

Upvotes: 2

Views: 4461

Answers (1)

Keval Gangani
Keval Gangani

Reputation: 1328

I know you want to redirect user to Edit Employee page with Employee Code. I suggest you to use Anchor tag instead of LinkButton. Try below code it is very easy to integrate and It prevent server request and response.

Your Edit Link Button:

<asp:LinkButton ID="lnkbtnEdit" runat="server" OnClick="lnkbtnEdit_Click">
                Edit</asp:LinkButton>

Replace it with this:

<a href='<%# "Forms/New_Employee/Employees/Edit_Employee.aspx?Selected_Employee=" + Eval("[Employee Code]") %>'>Edit</a>

What I did here, I have passed Employee Code in query string instead of Session, you can retrieve query string variable in your Edit_Employee page using below code:

int liEmployeeCode = Convert.ToInt32(Request.QueryString["Selected_Employee"]);

Please let me know if you have any questions.

Upvotes: 1

Related Questions