title: VB Gridview Populating and Sorting
body: <p>I am having a problem getting my GridView's DataSource worked out.  I have tried multiple variations in coding, each with their own issues.  I have the DataSourceID defined using the GridView Configuration manager.  When I attempt to call my stored procedure to populate my GridView, I get no results.  So, I tried to define a DataSource in the codebehind and manually bind (and set DataSourceID = '' to avoid error).  That populates my GridView very well, but won't allow for sorting.  </p>
  
  <p>Here's my question:  Does anyone see what I am doing wrong so that neither of these solutions works?  I've repurposed code from another project I did, and they are identical (in other project, both DataSourceID and Datasource are used with no error?)  Here's my ASP.net Gridview and Proc code:</p>
  
  <pre><code>    &lt;asp:GridView ID="gvUserSearch" runat="server" style="z-index: 1; left: 56px; top: 382px; position: absolute; height: 133px; width: 187px" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="UserSearchDataSource" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical"&gt;
  &lt;AlternatingRowStyle BackColor="Gainsboro" /&gt;
  &lt;Columns&gt;
      &lt;asp:CommandField ShowSelectButton="True" &gt;
      &lt;ItemStyle ForeColor="#0066FF" /&gt;
      &lt;/asp:CommandField&gt;
      &lt;asp:BoundField DataField="SubscriberID" HeaderText="SubscriberID" SortExpression="SubscriberID" Visible="False" /&gt;
      &lt;asp:BoundField DataField="Last Name" HeaderText="Last Name" SortExpression="Last Name" &gt;
      &lt;HeaderStyle HorizontalAlign="Center" Wrap="False" /&gt;
      &lt;ItemStyle HorizontalAlign="Left" Wrap="False" /&gt;
      &lt;/asp:BoundField&gt;
      &lt;asp:BoundField DataField="First Name" HeaderText="First Name" SortExpression="First Name" &gt;
      &lt;HeaderStyle HorizontalAlign="Center" Wrap="False" /&gt;
      &lt;ItemStyle HorizontalAlign="Left" Wrap="False" /&gt;
      &lt;/asp:BoundField&gt;
      &lt;asp:BoundField DataField="EmailAddress" HeaderText="Email Address" SortExpression="EmailAddress" &gt;
      &lt;HeaderStyle HorizontalAlign="Center" Wrap="False" /&gt;
      &lt;ItemStyle HorizontalAlign="Left" Wrap="False" /&gt;
      &lt;/asp:BoundField&gt;
      &lt;asp:BoundField DataField="locationid" HeaderText="Unit ID" SortExpression="locationid" &gt;
      &lt;HeaderStyle HorizontalAlign="Center" Wrap="False" /&gt;
      &lt;ItemStyle HorizontalAlign="Center" Wrap="False" /&gt;
      &lt;/asp:BoundField&gt;
      &lt;asp:BoundField DataField="unit_name" HeaderText="Unit Name" SortExpression="unit_name" &gt;
      &lt;HeaderStyle HorizontalAlign="Center" Wrap="False" /&gt;
      &lt;ItemStyle HorizontalAlign="Left" Wrap="False" /&gt;
      &lt;/asp:BoundField&gt;
      &lt;asp:BoundField DataField="Birthday" HeaderText="Birthday" ReadOnly="True" SortExpression="Birthday" DataFormatString="{0:MMMM/DD/yyyy}" HtmlEncode="False" HtmlEncodeFormatString="False" &gt;
      &lt;ItemStyle HorizontalAlign="Center" Wrap="False" /&gt;
      &lt;/asp:BoundField&gt;
      &lt;asp:BoundField DataField="Zip Code" HeaderText="Zip Code" SortExpression="Zip Code" &gt;
      &lt;HeaderStyle HorizontalAlign="Center" Wrap="False" /&gt;
      &lt;ItemStyle HorizontalAlign="Center" Wrap="False" /&gt;
      &lt;/asp:BoundField&gt;
      &lt;asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="True" SortExpression="Status" &gt;
      &lt;HeaderStyle HorizontalAlign="Center" Wrap="False" /&gt;
      &lt;ItemStyle HorizontalAlign="Left" Wrap="False" /&gt;
      &lt;/asp:BoundField&gt;
      &lt;asp:BoundField DataField="DateUnsubscribed" HeaderText="Unsubscribe Date" ReadOnly="True" SortExpression="DateUnsubscribed" DataFormatString="{0:MMMM/DD/yyyy}" HtmlEncode="False" &gt;
      &lt;HeaderStyle HorizontalAlign="Center" Wrap="False" /&gt;
      &lt;ItemStyle HorizontalAlign="Center" Wrap="False" /&gt;
      &lt;/asp:BoundField&gt;
  &lt;/Columns&gt;
  
  &lt;asp:SqlDataSource ID="UserSearchDataSource" runat="server" ConnectionString="&lt;%$ ConnectionStrings:CafeWorksConnectionString %&gt;" SelectCommand="MarketingPortal_UserSearchProc" SelectCommandType="StoredProcedure"&gt;
          &lt;SelectParameters&gt;
              &lt;asp:ControlParameter ControlID="txtEmail" Name="Email" PropertyName="Text" Type="String" /&gt;
              &lt;asp:ControlParameter ControlID="txtLastName" Name="LastName" PropertyName="Text" Type="String" /&gt;
              &lt;asp:ControlParameter ControlID="txtFirstName" Name="FirstName" PropertyName="Text" Type="String" /&gt;
          &lt;/SelectParameters&gt;
      &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p>Here is my VB.net codebehind:</p>
  
  <pre><code>Dim CafeWorksConnection As New SqlConnection()
  Dim CafeWorksCS As String
  Dim CafeWorksDA As SqlDataAdapter
  Dim CafeWorksCB As SqlCommandBuilder
  Dim CafeWorksDS As New DataSet
  Dim CafeWorksSqlC As New SqlCommand
  Dim CafeWorksReader As SqlDataReader
  
   Private Sub DBConnect()
      'Get the connection string from the web.config file
      CafeWorksCS = ConfigurationManager.ConnectionStrings("CafeWorksConnectionString").ConnectionString
  
      'Assign the Connection String to the Connection
      CafeWorksConnection.ConnectionString = CafeWorksCS
  
      'Open the database connection
      CafeWorksConnection.Open()
  End Sub
  
  Private Sub Populate_GridView()
      'Make a database connection
      DBConnect()
  
      'Define the type of query being executed (Stored Procedure)
      CafeWorksSqlC.CommandType = CommandType.StoredProcedure
      CafeWorksSqlC.CommandText = "MarketingPortal_UserSearchProc "
  
      'Define the stored procedure parameters
      CafeWorksSqlC.Parameters.AddWithValue("@Email", txtEmail.Text)
      CafeWorksSqlC.Parameters.AddWithValue("@LastName", txtLastName.Text)
      CafeWorksSqlC.Parameters.AddWithValue("@FirstName", txtFirstName.Text)
  
      'Make a connection for the stored procedure to run
      CafeWorksSqlC.Connection = CafeWorksConnection
      'CafeWorksConnection.Open()
  
      'Executes the stored procedure and stores the result set
      CafeWorksReader = CafeWorksSqlC.ExecuteReader()
  
      'You need to bind the data to the GridView
      'Got error that DataSourceID and DataSource can't be defined (DataSourceID define in Gridview ASP.net
      'code and is not giving me a result set for some reason, so I added the DataSource and DataBind
      'gvUserSearch.DataSourceID = ""
      gvUserSearch.DataSource = CafeWorksDS
      gvUserSearch.DataBind()
  
      'Always close the database connection when you are finished
      CafeWorksConnection.Close()
  End Sub
  
  Protected Sub btnNameSearch_Click(sender As Object, e As EventArgs) Handles btnNameSearch.Click
      'Call the Sub Populate_GridView to display results of search
      Populate_GridView()
  
      'Clear out the text boxes - Keeps data from lingering into other searches
      txtEmail.Text = ""
      txtLastName.Text = ""
      txtFirstName.Text = ""
  End Sub
  </code></pre>
  
  <p>I'm still relatively new to this, and my code may not be pretty.  I would appreciate any help that anyone could give me.  Thank you!</p>