Sunday, November 8, 2009

DISPLAY Datatbale

//Create the DataTable named "employee"
DataTable employee = new DataTable("Employee");
//Add the DataColumn using all properties
DataColumn eid = new DataColumn("Eid");
eid.DataType = typeof(string);
eid.MaxLength = 10;
eid.Unique = true;
eid.AllowDBNull = false;
eid.Caption = "EID";
employee.Columns.Add(eid);
//Add the DataColumn using defaults
DataColumn firstName = new DataColumn("FirstName");
firstName.MaxLength = 35;
firstName.AllowDBNull = false;
employee.Columns.Add(firstName);
DataColumn lastName = new DataColumn("LastName");
lastName.AllowDBNull = false;
employee.Columns.Add(lastName);
//Add the decimal DataColumn using defaults
DataColumn salary = new DataColumn("Salary", typeof(decimal));
salary.DefaultValue = 0.00m;
employee.Columns.Add(salary);
//Derived column using expression
DataColumn lastNameFirstName = new DataColumn("LastName and FirstName");
lastNameFirstName.DataType = typeof(string);
lastNameFirstName.MaxLength = 70;
lastNameFirstName.Expression = "lastName + ', ' + firstName";
employee.Columns.Add(lastNameFirstName);

//Add New DataRow by creating the DataRow first DataRow newemployee = employee.NewRow(); newemployee["Eid"] = "123456789A"; newemployee["FirstName"] = "Nancy"; newemployee["LastName"] = "Davolio"; newemployee["Salary"] = 10.00m; employee.Rows.Add(newemployee);
//Add New DataRow by simply adding the values employee.Rows.Add("987654321X", "Andrew", "Fuller", 15.00m);
//Load DataRow, replacing existing contents, if existing
employee.LoadDataRow(
new object[] { "987654321X", "Janet", "Leverling", 20.00m },
LoadOption.OverwriteChanges);

GridView1.DataSource = employee;

GridView1.DataBind();


Wednesday, September 30, 2009

Managing Session Timeout in ASP.net

Hi Coders,

Recently,I was asked to add session time to an existing website.

On goggling,I came across many useful links.First one from @ Geeks with Blogs
It needs that the base class must be added to each page before it is implemented.

The other one from 4guysfromrolla.com
Of course, my other useful links were there.

Finally , the solution that I found fit for my website included changes is global.aspx only.

When the user logins in successfully session variable loginname is created:
Session("Loginname") = Request("txtUser")).ToString.ToUpper 'txtUser -- my textbox on login page


And finally the code for global.aspx goes here:

Protected Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As System.EventArgs)

If TypeOf Context.Handler Is IRequiresSessionState OrElse TypeOf Context.Handler Is IReadOnlySessionState Then
Dim currentRequestpage As String = HttpContext.Current.Request.ServerVariables("SCRIPT_NAME").ToString().Substring(HttpContext.Current.Request.ServerVariables("SCRIPT_NAME").ToString().LastIndexOf("/") + 1)
If currentRequestpage.Equals("loginpage.aspx", StringComparison.CurrentCultureIgnoreCase) Then
'pages that I wanted to skip
Exit Sub
Else
If Session("Loginname") IsNot Nothing Then

Else
'redirect to login page
Response.Redirect("../loginpage.aspx?ErrMsg=3")
Response.End()
Session.Abandon()

End If
End If

End If

End Sub


The code above checks if username is nothing it redirects the User to login page.

Known issues:
If you are using a user control or alike then this function will be called multiple times.
Slowing down the speed of the website.However, in my case issue was not of much disadvantage.


In addition this code will automatically secure the website from any attacks.

KiSS (Keep It simple St***D)

Hey please let me know if you have any issues working with the code or have better ideas to share..

Regards...

Gourav

gupta.g21@gmail.com



Monday, September 28, 2009

Web Site Hosting for free!!

I just called up an HR in one of the companies. The lady asked about URL of Web Sites that I have developed.

Oops !! I was caught on wrong foot!!

Then started looking for ways for free website hosting

I just googled the same.. The two top results ....

1. www.webs.com :

Webs - Make a free website, get free hosting

2.www.doteasy.com

Free Web Hosting and Domain Name Registration Services by Doteasy.com

Doteasy offers bannerless web hosting, domain name registration, and free email service for your small business or personal website.






HI I am Gourav.  Blogging  straight from my desk...