Monday, March 16, 2009

How to Sort or handle Paging in GridView Manually without databinding

The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled.
The GridView 'GridViewID' fired event Sorting which wasn't handled.

private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;

switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;

case SortDirection.Descending:
newSortDirection = "DESC";
break;
}

return newSortDirection;
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
GridViewSortExpression = e.SortExpression;
int pageIndex = this.GridView1.PageIndex;
this.GridView1.DataSource = SortDataTable(GridView1.DataSource as DataTable, false);
this.GridView1.DataBind();
this.GridView1.PageIndex = pageIndex;

}

private string GridViewSortDirection
{
get
{
return ViewState["SortDirection"] as string ?? "ASC";
}

set
{
ViewState["SortDirection"] = value;
}
}



private string GridViewSortExpression
{
get
{
return ViewState["SortExpression"] as string ?? string.Empty;
}
set
{
ViewState["SortExpression"] = value;
}
}



private string GetSortDirection()
{
switch (GridViewSortDirection)
{
case "ASC":
GridViewSortDirection = "DESC";
break;
case "DESC":
GridViewSortDirection = "ASC";
break;
}
return GridViewSortDirection;
}



protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.DataSource = SortDataTable(this.GridView1.DataSource as DataTable, true);
this.GridView1.PageIndex = e.NewPageIndex;
this.GridView1.DataBind();
}

protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
{
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
if (GridViewSortExpression != string.Empty)
{
if (isPageIndexChanging)
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);
}
else
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());
}
}
return dataView;
}

else
{
return new DataView();
}
}

How to have Display Number in GridView

< asp:TemplateField >
< ItemTemplate >
< %# Container.DataItemIndex + 1 % >
< /ItemTemplate >
< /asp:TemplateField >

Tuesday, March 10, 2009

Unable to make the session state request to the session state server. Please ensure that the ASP.NET State service is started..

The error that you are getting is because your StateServer is not enabled. You have to go to AdministrativeTools > Computer Management > Services and Applications >Services and start the ASP.NET State Service

The SQL Server Service Broker for the current database is not enabled, and as a result query notifications are not supported...

This problem frequently occurs when you just restored a database, but service brokers are broken or lost.

To resolve this problem.

alter database xxxx set NEW_BROKER

Alternatively,

1. Go to Programs -> Microsoft Sql Server 2005 -> Configuration Tools -> Sql Server Surface Area Configuration -> Surface Area Configuration for Features.
2. Locate "Service Broker" under the database engine and enable it.

Sunday, March 08, 2009