ARTICLE

Filter data in VB.NET

Posted by Gurjeet Singh Articles | Visual Basic Language June 22, 2011
FilterExpression property used to filter rows returned by the control.FilterExpression reducing the amount of code needed to build condition and logical filter expression.
Download Files:
 
Reader Level:

SqlDataSource provide FilterExpression property used to filter rows returned by the control. FilterExpression reducing the amount of code needed to build condition and logical filter expression. Many advantage of filter expression is that you can search data from multiple tables. SqlDataSqource control use SelectCommand statement to retrieve data from database.

In Default.aspx, I have used FilterExpression manually in SqlDataSource and in Dynamic.aspx page I dynamically set the FilterExpression and FilterParameters. FilterParameters is read only property. If you click on search button, GridView shows the filter data. Both Default.aspx and Dynamic.aspx return same result.

Default.aspx

<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"    CodeFile="Default.aspx.vb" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</
asp:Content>
<
asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Search" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:Master %>"
    SelectCommand="SELECT [EMP_FNAME], [EMP_LNAME], [EMP_COUNTRY] FROM [EMPLOYEE1]"
    FilterExpression="EMP_FNAME like '{0}%' or EMP_LNAME like '{0}%'">
        <FilterParameters>
            <asp:ControlParameter ControlID="TextBox1" Name="Name" PropertyName="Text" />
        </FilterParameters>
</
asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="EMP_FNAME" HeaderText="EMP_FNAME"
                SortExpression="EMP_FNAME" />
            <asp:BoundField DataField="EMP_LNAME" HeaderText="EMP_LNAME"
                SortExpression="EMP_LNAME" />
            <asp:BoundField DataField="EMP_COUNTRY" HeaderText="EMP_COUNTRY"
                SortExpression="EMP_COUNTRY" />
        </Columns>
</
asp:GridView>
</
asp:Content>

Output:

Untitled-1.gif

Dynamic.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Dynamic.aspx.vb" Inherits="Dynamic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
    <title></title>
</
head>
<
body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Search" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:Master %>"
    SelectCommand="SELECT EMP_FNAME, EMP_LNAME, EMP_COUNTRY FROM EMPLOYEE1" >
    </
asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="EMP_FNAME" HeaderText="EMP_FNAME"
                SortExpression="EMP_FNAME" />
            <asp:BoundField DataField="EMP_LNAME" HeaderText="EMP_LNAME"
                SortExpression="EMP_LNAME" />
            <asp:BoundField DataField="EMP_COUNTRY" HeaderText="EMP_COUNTRY"
                SortExpression="EMP_COUNTRY" />
        </Columns>
</
asp:GridView>
    </div>
    </form>
</
body>
</
html>

Dynamic.aspx.vb

Partial Class Dynamic
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        SqlDataSource1.FilterExpression = "EMP_FNAME like '{0}%' or EMP_LNAME like'{0}%'"
        SqlDataSource1.FilterParameters.Add("EMP_FNAME", TextBox1.Text)
    End Sub
End
Class

Output:

Untitled-1.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor