Sunday, November 22, 2009

ASP.NET - Class file get Session, Application and QueryString

Session
System.Web.HttpContext.Current.Session["name"]

Application
System.Web.HttpContext.Current.Application["name"]

QueryString
System.Web.HttpContext.Current.Request.QueryString["name"]

Tuesday, November 17, 2009

ASP.NET - datetime format

Below listed the date time format you will frequently use in your application. You may replace the space in between the .ToString() to seperator instead of black space e.g. dash, slash, etc
DateTime d = DateTime.Now;
Response.Write("<br />today: " + d + "<br />");
//today: 11/18/2009 6:03:02 PM
Response.Write("<br />today: " + d.ToShortTimeString() + "<br />");
//today: 6:03 PM
Response.Write("<br />day: " + d.ToString("d dd ddd dddd") + "<br />");
//day: 18 18 Wed Wednesday
Response.Write("<br />month: " + d.ToString("M MM MMM MMMM") + "<br />");
//month: 11 11 Nov November
Response.Write("<br />year: " + d.ToString("y yy yyy yyyy") + "<br />");
//year: 9 09 2009 2009
Response.Write("<br />hour: " + d.ToString("h hh H HH") + "<br />");
//hour: 6 06 18 18
Response.Write("<br />minute: " + d.ToString("m mm") + "<br />");
//minute: 3 03
Response.Write("<br />second: " + d.ToString("s ss") + "<br />");
//second: 2 02
Response.Write("<br />AM/PM: " + d.ToString("t tt") + "<br />");
//AM/PM: P PM

ASP.NET - GridView RowCommand get row index


if you did assign a value into the CommandArgument e.g. below, you need to use the #1 methond else #2
<asp:ItemTemplate>
<asp:asp:ImageButton ID="imgUpdate" runat="server" CommandName="Update1"
CommandArgument='<%# Eval("id") %>' ImageUrl="~/icons/Update001 (2).gif" />
<asp:/ItemTemplate>

#1
VB
Dim selectedRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim intRowIndex As Integer = Convert.ToInt32(selectedRow.RowIndex)
GridView.Rows(intRowIndex).BackColor = System.Drawing.Color.Blue

C#
GridViewRow selectedRow = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
int intRowIndex = Convert.ToInt32(selectedRow.RowIndex);
GridView.Rows[intRowIndex].BackColor = System.Drawing.Color.Blue;

or #2
VB
Dim selectedRow As GridViewRow = GridView.Rows(Convert.ToInt32(e.CommandArgument))
selectedRow.BackColor = System.Drawing.Color.Blue

C#
GridViewRow selectedRow = GridView.Rows[Convert.ToInt32(e.CommandArgument)];
selectedRow.BackColor = System.Drawing.Color.Blue;

EXCEL - Date & Time format

Date & Time
=(DATE(YEAR(NOW()), MONTH(NOW()), DAY(NOW())) + TIME(HOUR(NOW()),MINUTE(NOW()),SECOND(NOW())))

Calculate working days different between two dates
=NETWORKDAYS(startdate, enddate)