views:

36

answers:

1

This is my aspx:

<asp:UpdatePanel ID="resultPanel" runat="server" UpdateMode="Conditional">
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="AddDocument" />
</Triggers>
<ContentTemplate>
    <asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" EnableSortingAndPagingCallbacks="True"
        AllowPaging="True" DataSourceID="FilesObjectDataSource" PageSize="5" OnRowCommand="gridView_RowCommand"
        DataKeyNames="FileGuid" HorizontalAlign="Left" Width="100%" BorderStyle="Solid"
        BorderColor="Black">
        <Columns>
            <asp:BoundField DataField="RID" HeaderText="ID" ReadOnly="True"></asp:BoundField>
            <asp:BoundField DataField="Category" HeaderText="SubCategory" ReadOnly="True">
            </asp:BoundField>
            <asp:BoundField DataField="FileTypeName" HeaderText="Type" ReadOnly="True">
            </asp:BoundField>
            <asp:BoundField DataField="FileGUID" Visible="false" />
            <asp:ButtonField Text="X" ButtonType="Button" ItemStyle-Width="20px" CommandName="DelFile">
                <ItemStyle Width="20px" />
            </asp:ButtonField>
        </Columns>
        <RowStyle CssClass="RowStyle" />
        <EmptyDataRowStyle CssClass="EmptyRowStyle" />
        <PagerStyle CssClass="PagerStyle" />
        <SelectedRowStyle CssClass="SelectedRowStyle" />
        <HeaderStyle CssClass="HeaderStyle" />
        <EditRowStyle CssClass="EditRowStyle" />
        <AlternatingRowStyle CssClass="AltRowStyle" />
    </asp:GridView>
</ContentTemplate>

This is my code behind for the ButtonField marked 'X'

protected void gridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "DelFile")
    {
        //Selected Row
        int rowIndex = Convert.ToInt32(e.CommandArgument);

        fileGuid = new Guid(gridView.DataKeys[rowIndex].Values["FileGuid"].ToString());

    }
}

Sometimes itt results in object ref not set and at times it works. Not sure why this happens in production and not in dev and testing.

A: 

I'm not sure if it makes any difference, but but the case in ("FileGuid")

DataKeyNames="FileGuid"

and

fileGuid = new Guid(gridView.DataKeys[rowIndex].Values["FileGuid"].ToString());

doesn't match ("FileGUID")

<asp:BoundField DataField="FileGUID" Visible="false" />
roman m