Thursday, December 29, 2011

ASP.Net - Different ways to transfer data between pages

1. Use the querystring:

protected void QueryStringButton_Click(object sender, EventArgs e)
         {
             Response.Redirect("QueryStringPage.aspx?Data=" + Server.UrlEncode(DataToSendTextBox.Text));
        }

2. Use HTTP POST:

<asp:Button ID="HttpPostButton" runat="server" Text="Use HttpPost"
             
PostBackUrl="~/HttpPostPage.aspx" onclick="HttpPostButton_Click"/>

protected void HttpPostButton_Click(object sender, EventArgs e)
        {
     // The PostBackUrl property of the Button takes care of where to send it!
        }

  3. Use Session State:

   protected void SessionStateButton_Click(object sender, EventArgs e)
         {
             Session["Data"] = DataToSendTextBox.Text;
             Response.Redirect("SessionStatePage.aspx");
        }

  4.  Use public properties:

   public string DataToSend
        {
            get
            {
                 return DataToSendTextBox.Text;
             }
        }

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

5. Use PreviousPage Control Info:

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

  // target page:
protected void Page_Load(object sender, EventArgs e)
        {
            var textbox = PreviousPage.FindControl("DataToSendTextbox") as TextBox;
             if (textbox != null)
            {
                DataReceivedLabel.Text = textbox.Text;
            }
        }

6. Use HttpContext Items Collection:

  protected void HttpContextButton_Click(object sender, EventArgs e)
         {
             HttpContext.Current.Items["data"] = DataToSendTextBox.Text;
             Server.Transfer("HttpContextItemsPage.aspx");
        }

// target page:
protected void Page_Load(object sender, EventArgs e)
         {
             this.DataReceivedLabel.Text =(String) HttpContext.Current.Items["data"];
        }

7. Use Cookies:

protected void CookiesButton_Click(object sender, EventArgs e)
        {
            HttpCookie cook =  new HttpCookie("data");
            cook.Expires = DateTime.Now.AddDays(1);
            cook.Value = DataToSendTextBox.Text;
             Response.Cookies.Add(cook);
             Response.Redirect("HttpCookiePage.aspx");
        }

// target page:
protected void Page_Load(object sender, EventArgs e)
        {
            DataReceivedLabel.Text = Request.Cookies["data"].Value;
        }

8. Use Cache:

protected void CacheButton_Click(object sender, EventArgs e)
         {
             Cache["data"] = DataToSendTextBox.Text;
             Server.Transfer("CachePage.aspx");
        }
   // target page:
    protected void Page_Load(object sender, EventArgs e)
         {
             this.DataReceivedLabel.Text = (string) Cache["data"];
        }

Tuesday, December 20, 2011

SQL SERVER – 2008 – Insert Multiple Records Using One Insert Statement – Use of Row Constructor

SQL Server 2008 Method of Row Construction:
USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES (
'First',1),
('Second',2),
('Third',3),
('Fourth',4),
('Fifth',5)

Monday, December 19, 2011

SQL Convert function - DateTime explored

Standard Date Formats
Date Format Standard SQL Statement Sample Output
Mon DD YYYY 1
HH:MIAM (or PM)
Default SELECT CONVERT(VARCHAR(20), GETDATE(), 100) Jan 1 2005 1:29PM 1
MM/DD/YY USA SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY] 11/23/98
MM/DD/YYYY USA SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY] 11/23/1998
YY.MM.DD ANSI SELECT CONVERT(VARCHAR(8), GETDATE(), 2) AS [YY.MM.DD] 72.01.01
YYYY.MM.DD ANSI SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD] 1972.01.01
DD/MM/YY British/French SELECT CONVERT(VARCHAR(8), GETDATE(), 3) AS [DD/MM/YY] 19/02/72
DD/MM/YYYY British/French SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY] 19/02/1972
DD.MM.YY German SELECT CONVERT(VARCHAR(8), GETDATE(), 4) AS [DD.MM.YY] 25.12.05
DD.MM.YYYY German SELECT CONVERT(VARCHAR(10), GETDATE(), 104) AS [DD.MM.YYYY] 25.12.2005
DD-MM-YY Italian SELECT CONVERT(VARCHAR(8), GETDATE(), 5) AS [DD-MM-YY] 24-01-98
DD-MM-YYYY Italian SELECT CONVERT(VARCHAR(10), GETDATE(), 105) AS [DD-MM-YYYY] 24-01-1998
DD Mon YY 1 - SELECT CONVERT(VARCHAR(9), GETDATE(), 6) AS [DD MON YY] 04 Jul 06 1
DD Mon YYYY 1 - SELECT CONVERT(VARCHAR(11), GETDATE(), 106) AS [DD MON YYYY] 04 Jul 2006 1
Mon DD, YY 1 - SELECT CONVERT(VARCHAR(10), GETDATE(), 7) AS [Mon DD, YY] Jan 24, 98 1
Mon DD, YYYY 1 - SELECT CONVERT(VARCHAR(12), GETDATE(), 107) AS [Mon DD, YYYY] Jan 24, 1998 1
HH:MM:SS - SELECT CONVERT(VARCHAR(8), GETDATE(), 108) 03:24:53
Mon DD YYYY HH:MI:SS:MMMAM (or PM) 1 Default +
milliseconds
SELECT CONVERT(VARCHAR(26), GETDATE(), 109) Apr 28 2006 12:32:29:253PM 1
MM-DD-YY USA SELECT CONVERT(VARCHAR(8), GETDATE(), 10) AS [MM-DD-YY] 01-01-06
MM-DD-YYYY USA SELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS [MM-DD-YYYY] 01-01-2006
YY/MM/DD - SELECT CONVERT(VARCHAR(8), GETDATE(), 11) AS [YY/MM/DD] 98/11/23
YYYY/MM/DD - SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD] 1998/11/23
YYMMDD ISO SELECT CONVERT(VARCHAR(6), GETDATE(), 12) AS [YYMMDD] 980124
YYYYMMDD ISO SELECT CONVERT(VARCHAR(8), GETDATE(), 112) AS [YYYYMMDD] 19980124
DD Mon YYYY HH:MM:SS:MMM(24h) 1 Europe default + milliseconds SELECT CONVERT(VARCHAR(24), GETDATE(), 113) 28 Apr 2006 00:34:55:190 1
HH:MI:SS:MMM(24H) - SELECT CONVERT(VARCHAR(12), GETDATE(), 114) AS [HH:MI:SS:MMM(24H)] 11:34:23:013
YYYY-MM-DD HH:MI:SS(24h) ODBC Canonical SELECT CONVERT(VARCHAR(19), GETDATE(), 120) 1972-01-01 13:42:24
YYYY-MM-DD HH:MI:SS.MMM(24h) ODBC Canonical
(with milliseconds)
SELECT CONVERT(VARCHAR(23), GETDATE(), 121) 1972-02-19 06:35:24.489
YYYY-MM-DDTHH:MM:SS:MMM ISO8601 SELECT CONVERT(VARCHAR(23), GETDATE(), 126) 1998-11-23T11:25:43:250
DD Mon YYYY HH:MI:SS:MMMAM 1 Kuwaiti SELECT CONVERT(VARCHAR(26), GETDATE(), 130) 28 Apr 2006 12:39:32:429AM 1
DD/MM/YYYY HH:MI:SS:MMMAM Kuwaiti SELECT CONVERT(VARCHAR(25), GETDATE(), 131) 28/04/2006 12:39:32:429AM

Some basic ASP.Net thoughts

What is State Management?
State management is the process by which you maintain state and page information over multiple requests for the same or different pages.
ASP.NET includes several options that help you preserve data on both a per-page basis and an application-wide basis.

Types of State Management
There are two basic types of State Management:
1.       Client-Based State Management
2.       Server-Based State Management
Client-Based State Management:
Client based state management techniques stores data on the client in various ways. Client based state management techniques are:
A.      View state
B.      Control state
C.      Hidden fields
D.      Cookies
E.       Query strings
View state:
The view state represents the state of the page when it was last processed on the server. It's used to build a call context and retain values across two successive requests for the same page. By default, the state is persisted on the client using a hidden field added to the page and is restored on the server before the page request is processed.
If the amount of data stored in the ViewState() property exceeds the specified value in the MaxPageStateFieldLength() property, multiple hidden fields are used to store View state data.
                Advantages:     
a)      Server resources not required.
b)      Simple implementation
c)       automatic retention of page and control state
d)      Enhanced security features. The values in view state are hashed, compressed, and encoded for Unicode implementations.
Disadvantages:
a)      Scope is limited to only single page.
b)      Performance. The view state is stored in the page itself, so increase the page size.
c)       Security. The view state is stored in a hidden field on the page. Although view state stores data in a hashed format, it can be tampered with. 
Control state:
If you create a custom control that requires view state to work properly, you should use control state to ensure your control work properly though developers disable view state. The ControlState() property allows you to persist property information that is specific to a control and cannot be turned off like the ViewState() property.
     Advantages:     
a)      Server resources not required.
b)      Reliable. Because control state can not be turned off like ViewState.

Disadvantages:
a)      Some programming is required.
Hidden Fields:
A hidden field acts as a repository for any page-specific information that you want to store directly in the page. ASP.NET allows you to store information in a HiddenField control, which renders as a standard HTML hidden field. A hidden field does not render visibly in the browser.
You must submit the page using an HTTP POST command in order for hidden-field values to be available.

                Advantages:     
a)      Server resources not required.
b)      Simple implementation
c)       Widespread support.
Disadvantages:
a)      Potential security risk.
b)      Performance. The hidden fields are stored in the page itself, so increase the page size.
c)       Does not support rich data types to store.
Cookies:
A cookie is a small amount of data that is stored either in a text file on the client file system or in-memory in the client browser session. Browser sends with cookies values with every page request to the same server. Cookies can be temporary or persistent.

Advantages:     
a)      Server resources not required.
b)      Simple implementation
c)       Configurable expiration rules
d)      Data persistent.
Disadvantages:
e)      Size limitation: Most browsers support maximum 4096 bytes cookies.
f)       User configuration refusal: User can disable cookies in their browser.
g)      Security risk: Can be tempered.  
Query strings:
A query string is information that is appended to the end of a page URL. Query strings provide a simple but limited way to maintain state information.  Some browsers and client devices impose a 2083 character limit on the length of the URL.
You must submit the page using an HTTP POST command in order for query string values to be available.
Advantages:     
h)      Server resources not required.
i)        Simple implementation
j)        Widespread support.
Disadvantages:
k)      Size limitation: Some browser limits 2083 chars on the length of URLs.
l)        Security risk: Information is visible to the user.  
Server-Based State Management:
Server based state management techniques store data in memory on the server.  Server based state management techniques are:
A.      Application state
B.      Session state
C.      Profile Properties
Application state:
Application state provides a method of storing data global to whole application. These data are visible to entire application and shared by all active sessions. That’s why Application state variables are global variables for an ASP.Net application.
Advantages:     
m)    Simple implementation
n)      Application wide scope
Disadvantages:
o)      Application scope in case of Web Garden or Web Firm.
p)      Limited Durability of data.
q)      Requires server memory.
Session state:
Session state provides a method of storing session specific information that is visible only within the session.
Advantages:     
r)       Simple implementation
s)       Session specific events
t)       Data can persists across multiple process
Disadvantages:
u)      Application scope
v)      Limited Durability of data.
w)    Requires server memory