title: ASP.NET Gridview Error on Paging
body: <p>I set up paging for my gridview and now I get an error saying <code>Guid should contain 32 digits with 4 dashes</code> 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. </p>
  
  <p>Debugging has caught the exception at this method:</p>
  
  <pre><code>protected void cntrlGrdVwDisplayProducts_RowCommand ( object sender , GridViewCommandEventArgs e )
      {
          Guid productID;
          try
          {
              productID = new Guid ( e.CommandArgument.ToString ( ) );
              if ( ( ( e.CommandName != null ) &amp; ( 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 );
          }
      }
  </code></pre>
  
  <p>Here is my paging method just in case:</p>
  
  <pre><code>protected void cntrlGrdVwDisplayProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
      {
          cntrlGrdVwDisplayProducts.PageIndex = e.NewPageIndex;
          BindDataToUI();
      }
  </code></pre>
  
  <p>Gridview:</p>
  
  <pre><code>&lt;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"&gt;
              &lt;Columns&gt;
                  &lt;asp:TemplateField HeaderText="Product Name"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;div class="AdminFontText"&gt;
                              &lt;asp:Label ID="grdLblPrdctName" runat="server" Text='&lt;%# DataBinder.Eval(Container.DataItem,"ProductName") %&gt;'&gt;
                              &lt;/asp:Label&gt;
                          &lt;/div&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;HeaderStyle VerticalAlign="Bottom" HorizontalAlign="Left" CssClass="AdminFontHead" /&gt;
                      &lt;ItemStyle VerticalAlign="Top" HorizontalAlign="Left" CssClass="AdminFontText" /&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField HeaderText="Item Number"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;div class="AdminFontText"&gt;
                              &lt;asp:Label ID="grdLblItemNumber" runat="server" Text='&lt;%# DataBinder.Eval(Container.DataItem,"ItemNumber") %&gt;'&gt;
                              &lt;/asp:Label&gt;
                          &lt;/div&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;HeaderStyle VerticalAlign="Bottom" HorizontalAlign="Left" CssClass="AdminFontHead" /&gt;
                      &lt;ItemStyle VerticalAlign="Top" HorizontalAlign="Left" CssClass="AdminFontText" /&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField HeaderText="Description"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;div class="AdminFontText"&gt;
                              &lt;asp:Label ID="grdLblPrdctDescription" runat="server" Text='&lt;%# DataBinder.Eval(Container.DataItem,"Description") %&gt;'&gt;
                              &lt;/asp:Label&gt;
                          &lt;/div&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;HeaderStyle VerticalAlign="Bottom" HorizontalAlign="Left" CssClass="AdminFontHead" /&gt;
                      &lt;ItemStyle VerticalAlign="Top" HorizontalAlign="Left" CssClass="AdminFontText" /&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField HeaderText=""&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:LinkButton ID="grdBtnPrdctEdit" runat="server" Text="Edit" CommandArgument='&lt;%# DataBinder.Eval(Container.DataItem,"ProductID") %&gt;'
                              CommandName="editproduct" CssClass="btn_edit"&gt;&lt;/asp:LinkButton&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;HeaderStyle HorizontalAlign="Left" CssClass="btnFontHead" /&gt;
                      &lt;ItemStyle Width="90" VerticalAlign="Top" HorizontalAlign="Center" CssClass="btnFontHead" /&gt;
                  &lt;/asp:TemplateField&gt;
             &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Databinding Method:</p>
  
  <pre><code>private void BindDataToUI ( )
      {
          //btnSaveData.Text = "Save";
          List&lt;MappedCategoryInfo&gt; categoryMappings = BusinessFactory.GetCategoryManager ( ).GetCategoryMappings ( );
          BindCategoryFilters ( cntrlDdlFltrCategory , categoryMappings );
          if ( cntrlDdlFltrCategory.Items.Count &gt; 0 )
          {
              cntrlDdlFltrCategory.SelectedIndex = 0;
              cntrlDdlFltrSubCategory.Enabled = false;
              List&lt;ProductInfo&gt; allProducts = new List&lt;ProductInfo&gt; ( );
              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 &gt; 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 ( );
          }
  
      }
  </code></pre>