ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Filter ASP.Net GridView with TextBox using SqlDataSource in C# and VB.Net<p>Hi <a class="username" rel="kelsen1989"> kelsen1989</a>,</p> <p>I checked code is working.</p> <p><span style="text-decoration: underline;"><strong>Database</strong></span></p> <p>For this sample I have used of NorthWind database that you can download using the link given below.</p> <p><a href="https://www.aspsnippets.com/Articles/Install-the-Northwind-and-Pubs-Sample-Databases-in-SQL-Server-Express.aspx">Download Northwind Database</a></p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div id=&#34;Div1&#34; runat=&#34;server&#34;&gt; &lt;p&gt; Search Name By &lt;asp:DropDownList ID=&#34;DropDownList1&#34; runat=&#34;server&#34; AutoPostBack=&#34;True&#34;&gt; &lt;asp:ListItem Value=&#34;&#34;&gt;--Select All--&lt;/asp:ListItem&gt; &lt;asp:ListItem Value=&#34;ProjectName&#34;&gt;Contact Name&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; : &lt;asp:TextBox ID=&#34;txtSearch&#34; runat=&#34;server&#34; Width=&#34;200px&#34;&gt;&lt;/asp:TextBox&gt; &amp;nbsp;&lt;asp:Button ID=&#34;btnSearch&#34; runat=&#34;server&#34; Text=&#34;Seach&#34; OnClick=&#34;btnSearch_Click&#34;/&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;GridView1&#34; /&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">SqlDataSource SqlDataSource1 = new SqlDataSource(); protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { SqlDataSource1.ID = &#34;SqlDataSource1&#34;; this.Page.Controls.Add(SqlDataSource1); SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; SqlDataSource1.SelectCommand = &#34;SELECT top 10 ContactName, City, Country from Customers&#34;; GridView1.DataSource = SqlDataSource1; GridView1.DataBind(); } } protected void btnSearch_Click(object sender, System.EventArgs e) { SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; switch (DropDownList1.SelectedValue) { case &#34;ProjectName&#34;: { SqlDataSource1.SelectCommand = &#34;SELECT top 10 ContactName, City, Country from Customers WHERE ContactName like &#39;%&#34; + txtSearch.Text + &#34;%&#39;&#34;; break; } default: { SqlDataSource1.SelectCommand = &#34;SELECT top 10 ContactName, City, Country FROM [Customers] order by ContactName DESC&#34;; break; } } GridView1.DataSource = SqlDataSource1; GridView1.DataBind(); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Private SqlDataSource1 As SqlDataSource = New SqlDataSource() Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load If Not Me.IsPostBack Then SqlDataSource1.ID = &#34;SqlDataSource1&#34; Me.Page.Controls.Add(SqlDataSource1) SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString SqlDataSource1.SelectCommand = &#34;SELECT top 10 ContactName, City, Country from Customers&#34; GridView1.DataSource = SqlDataSource1 GridView1.DataBind() End If End Sub Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Select Case DropDownList1.SelectedValue Case &#34;ProjectName&#34; SqlDataSource1.SelectCommand = &#34;SELECT top 10 ContactName, City, Country from Customers WHERE ContactName like &#39;%&#34; &amp; txtSearch.Text &amp; &#34;%&#39;&#34; Exit Select Case Else SqlDataSource1.SelectCommand = &#34;SELECT top 10 ContactName, City, Country FROM [Customers] order by ContactName DESC&#34; Exit Select End Select GridView1.DataSource = SqlDataSource1 GridView1.DataBind() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/b1QsVu6.gif" alt="" width="279" height="475" /></p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/127306/Filter-ASPNet-GridView-with-TextBox-using-SqlDataSource-in-C-and-VBNet/https://www.aspforums.net:443/Threads/127306/Filter-ASPNet-GridView-with-TextBox-using-SqlDataSource-in-C-and-VBNet/Wed, 31 Jul 2019 06:07:21 GMT