Mar 6th, 2009 | No Comments

Today I just got went though this, this helps you to execute a string in SQL. This is how you can execute a string in SQL,

Declare @query Varchar(500)

Set @query = ‘Select * From Employees’

Exec (@query)

OR

EXEC (‘USE AdventureWorks; SELECT EmployeeID, Title FROM HumanResources.Employee;’)

This way you can execute a string in SQL.

This is extremely helpful when you need to add the Where clause based on some criteria. You can have your query in a string and based on that criteria you can append the clause in that string and finally execute the string at the end. This works perfectly.

However this does not work if you want to execute the query and get the result in the Dataset in your .Net application. For that you need to create a #Temp table execute the string and get the result in the #Temp table and then fire the select again on your #Temp table.

Note: Don’t forget to get the brackets ( & ) around your query else SQL will throw an error, Unrecognized stored procedure.

Written by Ajay Matharu

March 6th, 2009 at 2:18 pm

Feb 26th, 2009 | 1 Comment

The GridView control provides you with the option of sorting and paging records without requiring a form-post back to the Web server. In other words, you can re-render the contents of a GridView when sorting and paging, without needing to re-render the entire page.

You enable client paging and sorting by assigning the value true to the EnableSortingAndPagingCallback property. When this property has the value true, the GridView uses JavaScript to request an updated set of records from the Web server.


1 <%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

2

3 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

4 <html>

5 <head id=”Head1″ runat=”server”>

6 <title>Callback GridView</title>

7 </head>

8 <body>

9 <form id=”Form1″ runat=”server”>

10 <%=DateTime.Now %>

11 <asp:GridView ID=”GridView1″ DataSourceID=”TitlesSource” EnableSortingAndPagingCallbacks=”true”

12 AllowPaging=”true” AllowSorting=”true” runat=”Server” />

13 <asp:SqlDataSource ID=”TitlesSource” ConnectionString=”Server=localhost;Database=northwind;Trusted_Connection=true”

14 SelectCommand=”SELECT * FROM customers” runat=”Server” />

15 </form>

16 </body>

17 </html>


Here when you will sort the page or click on the page number the time will not change as it is being set on the page load but the data will get sorted. The time doesn’t change because the page is not reloaded.

Behind the scenes, the GridView uses the Microsoft Internet Explorer XMLHTTPRequest object to communicate with the Web server. This object is supported by Internet Explorer version 5.0 and higher.

However this does not work when you have a templatefield in your Gridview. If you have a templatefield in your Gridview you need to perform the pagination like traditional way handling the events.

Written by Ajay Matharu

February 26th, 2009 at 7:00 pm