Skip to main content

How to use QueryExtenderControl in ASP.NET with LINQ


Queryextender control is a control that helps in doing actions like filtration, searching, sorting etc. on the LINQ Data source with few steps. In fact we don’t have to write any code with this. Screenshot given below is just an example that can easily be achieved through this control.

In the above screenshot, we can see that there is a GridView Control that has been binded to a LINQData Source Control. And we have a DropDownlistBox that is also binded to another LINQData Source Control and displaying only grouped jobs. The GridView display all the employees who are matching the Job Parameter. This is done through QueryExtender Control. Also we can search for any name in the employee name's through Query Extender control. The code in the HTML goes like:

<form id="form1" runat="server">
    <div>
        Select Job: <asp:DropDownList ID="Job" runat="server" DataSourceID="LinqDataSource2"
            DataTextField="JOB" DataValueField="JOB" Width="300px" AutoPostBack="True"></asp:DropDownList>

        <asp:LinqDataSource ID="LinqDataSource2" runat="server" ContextTypeName="EmployeeDataContext" EntityTypeName="" GroupBy="JOB" Select="new (key as JOB, it as EMPs)" TableName="EMPs">
        </asp:LinqDataSource>

        Search Name:
        <asp:TextBox ID="TextBox1" runat="server"Width="273px"></asp:TextBox>
        <br />

Now the code in html for GridView and LINQData Source.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="LinqDataSource1" Height="215px" Width="765px" ForeColor="Black" GridLines="Vertical">
            <AlternatingRowStyle BackColor="#CCCCCC" />
            <Columns>
                <asp:BoundField DataField="EMPNO" HeaderText="EMPNO" SortExpression="EMPNO" />
                <asp:BoundField DataField="ENAME" HeaderText="ENAME" SortExpression="ENAME" />
                <asp:BoundField DataField="JOB" HeaderText="JOB" SortExpression="JOB" />
                <asp:BoundField DataField="MGR" HeaderText="MGR" SortExpression="MGR" />
                <asp:BoundField DataField="HIREDATE" HeaderText="HIREDATE" SortExpression="HIREDATE" />
                <asp:BoundField DataField="SAL" HeaderText="SAL" SortExpression="SAL" />
                <asp:BoundField DataField="COMM" HeaderText="COMM" SortExpression="COMM" />
                <asp:BoundField DataField="DEPTNO" HeaderText="DEPTNO" SortExpression="DEPTNO" />
            </Columns>
            <FooterStyle BackColor="#CCCCCC" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#808080" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#383838" />
        </asp:GridView>
   
  <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="EmployeeDataContext"EntityTypeName="" TableName="EMPs">
        </asp:LinqDataSource>


 <asp:QueryExtender ID="QueryExtender1" runat="server" TargetControlID="LinqDataSource1">
          
<asp:PropertyExpression>
                <asp:ControlParameter ControlID="Job" Name="Job" />
            </asp:PropertyExpression>

       <asp:SearchExpression ComparisonType="InvariantCultureIgnoreCase" 
                DataFields="Ename, Job" SearchType="Contains">
                    <asp:ControlParameter ControlID="TextBox1" />
            </asp:SearchExpression>

        </asp:QueryExtender>


There are two filters that we have used in the QueryExtender Control.

a.    PropertyExpression: That just matches with anything but works only for the equality operator.
     <asp:PropertyExpression>
                <asp:ControlParameter ControlID="Job" Name="Job" />
            </asp:PropertyExpression>
The “Name” attribute in the above tag specifies the name of column from the database.

b.    SearchExpression: That searches for the given content irrespective of Lowercase of uppercase and in number of fields

            <asp:SearchExpression ComparisonType="InvariantCultureIgnoreCase" 
                DataFields="Ename, Job" SearchType="Contains">
                    <asp:ControlParameter ControlID="TextBox1" />
           


Comments

Popular posts from this blog

SQLServer Error: 15404, Could not obtain information about Windows NT group/user Error code 0x5. [SQLSTATE 42000] (ConnIsLoginSysAdmin)

If we encounter this error "SQLServer Error: 15404, Could not obtain information about Windows NT group/user Error code 0x5. [SQLSTATE 42000] (ConnIsLoginSysAdmin)" in SQL Server in which the job fails because of the user account related problem, then we need to take the following steps to make it work: a. Go to SQL Server Agent. b. Then select the job which is giving error and not running successfully. c. Then choose properties and in the General Tab, change the owner to "SA" or any account that has the administrative privileges. The problem will be resolved.

How to Delete Record through Knex.

This post is all about of deleting records through KNEX ORM in Nodejs. The following code shows how to achieve this: knex("depts").where("deptno","50").del() .then(function (count) {     console.log(count); }) .finally(function () {     knex.destroy(); }); The above code deletes a record from a table "Depts" where Deptno = 50. It has a "then" Promise attached that will show how many records were deleted with this command. Also "finally" is the method that will always execute and will close the KNEX connection. Happy Coding !!!