Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle empty return from database with gridview?
    primarykey
    data
    text
    <p>I have a gridview that is bound to an ObjectDataSource, which retrieves records from a database to display in the gridview. The procedure for returning the records takes in a search string and displays the relevant results. However when there are no results from the database, I get an empty gridview with the page numbers along the bottom, as if it returned all the records from the database, as shown in the pic below:</p> <p><img src="https://i.stack.imgur.com/jP87l.jpg" alt="enter image description here"></p> <p>I have set both the EmptyDataText and EmptyDataTemplate properties, but they do not show when there are no results.</p> <p>Anyone know what's going on here?</p> <p>Here's the asp for the ObjectDataSource and GridView:</p> <pre><code>&lt;asp:ObjectDataSource ID="ObjectDataSource1" runat="server" EnablePaging="True" SelectCountMethod="GetUsersCount" SelectMethod="GetUsers" SortParameterName="sortColumn" TypeName="WebsiteBuilder.Core.UUser" OnSelecting="ObjectDataSource1_Selecting"&gt; &lt;SelectParameters&gt; &lt;asp:Parameter Name="searchExpression" Type="String" DefaultValue="" /&gt; &lt;/SelectParameters&gt; &lt;/asp:ObjectDataSource&gt; &lt;asp:GridView ID="grdUsers" runat="server" CssClass="grdUsers" AutoGenerateColumns="false" OnDataBound="grdUsers_DataBound" DataSourceID="ObjectDataSource1" AllowPaging="true" AllowSorting="true" OnRowCommand="grdUsers_RowCommand" PageSize="5" EmptyDataText="No Results"&gt; &lt;PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" PageButtonCount="5" Position="Bottom" /&gt; &lt;PagerStyle CssClass="pagination" HorizontalAlign="Center" VerticalAlign="Middle" /&gt; &lt;EmptyDataTemplate&gt;No Results&lt;/EmptyDataTemplate&gt;` </code></pre> <p>Here is the code for the selecting event:</p> <pre><code> protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e) { if (!String.IsNullOrEmpty(this.txtSearchBox.Text)) { e.InputParameters["searchExpression"] = "%" + this.txtSearchBox.Text + "%"; } else return; } </code></pre> <p>And the code for fetching the data:</p> <pre><code> cmd.AddParameter("searchExpression", searchExpression); cmd.AddParameter("sortExpression", sortColumn); cmd.AddParameter("startRowIndex", startRowIndex); cmd.AddParameter("maximumRows", maximumRows); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); DataTable dt = ds.Tables[0]; int i = dt.Rows.Count; return ds.Tables[0]; </code></pre> <p>When I check i in debug the value is 0. My question is why the gridview is not showing the EmptyDataTemplate and why it is still showing multiple page numbers when there are no rows.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload