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)

Monday, May 25, 2009

ASP - Passing data between pages

Here i am going to demonstrate the ways you pass data from one page to another. supposingly you have a textbox control in page1 for user to enter the user id and user name and upon submit, redirect user to page2. In page2, you can retrieve the data passed from page1 using the following method:

Querystring
This is the most common way to pass data and can be seen in almost all webform thru the URL, e.g.
www.website.com?userid=1&username=abc. From the link, we know the userid value which is 1 and username as abc seperated by &

page1.aspx
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("page2.aspx?userid=" + TextBox1.Text + "&username=" + Textbox2.Text);
}

page2.aspx
string strUserID = Request.QueryString["userid"];
string strUserName = Request.QueryString["username"];

note
You can access the parameters in the URL using Request.QueryString by specifying the parameter name or index anywhere within the page

Session
This varible will be stored in the memory and accessible thoughout the websiteSince this variable will be stored in the server's memory, its
page1.aspx
protected void Button1_Click(object sender, EventArgs e)
{
Session["userid"] = TextBox1.Text;
Session["username"] = TextBox2.Text;
Response.Redirect("page2.aspx");
}

page2.aspx
string strUserID = Session["userid"].ToString();
string strUserName = Session["username"].ToString();

note
You can access the parameters in the Session variable by specifying the variable name or index anywhere within the website

Server.Tranfer
Beside using the Response.Redirect above, here im going to use Server.Trasfer to open page2
page1.aspx
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("page2.aspx", true);
}

page2.aspx
string strUserID = Request.Form["TextBox1"];
string strUserName = Request.Form["TextBox2"];

note
You can access the value of the control in the old page by specifying the control name


Cookies
Is a small text file stored on the client machine ~4kbs
page1.aspx
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("UserID");
cookie.Value = TextBox1.Text;
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
HttpCookie cookie = new HttpCookie("UserName");
cookie.Value = TextBox2.Text;
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
Response.Redirect("WebForm2.aspx");
}

page2.aspx
string strUserID = Request.Cookies["UserID"].Value;
string strUserName = Request.Cookies["UserName"].Value;

note
You can access the cookies by specifying the cookies name within the website
http://msdn.microsoft.com/en-us/library/ms178194.aspx

PreviousPage
2.0 and above

page1.aspx

protected void Button1_Click(object sender, EventArgs e){ Server.Transfer("page2.aspx");
}

page2.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TextBox txt1 = (TextBox)Page.PreviousPage.FindControl("TextBox1");
TextBox txt2 = (TextBox)Page.PreviousPage.FindControl("TextBox2");
string strUserID = txt1.Text;
string strUserName = txt2.Text;
}
}


note
I used Server.Transfer in this case

ASP.Net - Textbox validate integer & float / decimal

Back few years ago, when we want to prevent user to key in certain value into the textbox at the client side, we need to write some massive JavaScript to achieve this. When AJAX in place especially builded into VS 2008, it makes programmer life easier by just including the AJAX extender on top of the textbox by setting few parameters and its done.

Here im going to demonstrate the ways you can validate value in your textbox i.e. integer and float by using JavaScript and AJAX

JavaScipt

integer
function validnumber(ev) {
ev.returnValue = (ev.keyCode>=48 && ev.keyCode<=57);
}
<asp:TextBox ID="txtInt" runat="server" onkeypress="validnumber(event)" />

note
Notice that in the textbox, i added an onKeyPress event which is triggered when user press a key on the keyboard. This validnumber function will receive the keypress event. In the JavaScript function i will check which key has being pressed on the keyboard i.e. 0 to 9 and return whether it is a valid integer or not

float/decimal
function validfloat(txt, ev)
{
ev.returnValue = ((ev.keyCode>=48 && ev.keyCode<=57) ev.keyCode==46 && txt.value.indexOf('.') == -1); } <asp:TextBox ID="txtFloat" runat="server" onkeypress="validfloat(this, event)" />>

note
Notice that in the textbox, i added an onKeyPress event which is triggered when user press a key on the keyboard. This validfloat function will receive the textbox control & the keypress event. In the JavaScript function i will check which key has being pressed on the keyboard i.e. 0 to 9 or fullstop & is it the only fullstop available in the textbox and return whether it is a valid float or not

AJAX

first, make sure AjaxControlToolkit available in your page. i used to include the AjaxControlToolkit in the page directives as below
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

integer
<asp:TextBox ID="txtAjaxInt" runat="server" />
<cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" TargetControlID="txtAjaxInt" FilterType="Numbers" runat="server" />

note
I added a AJAX's FilteredTextBoxExtender as an extender to the txtAjaxInt textbox by defining the TargetControlID
To only allow the user to key in integers into this textbox, i specify the FilterType to Numbers

float/decimal
<asp:TextBox ID="txtAjaxFloat" runat="server" />
<cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender2" TargetControlID="txtAjaxFloat" FilterType="Custom, numbers" ValidChars="." runat="server" />
<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ControlToValidate="txtAjaxFloat" ErrorMessage="Enter a valid float" ValidationExpression="\d+\.\d{2}" />
<asp:button id="btn1" runat="Server" text="test float" />

note
I added a AJAX's FilteredTextBoxExtender as an extender to the txtAjaxFloat textbox by defining the TargetControlID. To only allow the user to key in integers or fullstop into this textbox, i specify the FilterType to Custom, numbers and ValidChars to . which means user can key in any integers only plus fullstop. Drawback on this extender is, it allows user to enter more than one fullstop hence i added in a RegularExpressionValidator to check there is only one fullstop in the textbox by specifying the ValidationExpression to \d+\.\d{2} (only allow user to key in up to 2 decimal point {2}). The RegularExpressionValidator will be triggered when validation take place e.g. click on the button as illustrate above

Saturday, May 23, 2009

SQL - Split string

In this post i will demonstrate how can you split a string by using SQL
If you have a string like:
1,James,USA;2,Amy,Canada;3,Charles,Singapore;
and want to split this to the format below:
UserIDUserNameCountry
1JamesUSA
2AmyCanada
3CharlesSingapore

basically you need to loop thru the string to search for delimeter i.e. semicolon to get one user record, and splitting the sub string again using another delimeter i.e. comma to get the user's UserID, UserName, Country

NOTE: in whatever splitting function, you need to make sure to format variable is properly formatted else more customer checking needed

DECLARE @string varchar(100)
SET @string = '1,James,USA;2,Amy,Canada;3,Charles,Singapore;'

DECLARE @delimiter1 CHAR(1)
DECLARE @delimiter2 CHAR(1)
SET @delimiter1 = ';'
SET @delimiter2 = ','

DECLARE @pos as int
SET @pos = charindex(@delimiter1, @string)

DECLARE @temp AS NVARCHAR(1000)
DECLARE @country NVARCHAR(100)
DECLARE @username NVARCHAR(100)
DECLARE @userid INT

DECLARE @tbl_user TABLE (userid int, username varchar(50), country varchar(50))

WHILE(@pos > 0)
BEGIN
SET @temp = SUBSTRING(@string, 0, @pos)
SET @userid = SUBSTRING(@temp, 0, CHARINDEX(@delimiter2, @temp))
SET @username = SUBSTRING(@temp, CHARINDEX(@delimiter2, @temp) + 1, CHARINDEX(@delimiter2, @temp, CHARINDEX(@delimiter2, @temp) + 1) - CHARINDEX(@delimiter2, @temp) - 1)
SET @country = SUBSTRING(@temp, CHARINDEX(@delimiter2, @temp, CHARINDEX(@delimiter2, @temp) + 1) + 1, @pos - CHARINDEX(@delimiter2, @temp, CHARINDEX(@delimiter2, @temp) + 1))
INSERT INTO @tbl_user (userid, username, country) VALUES (@userid, @username, @country)
SET @string = SUBSTRING(@string, charindex(@Delimiter1,@String) + 1, LEN(@string) - charindex(@Delimiter1,@String))
SET @pos = charindex(@Delimiter1,@string)
END
SELECT * FROM @tbl_user

note
As you can see, the above sql utilize more on using SUBSTRING and also CHARINDEX function to get the position of each delimeter and extract the data based on the specific location index

SET @string = '1,James,USA;2,Amy,Canada;3,Charles,Singapore;'
SET @temp = SUBSTRING(@string, 0, @pos)
Result: 1,James,USA - to extract the first set of user record

SET @userid = SUBSTRING(@temp, 0, CHARINDEX(@delimiter2, @temp))
Result: 1 - to get the first userid which will start from the index 0 and to length of the first character index of comma

gettting the @username and @country also utilizing this approach

SET @string = SUBSTRING(@string, charindex(@Delimiter1,@String) + 1, LEN(@string) - charindex(@Delimiter1,@String))
- to get the remaining set of user records i.e 2,Amy,Canada;3,Charles,Singapore;

SET @pos = charindex(@Delimiter1,@string)
- to get the position of next semicolon

SQL - Create table

Creating temporary table
1. using #
CREATE TABLE #tbl_user(userid int,username varchar(50))
INSERT INTO #tbl_user (userid, username)
SELECT 1, 'user name 1' UNION ALL
SELECT 2, 'user name 2' UNION ALLS
SELECT 3, 'user name 3'

SELECT * FROM #tbl_user

DROP TABLE #tbl_user

note
Here im using the # hash sign when creating the temporary table which tell the SQL Server i want to create a local temporary table located in the tempdb. If you are creating this table within one store procedure, this table will only usable within this store procedure and it will automatically dropped when its out of the store procedure scope

2. using @
DECLARE @tbl_user TABLE (
userid int,
username varchar(50))

INSERT INTO @tbl_user (userid, username)
SELECT 1, 'user name 1' UNION ALL
SELECT 2, 'user name 2' UNION ALL
SELECT 3, 'user name 3'

SELECT * FROM @tbl_user

note
Here im using the @ sign when creating the temporary table which tell the SQL Server i want to create a local temporary table in the memory. This variable will auto cleared itself once it goes out of it scope


Creating permanent table
CREATE TABLE tbl_user (
userid int,
username varchar(50))

INSERT INTO tbl_user (userid, username)
SELECT 1, 'user name 1' UNION ALL
SELECT 2, 'user name 2' UNION ALL
SELECT 3, 'user name 3'

SELECT * FROM tbl_user

--DROP TABLE tbl_user

note
Here im trying to create a permanent table in the same database where this query runs. This is the most common where you want to store the data permanently

Friday, May 22, 2009

SQL - Select multiple rows into single column

Here im going to demonstrate how can you concate all the values in multiple rows into a single value:

Data
TABLE: @tbl_user_request

Raw data
userid request_id
1 'A000001'
1 'A000002'
2 'A000003'
3 'A000004'
4 'A000005'
5 'A000006'
5 'A000007'

Expected result
userid request_id
1 A000001,A000002
2 A000003
3 A000004
4 A000005
5 A000006,A000007

SQL
DECLARE @tbl_user_request TABLE
(userid int, request_id varchar(50))

INSERT INTO @tbl_user_request
SELECT 1, 'A000001' UNION ALL
SELECT 1, 'A000002' UNION ALL
SELECT 2, 'A000003' UNION ALL
SELECT 3, 'A000004' UNION ALL
SELECT 4, 'A000005' UNION ALL
SELECT 5, 'A000006' UNION ALL
SELECT 5, 'A000007'

SELECT a.userid, request_id=substring((SELECT ( ', ' + request_id)
FROM @tbl_user_request b
WHERE a.userid=b.userid
ORDER BY userid
FOR XML PATH('')), 3, 1000)FROM @tbl_user_request a
GROUP BY userid

note

There is a subquery in the SQL which joining the same table as the main query. The purpose of this subquery is to select all the "requet_id" pertaining to the same userid in the "tbl_user" table and concatenate them into a single "concatename" variable by using the FOR XML PATH


For more example you can refer here:
http://code.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=createacommadelimitedlist