1

I have a Grid and it is displaying users this is working. You can click on Edit (Image Button) or on a Delete (Image Button) to do the action . But if I click on on of these I get a

Input string was not in a correct format

This should not happen -- I don't see my error. What is the problem?

Here is my grid:

<asp:EntityDataSource ID="EntityDataSource1" runat="server"
    ConnectionString="name=HolidayTrackerEntities"
    DefaultContainerName="HolidayTrackerEntities" EnableFlattening="False"
    EntitySetName="HtUsers">
</asp:EntityDataSource>
<telerik:RadGrid ID="rgGrid" runat="server" DataSourceID="EntityDataSource1"
    AllowSorting="True" AllowPaging="True" PageSize="20" 
    AllowFilteringByColumn="True" ShowStatusBar="True" Width="100%" 
    CellSpacing="0" GridLines="None" OnItemCommand="rgGrid_ItemCommand">
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="UserId">
        <NoRecordsTemplate>
            Can't find Users to display
        </NoRecordsTemplate>
        <Columns>
            <telerik:GridBoundColumn DataField="UserId" DataType="System.Int32"
                FilterControlAltText="Filter UserId column" HeaderText="UserId" 
                ReadOnly="True" SortExpression="UserId" UniqueName="UserId"
                Visible="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="FirstName"
                FilterControlAltText="Filter FirstName column"
                HeaderText="FirstName" ItemStyle-Width="60px"
                SortExpression="FirstName" UniqueName="FirstName">
                <ItemStyle Width="60px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="LastName"
                FilterControlAltText="Filter LastName column"
                HeaderText="LastName" ItemStyle-Width="60px" 
                SortExpression="LastName" UniqueName="LastName">
                <ItemStyle Width="60px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="UserName"
                FilterControlAltText="Filter UserName column"
                HeaderText="UserName" ItemStyle-Width="60px"
                SortExpression="UserName" UniqueName="UserName">
                <ItemStyle Width="60px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Email"
                FilterControlAltText="Filter Email column"
                HeaderText="Email" SortExpression="Email" UniqueName="Email"
                Visible="False">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn UniqueName="DeleteColumn" 
                ItemStyle-HorizontalAlign="Right" ItemStyle-Width="20px"
                AllowFiltering="false">
                <ItemTemplate>
                    <telerik:RadButton ID="btnEdit" CommandName="Edit" runat="server" Width="20px" ToolTip="View Details" Height="20px">
                        <Image ImageUrl="~/Resources/Images/Grid/edit-app.png" IsBackgroundImage="true" />
                    </telerik:RadButton>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn UniqueName="Delete"
                ItemStyle-HorizontalAlign="Right" ItemStyle-Width="20px"
                AllowFiltering="false" FilterImageUrl="../Image/Filter.gif">
                <ItemTemplate>
                    <telerik:RadButton ID="btnDelete" CommandName="Delete" runat="server" Width="20px" ToolTip="Delte User" Height="20px">
                        <Image ImageUrl="~/Resources/Images/Grid/delete-app.png" IsBackgroundImage="true" />
                    </telerik:RadButton>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

As I said nothing special. Here are the buttons in code behind:

protected void rgGrid_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName.Equals("Edit"))
    {
        int id = int.Parse(e.Item.Cells[2].Text);
        Response.Redirect("~/Administrator/UserEditPanel.aspx?userId=" + id);
    }
    else if (e.CommandName.Equals("Delete"))
    {
        int id = int.Parse(e.Item.Cells[2].Text);
        HtUser.DeleteUserById(id);
    }
}

My Delete Function in the User class

public static void DeleteUserById(int id)
{
    HolidayTrackerEntities ctx = HtEntityFactory.Context;
    HtUser userToDelete = ctx.HtUsers.Where(user => user.UserId == id).FirstOrDefault();

    //Get all the userroles where user is containing
    IEnumerable<HtUserRole> roles = ctx.HtUserRoles.Where(ur => ur.HtUsers.Where(x => x.UserId == id).Any());
    foreach (HtUserRole role in roles)
    {
        //Remove reference from htuserroles table
        role.HtUsers.Remove(userToDelete);
    }

    ctx.HtUsers.DeleteObject(userToDelete);
    ctx.SaveChanges();
}
1
  • 1
    Are you sure that e.Item.Cells[2].Text is numeric (integer)? The second column is LastName Commented Mar 27, 2013 at 11:11

2 Answers 2

2

Rather than parsing column text, a better way is to assign the UserId to the Edit and Delete buttons' CommandArgument property, then parse that in rgGrid_ItemCommand. To assign UserId to the buttons:

<telerik:GridTemplateColumn UniqueName="DeleteColumn" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="20px" AllowFiltering="false">
    <ItemTemplate>
        <telerik:RadButton ID="btnEdit" CommandName="Edit" runat="server" Width="20px" ToolTip="View Details"
       Height="20px" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>'>
            <Image ImageUrl="~/Resources/Images/Grid/edit-app.png" IsBackgroundImage="true" />
        </telerik:RadButton>
    </ItemTemplate>
</telerik:GridTemplateColumn>

then in in your code-behind:

protected void rgGrid_ItemCommand(object sender, GridCommandEventArgs e)
{
    var userId = int.Parse(e.CommandArgument.ToString());

    if (e.CommandName.Equals("Edit"))
    {
        Response.Redirect("~/Administrator/UserEditPanel.aspx?userId=" + userId);
    }
    else if (e.CommandName.Equals("Delete"))
    {
        HtUser.DeleteUserById(userId);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks allot for your help perfect solution & it works !!!! I hope you got more up votes cause your solution is working !!!
2

e.Item.Cells[2] is the cell with DataField="LastName". So i assume that this is not a valid integer. You are storing the id in the first column, therefore you need:

protected void rgGrid_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName.Equals("Edit"))
    {
        int id = int.Parse(e.Item.Cells[0].Text);
        Response.Redirect("~/Administrator/UserEditPanel.aspx?userId=" + id);
    }
    else if (e.CommandName.Equals("Delete"))
    {
        int id = int.Parse(e.Item.Cells[0].Text);
        HtUser.DeleteUserById(id);
    }
}

1 Comment

@Mingebag: I suggest to use the debugger. Set a breakpoint in rgGrid_ItemCommand and look at the value of e.Item.Cells[0].Text. I'm not familiar with telerik-GridBoundColumns, maybe you need to use FindControl instead of Cell.Text or use GetDataKeyValue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.