Thursday, January 21, 2010

Paging with Rownumber

DECLARE @PageNum AS INT;
DECLARE @PageSize AS INT;
SET @PageNum = 2;
SET @PageSize = 10;

WITH OrdersRN AS
(
SELECT ROW_NUMBER() OVER(ORDER BY OrderDate, OrderID) AS RowNum
,OrderID
,OrderDate
,CustomerID
,EmployeeID
FROM dbo.Orders
)

SELECT *
FROM OrdersRN
WHERE RowNum BETWEEN (@PageNum - 1) * @PageSize + 1
AND @PageNum * @PageSize
ORDER BY OrderDate
,OrderID;

Monday, January 11, 2010

Export to Excel in ASP.Net

Random rnd = new Random();
string filename = "SummaryReport" + rnd.Next(9999999).ToString() + ".xls";
HtmlForm form = new HtmlForm();
string attachment = "attachment;filename=" + filename;

//------------ Export to Excel from datagrid -----------------------------
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);

grdSearchResult.Columns[13].Visible = false;
form.Controls.Add(grdSearchResult);
this.Controls.Add(form);
form.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();

//----------------------------End---------------------------------------------

//---------------Export to excel from datatable ----------------------------

DataTable dt = dtAgentSearch;
//string attachment = "attachment; filename=Employee.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dc in dt.Columns)
{
Response.Write(tab + dc.ColumnName);
tab = "\t";
}
Response.Write("\n");

int i;
foreach (DataRow dr in dt.Rows)
{
tab = "";
for (i = 0; i <>
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();

//-----------------------------End---------------------------------------------