title: VB.NET Error "Cannot find Column" upon GridView sorting
body: <p>Currently I am working on VB.NET code to implement GridView sorting for one of the columns (State). The page has two buttons - 'Add' and 'Update'. Both buttons give different views but same dataset and some of the columns being similar (including the sorting column), each of which are represented in MultiView. One of the views is actually derived from a UserControl.</p>
  <p>I am using the same code for sorting functionality for both the views, and while it works perfectly fine for Update, it is throwing me an error stating &quot;Cannot find Column State&quot; upon clicking on Add button. Please provide assitance as I have thoroughly checked the stored procedure as well as the code and the sort column is present in the procedure query. I am really not sure why the sorting works for one button click and why it doesn't for another.</p>
  <p>Also below is the  code that I am using.</p>
  <p><strong>Page1.aspx</strong>
  Registering UserControl,</p>
  <pre><code>&lt;%@ Register Src=&quot;../UserControls/AddUserControl.ascx&quot; TagName=&quot;addUserControl&quot; TagPrefix=&quot;uc1&quot; %&gt;
  </code></pre>
  <p><strong>MultiView:-</strong></p>
  <pre><code>&lt;asp:MultiView ID=&quot;mv1&quot; runat=&quot;server&quot;&gt;
  &lt;asp:View ID=&quot;vAdd&quot; runat=&quot;server&quot;&gt;
  &lt;uc1:addUserControl id=&quot;Add1&quot; parentnumbercontrol=&quot;holdingNumber&quot; submitcontrol=&quot;ButtonAdd&quot; runat=&quot;server&quot; /&gt;
  &lt;/asp:View&gt;
  &lt;asp:View ID=&quot;vUpdate&quot; runat=&quot;server&quot;&gt;
  &lt;asp:GridView HeaderStyle-VerticalAlign=&quot;Top&quot; AllowSorting=&quot;true&quot; OnSorting=&quot;gvUpdate_Sorting&quot; ID=&quot;gvUpdate&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot;&gt;
  &lt;asp:TemplateField&gt;
  &lt;HeaderTemplate&gt;User Code&lt;br /&gt;&lt;/HeaderTemplate&gt;
  &lt;ItemTemplate&gt;&lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text='&lt;%# Bind(&quot;UserCode&quot;) %&gt;'&gt;&lt;/asp:Label&gt;&lt;/ItemTemplate&gt;
  &lt;EditItemTemplate&gt;&lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; Text='&lt;%# Bind(&quot;UserCode&quot;) %&gt;'&gt;&lt;/asp:TextBox&gt;&lt;/EditItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  &lt;asp:BoundField DataField=&quot;Address&quot; HeaderText=&quot;Address&quot; /&gt;
  &lt;asp:BoundField DataField=&quot;StateCode&quot; HeaderText=&quot;State&quot; SortExpression=&quot;StateCode&quot; /&gt;
  &lt;/asp:GridView&gt;
  &lt;/asp:MultiView&gt;
  </code></pre>
  <p><strong>AddUserControl.ascx</strong>
  Adding GridView in User Control,</p>
  <pre><code>&lt;asp:GridView HeaderStyle-VerticalAlign=&quot;Top&quot; AllowSorting=&quot;true&quot; OnSorting=&quot;gvAdd_Sorting&quot; ID=&quot;gvAdd&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot;&gt;
  &lt;asp:TemplateField&gt;
  &lt;HeaderTemplate&gt;User Code&lt;br /&gt;&lt;/HeaderTemplate&gt;
  &lt;ItemTemplate&gt;&lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text='&lt;%# Bind(&quot;UserCode&quot;) %&gt;'&gt;&lt;/asp:Label&gt;&lt;/ItemTemplate&gt;
  &lt;EditItemTemplate&gt;&lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; Text='&lt;%# Bind(&quot;UserCode&quot;) %&gt;'&gt;&lt;/asp:TextBox&gt;&lt;/EditItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  &lt;asp:BoundField DataField=&quot;Address&quot; HeaderText=&quot;Address&quot; /&gt;
  &lt;asp:BoundField DataField=&quot;StateCode&quot; HeaderText=&quot;State&quot; SortExpression=&quot;StateCode&quot; /&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  <p><strong>Sorting Code in Page1.aspx.vb:-</strong></p>
  <pre><code>Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
          If Not Page.IsPostBack Then
             Me.BindingGrid()
          End If
      End Sub
  
  Private Property SortingDirection As String
          Get
              Return IIf(ViewState(&quot;SortDirection&quot;) IsNot Nothing, Convert.ToString(ViewState(&quot;SortDirection&quot;)), &quot;ASC&quot;)
          End Get
          Set(value As String)
              ViewState(&quot;SortDirection&quot;) = value
          End Set
  End Property
  
  Private Sub BindingGrid(Optional ByVal sortExpression As String = Nothing)
          Dim dt As DataTable = DataCollection.GetDetails(empID.Text)
          If (Not (sortExpression) Is Nothing) Then
              Dim dv As DataView = dt.AsDataView
              Me.SortingDirection = IIf(Me.SortingDirection = &quot;ASC&quot;, &quot;DESC&quot;, &quot;ASC&quot;)
              dv.Sort = sortExpression &amp; &quot; &quot; &amp; Me.SortingDirection     //It throws &quot;Cannot find Column State&quot; error on this line
  
              gvUpdate.DataSource = dv
          Else
              gvUpdate.DataSource = dt
          End If
          gvUpdate.DataBind()
  End Sub
  
  Protected Sub gvAlarmCodes_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
          Me.BindingGrid(e.SortExpression)
  End Sub
  </code></pre>
  <p>The code in AddUserControl.ascx.vb is same as above except instead of gvUpdate, the gridview ID is gvAdd.</p>
  <p><strong>Page Load in AddUserControl.ascx.vb</strong></p>
  <pre><code>Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  If False Or (Not Page.IsPostBack) Then
              Me.BindingGrid()
          End If
  End Sub
  </code></pre>
  <p>While the sorting code works perfectly fine in .aspx.vb file, it throws error  stating &quot;Cannot find Column State&quot; when trying to sort State column from the UserControl Gridview (retrieved after clicking on &quot;Add&quot; button)</p>