Thursday 2 August 2007

Where is Global.asax file in ASP.NET 2.0

I just figure out how to add Global.asax file in Microsoft Visual Studio.Net 2005.
Here is step by step guide.



First of all you need to Add Global.asax fle from



-->right click project-->Add New Item-->Add Global.asax



change script top tag to




@ Application Language="C#" CodeBehind ="Global.asax.cs" Inherits="NEWSU.Global" %>

where NewsU is namespace in my case \.

comment all script bellow this tag.



Now add normal class file to APP_Code folder in solution.

Right click->Add New Item->Class.



Name it as Global.asax.cs

and then inherit it from System.Web.HttpApplication.

Copy code (events handlers) from Global.asax file to new Global.asax.cs file

aha now you can add your code inside and this class will always execute upon intantiation of application from web server.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
///
/// Summary description for Global
///

///
namespace NEWSU
{
public partial class Global : System.Web.HttpApplication
{
public static int usercnt = 0;
public Global()
{
//
// TODO: Add constructor logic here
//
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}

Thursday 19 July 2007

Difference between DataRow.Delete and DataTable.Rows.Remove Methods

DataRow.Delete() Method:

If delete() method is called on a row, after last Acceptchanges() was called,
values are deleted but row exists in table and values can be recovered by using DataTable.Rejectchanges().
DataRow.RowState become "deleted" and current values are are set to Null.
However there is a trick--->
The Delete method physically removes DataRow only if it was added to the DataTable since the last AcceptChanges() was called ,Otherwise,it sets RowState=RowState.Deleted and set current values to Null.

Remove Method DataRowCollection physically removes dataRow and wont be recovered.

However Remove Method does not effect datasource when Update Method Data Adapter is called , you need to call Delete Method of Data Row.





C# Examples:
Delete:


DataTable table = new DataTable("table");
DataColumn idColumn = new DataColumn("id",
Type.GetType("System.Int32"));
idColumn.AutoIncrement = true;
DataColumn itemColumn = new DataColumn("desc",
Type.GetType("System.String"));
table.Columns.Add(idColumn);
table.Columns.Add(itemColumn);
// Add ten rows.
DataRow newRow;
for (int i = 0; i <>
{
newRow = table.NewRow();
newRow["desc"] = "Item " + i;
table.Rows.Add(newRow);
// table.Rows.Remove(newRow);
System.Diagnostics.Debug.Write(newRow[0].ToString());
}
table.AcceptChanges();
DataRow rnew = table.NewRow();
rnew["item"] = "zenab row";
table.Rows.Add(rnew);
table.Rows[table.Rows.Count-1].Delete();//phyically removes the row if rowstat is set to added
table.Rows[1].Delete();//values are deleted but row exists in table and values can be recovered table.Rejectchanges() if rowstate is unchanged
foreach (DataRow r in table.Rows)
{
if (r.RowState != DataRowState.Deleted)
listBox6.Items.Add(r["desc"].ToString());
else if (r.RowState == DataRowState.Deleted)
{
MessageBox.Show(r.ToString());
}
}
table.RejectChanges();
foreach (DataRow r in table.Rows)
{
if (r.RowState != DataRowState.Deleted)
listBox6.Items.Add(r["desc"].ToString());
else if (r.RowState == DataRowState.Deleted)
{
MessageBox.Show(r.ToString());
}

}

Remove Method:




DataTable table = new DataTable("table");
DataColumn idColumn = new DataColumn("id",
Type.GetType("System.Int32"));
idColumn.AutoIncrement = true;
DataColumn itemColumn = new DataColumn("Desc",
Type.GetType("System.String"));
table.Columns.Add(idColumn);
table.Columns.Add(itemColumn);
// Add ten rows.
DataRow newRow;
for (int i = 0; i <>
{
newRow = table.NewRow();
newRow["Desc"] = "Item " + i;
table.Rows.Add(newRow);
// table.Rows.Remove(newRow);
System.Diagnostics.Debug.Write(newRow[0].ToString());
}
DataRow rR = table.NewRow();
rR[1] = "zenab";
table.Rows.Add(rR);
table.AcceptChanges();
table.Rows.Remove(rR);//row no longer exits, vlaues can not be recoverd by call rejectchanges
foreach (DataRow r in table.Rows)
{
listBox5.Items.Add(r["Desc"].ToString());

}
table.RejectChanges();
foreach (DataRow r in table.Rows)
{
listBox5.Items.Add(r["Desc"].ToString());


}