Friday, April 23, 2010

Handle Custom Error

Page Level

User errorPage attribute from the form.

Example:-

<%@ Page language="c#" Codebehind="ProductPnl.aspx.cs" 
    AutoEventWireup="false" Inherits=" ProductPnl " 
    errorPage="ErrorPages/PageError.aspx"%>
 
 Web.Config   


 Application_Error procedure in Global.asax
 
 
void Application_Error(object sender, EventArgs e)

{
Exception CurrentException = Server.GetLastError();
Server.ClearError();
Response.Clear();Response.Write("Error message: "+ CurrentException.Message + "");
Response.Write("Error details:");
Response.Write(CurrentException.ToString());
}



Thursday, April 22, 2010

Bind Grid Using Generic List

HTML Code Aspx file:-

First, Create one class called Product.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public class Product
{
private int _Quantity;
public int Quantity
{
get { return _Quantity; }
set { _Quantity = value; }
}

private int _Price;
public int Price
{
get { return _Price; }
set { _Price = value; }
}

private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}

public Product()
{
}
public Product(string sname, int itemqty, int itemprice)
{
_Name = sname;
_Quantity = itemqty;
_Price = itemprice;
}
}

And then Copy this function on the page_Load event.

public void bindgrid() {

Product p = new Product();
List listProducts = new List();
listProducts.Add(new Product("Prouct 1", 1, 1));
listProducts.Add(new Product("Prouct 2", 2, 2));
listProducts.Add(new Product("Prouct 3", 3, 3));
listProducts.Add(new Product("Prouct 4", 4, 4));
listProducts.Add(new Product("Prouct 5", 5, 5));
listProducts.Add(new Product("Prouct 6", 6, 6));
listProducts.Add(new Product("Prouct 7", 7, 7));

gvProduct.DataSource = listProducts;
gvProduct.DataBind();
}

See the result

Wednesday, April 21, 2010

DataTable.Compute - filter parameters

The following code is to show most of the known feature of DataTable.Compute() method.

What this method do?

It computes the given expression on the current rows that pass the filter criteria.

object DataTable.Compute( string expression, string filter)

the function return "Object",so you can cast it to your data type as per the requirement.

The following aggregate types are supported in expression:

* Sum (Sum)
* Avg (Average)
* Min (Minimum)
* Max (Maximum)
* Count (Count)
* StDev (Statistical standard deviation)
* Var (Statistical variance)

Example,

int countFirstNameName = (int) dt.Compute("Count(FirstName)", "Name Like '" + txtFirstName.Text + "*'")

If the filter is not required, you must pass an empty string as the argument:

Like,
object sumObject = table.Compute("Sum(Amount)", "");

Here,
Amount is the Data tables Column name.