Question

I set up paging for my gridview and now I get an error saying Guid should contain 32 digits with 4 dashes when I click to another page in the pager. When the friendly error message pops up, I click ok, and the Grid still populates with the next page of data.

Debugging has caught the exception at this method:

protected void cntrlGrdVwDisplayProducts_RowCommand ( object sender , GridViewCommandEventArgs e )
    {
        Guid productID;
        try
        {
            productID = new Guid ( e.CommandArgument.ToString ( ) );
            if ( ( ( e.CommandName != null ) & ( e.CommandArgument != null ) ) )
            {
                switch ( e.CommandName.ToString ( ).ToLower ( ) )
                {
                    case "editproduct":
                        EditProduct ( productID );
                        break;
                    case "deleteproduct":
                        DeleteProduct ( productID );
                        break;
                    case "publishproduct":

                        string currentStatus = ((LinkButton)e.CommandSource).Text.Trim();
                        int newStatus = -1;
                        if ( string.Equals ( currentStatus , "Publish" ) )
                        {
                            PublishProduct ( productID , true );

                        }
                        else if ( string.Equals ( currentStatus , "UnPublish" ) )
                        {
                            PublishProduct ( productID , false );

                        }
                        break;

                }
            }
        }
        catch ( Exception ex )
        {
            Forbin.Logging.Log.LogException ( Forbin.Logging.PageType.ProductManage , Forbin.Logging.MessageType.Exception , ex.ToString ( ) );
            UIUtils.ShowMessageToUser ( "OnErrorMesg" , this.Page );
        }
    }

Here is my paging method just in case:

protected void cntrlGrdVwDisplayProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        cntrlGrdVwDisplayProducts.PageIndex = e.NewPageIndex;
        BindDataToUI();
    }

Gridview:

<asp:GridView ID="cntrlGrdVwDisplayProducts" runat="server" AutoGenerateColumns="False" AllowPaging="true" PageSize="100" PagerSettings-Mode="NextPreviousFirstLast" OnPageIndexChanging="cntrlGrdVwDisplayProducts_PageIndexChanging" OnRowCommand="cntrlGrdVwDisplayProducts_RowCommand" GridLines="None" OnRowDataBound="cntrlGrdVwDisplayProducts_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Product Name">
                    <ItemTemplate>
                        <div class="AdminFontText">
                            <asp:Label ID="grdLblPrdctName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ProductName") %>'>
                            </asp:Label>
                        </div>
                    </ItemTemplate>
                    <HeaderStyle VerticalAlign="Bottom" HorizontalAlign="Left" CssClass="AdminFontHead" />
                    <ItemStyle VerticalAlign="Top" HorizontalAlign="Left" CssClass="AdminFontText" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Item Number">
                    <ItemTemplate>
                        <div class="AdminFontText">
                            <asp:Label ID="grdLblItemNumber" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ItemNumber") %>'>
                            </asp:Label>
                        </div>
                    </ItemTemplate>
                    <HeaderStyle VerticalAlign="Bottom" HorizontalAlign="Left" CssClass="AdminFontHead" />
                    <ItemStyle VerticalAlign="Top" HorizontalAlign="Left" CssClass="AdminFontText" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Description">
                    <ItemTemplate>
                        <div class="AdminFontText">
                            <asp:Label ID="grdLblPrdctDescription" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Description") %>'>
                            </asp:Label>
                        </div>
                    </ItemTemplate>
                    <HeaderStyle VerticalAlign="Bottom" HorizontalAlign="Left" CssClass="AdminFontHead" />
                    <ItemStyle VerticalAlign="Top" HorizontalAlign="Left" CssClass="AdminFontText" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:LinkButton ID="grdBtnPrdctEdit" runat="server" Text="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"ProductID") %>'
                            CommandName="editproduct" CssClass="btn_edit"></asp:LinkButton>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Left" CssClass="btnFontHead" />
                    <ItemStyle Width="90" VerticalAlign="Top" HorizontalAlign="Center" CssClass="btnFontHead" />
                </asp:TemplateField>
           </Columns>
</asp:GridView>

Databinding Method:

private void BindDataToUI ( )
    {
        //btnSaveData.Text = "Save";
        List<MappedCategoryInfo> categoryMappings = BusinessFactory.GetCategoryManager ( ).GetCategoryMappings ( );
        BindCategoryFilters ( cntrlDdlFltrCategory , categoryMappings );
        if ( cntrlDdlFltrCategory.Items.Count > 0 )
        {
            cntrlDdlFltrCategory.SelectedIndex = 0;
            cntrlDdlFltrSubCategory.Enabled = false;
            List<ProductInfo> allProducts = new List<ProductInfo> ( );
            Guid subCategoryID = Guid.Empty;
            switch ( cntrlDdlFltrCategory.SelectedItem.Value )
            {
                case All:
                    allProducts = BusinessFactory.GetProductManager ( ).GetAllProducts ( );
                    cntrlDdlFltrSubCategory.Enabled = false;
                    break;
                default:
                    Guid categoryID = new Guid ( cntrlDdlFltrCategory.SelectedValue );
                    BindSubcategoryFilters ( categoryID , cntrlDdlFltrSubCategory , categoryMappings );
                    if ( cntrlDdlFltrSubCategory.Items.Count > 0 )
                    {
                        cntrlDdlFltrSubCategory.Enabled = true;
                        switch ( cntrlDdlFltrSubCategory.SelectedItem.Value )
                        {
                            case All:
                                foreach ( ListItem subCategoryItem in cntrlDdlFltrSubCategory.Items )
                                {
                                    if ( subCategoryItem.Value != All )
                                    {
                                        subCategoryID = new Guid ( subCategoryItem.Value );
                                        allProducts.AddRange ( BusinessFactory.GetProductManager ( ).GetPublAndUnPublProductBasedOnCategory ( subCategoryID ) );
                                    }
                                }
                                break;
                            default:
                                subCategoryID = new Guid ( cntrlDdlFltrSubCategory.SelectedItem.Value );
                                allProducts.AddRange( BusinessFactory.GetProductManager().GetPublAndUnPublProductBasedOnCategory( subCategoryID ) );
                                break;
                        }
                    }
                    break;
            }
            NumRecords.InnerText = allProducts.Count.ToString();
            cntrlGrdVwDisplayProducts.DataSource = allProducts;
            cntrlGrdVwDisplayProducts.DataBind ( );
        }

    }
Was it helpful?

Solution

Try checking the e.CommandName before this line:

productID = new Guid ( e.CommandArgument.ToString ( ) );

If you are paging or sorting, the ProductID will not be in the e.CommandArgument property.

So try

if (e.CommandName == "Page") { return; }
productID = new Guid ( e.CommandArgument.ToString ( ) );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top