DataTable must be set prior to using DataView. Am I binding my data the wrong way?
<p>it have been a week since I start with this project but I'm totally clueless on what to fix anymore. down here is part of my coding. </p>

<pre><code>Protected Sub bpn_Click(sender As Object, e As EventArgs)
    Try
      Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("SQLServer2005DBConnectionString").ToString())
        connection.Open()
        Dim sql As String = ("select * from LOT_ WHERE PRODUCTNAME ='" &amp; txtbpn.Text &amp; "'")
        Dim cmd As SqlCommand = New SqlCommand(sql, connection)
        Using reader As SqlDataReader = cmd.ExecuteReader
            If reader.HasRows Then
                lblerror.Visible = False
                connection.Dispose()
                connection.Close()
                Me.BindBpn()

                txtbpn.Text = String.Empty
                TextBox2.Text = String.Empty
                TextBox3.Text = String.Empty
                TextBox4.Text = String.Empty
                TextBox5.Text = String.Empty
            Else
                connection.Dispose()
                connection.Close()

                lblerror.Text = "bpn not found"
                lblerror.Visible = True
                txtbpn.Text = String.Empty
                TextBox2.Text = String.Empty
                TextBox3.Text = String.Empty
                TextBox4.Text = String.Empty
                TextBox5.Text = String.Empty
            End If
        End Using
    Catch ex As Exception
        Response.Write(ex.Message)
        Response.AppendHeader("Refresh", "1;url=Summary.aspx")
    End Try
End Sub
</code></pre>

<p>1.the binding sub</p>

<pre><code>    Private Sub BindBpn()

  Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("SQLServer2005DBConnectionString").ToString())
        Using cmd As New SqlCommand("SELECT * FROM LOT_ WHERE PRODUCTNAME='" &amp; txtbpn.Text &amp; "'order by checkin asc")
            Using sda As New SqlDataAdapter()
                con.Open()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Dim dt As New DataTable()
                sda.Fill(dt)
                Dim dv As New DataView(dt)

                GridView1.DataSource = dv
                GridView1.DataBind()
                ' Me.BindGridView()
                con.Close()

            End Using
        End Using
    End Using
    If GridView1.Visible = False Then
        GridView1.Visible = True
    End If
    If Button1.Visible = False Then
        Button1.Visible = True
    End If
End Sub
</code></pre>

<p>2.the error happen when I'm trying to sort the data.</p>

<pre><code>Protected Sub SortRecords(sender As Object, e As GridViewSortEventArgs)

    Dim SortDir As String = String.Empty
    Dim sortExpression As String = e.SortExpression


    Dim dv As New DataView(GridView1.DataSource) 
    If dv IsNot Nothing Then
        If direction = SortDirection.Ascending Then
            direction = SortDirection.Descending

            SortDir = "Desc"
            ViewState("SortExpression") = Convert.ToString(e.SortExpression &amp; " " &amp; SortDir)
        Else
            direction = SortDirection.Ascending

            SortDir = "Asc"
            ViewState("SortExpression") = Convert.ToString(e.SortExpression &amp; " " &amp; SortDir)

        End If


    End If

    dv.Sort = ViewState("SortExpression").ToString 
    GridView1.DataSource = dv
    GridView1.DataBind()
end sub
</code></pre>

<p>The error happen on this code</p>

<pre><code>dv.Sort = ViewState("SortExpression").ToString 
</code></pre>

<p>Any help would be great. Thanks in advance</p>

<p>this is my aspx GridView:</p>

<pre><code>  &lt;asp:GridView ID="GridView1" width="100%" runat="server" 
              AllowSorting="True" 
              OnSorting="SortRecords"  
              OnRowDataBound="GridView1_RowDataBound"   
              ItemStyle-HorizontalAlign="Center" 
              AutoGenerateColumns="False" &gt;
    &lt;Columns&gt;
      &lt;asp:TemplateField HeaderText="ID"  ItemStyle-HorizontalAlign="Center"&gt;
        &lt;ItemTemplate&gt;
          &lt;%# Container.DataItemIndex + 1 %&gt;
        &lt;/ItemTemplate&gt;
        &lt;ItemStyle HorizontalAlign="center" Width="5%" /&gt;
      &lt;/asp:TemplateField&gt;

      &lt;asp:BoundField DataField="Location" HeaderText="Location" /&gt;
      &lt;asp:BoundField DataField="LOT_ID" HeaderText="Lot ID"  /&gt;
      &lt;asp:BoundField DataField="PkgName" HeaderText="PIN PACKAGE NAME" /&gt;
      &lt;asp:BoundField DataField="MBTBoardNo" HeaderText="MBTBoardNo" SortExpression="MBTBoardNo" /&gt;
      &lt;asp:BoundField DataField="BTProgramName" HeaderText="BT Program Name"  /&gt;
      &lt;asp:BoundField DataField="ProductRank" HeaderText="ProdRank"/&gt;
      &lt;asp:BoundField DataField="BASEPRODUCTNAME" HeaderText="BPN" /&gt;
      &lt;asp:BoundField DataField="MCNo" HeaderText="MC No" /&gt;
      &lt;asp:BoundField DataField="QTY" HeaderText="Qty" /&gt;
      &lt;asp:BoundField DataField="CreateDate" HeaderText="Start Date" /&gt;
      &lt;asp:BoundField DataField="CheckIn" HeaderText="CheckIn" /&gt;
      &lt;asp:BoundField DataField="CheckOut" HeaderText="CheckOut" /&gt;  
    &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
</code></pre>