Profile

Cover photo
132 followers|11,938 views
AboutPostsPhotosVideos

Stream

asp.net Tutorial

Shared publicly  - 
 
var vs dynamic vs object
1
Add a comment...

asp.net Tutorial

Shared publicly  - 
 
Keyboard Shortcut Keys for Visual Studio 2008, 2010, 2012
Keyboard shortcut keys for 2012, 2010 and 2008.Using this keyboard shortcut keys for visual studio we can easily navigate without using mouse
Project Related Shortcut Keys
ShortCut
Description
Ctrl-Shift-B
Builds the solution
Ctrl-N
Displays the New File dialog. Note: files created this way are not associated with a project. Use Ctrl-Shift-A to add a new file in a project
Ctrl-Shift-N
Displays the New Project dialog
Ctrl-O
Displays the Open File dialog
Ctrl-Shift-O
Displays the Open Project dialog
Shift-Alt-A
Displays the Add Existing Item dialog
Ctrl-Shift-A
Displays the Add New Item dialog
Ctrl-Alt-Insert
Allows you to override base class methods in a derived class when an overridable method is highlighted in the Class View pane
Ctrl-M-O
Collapse all the methods, classes, regions in the current code behind or class file
Ctrl-M-P or Ctrl-M-L
Expands all the methods, classes, regions in the current code behind or class file
Ctrl-F
Displays the Find dialog
Ctrl-H
Displays the Replace dialog
Ctrl-Shift-F
Find the reference of selected item into entire solution.
Ctrl-Tab
Move from one opened file to another opened file in visual studio.
F9
Sets or removes a breakpoint at the current line
Ctrl-F9
Enables or disables the breakpoint on the current line of code. The line must already have a breakpoint for this to work
F5
Runs the code with invoking the debugger.
Ctrl-F5
Runs the code without invoking the debugger.
F4 or Alt-Enter
Displays the Properties window, which lists the design-time properties and events for the currently selected item
Ctrl-Alt-S
Displays the Server Explorer window, which allows you to view and manipulate database servers, event logs, message queues, web services, and many other operating system services
Ctrl-Alt-L
Displays the Solution Explorer, which lists the projects and files in the current solution
Ctrl-Alt-X
Displays the Toolbox, which contains controls and other items that can be dragged into editor and designer windows
Ctrl-Alt-I
Displays the Immediate window, where you can find the controls or variables values or can do data manipulation during debugging
 
Text Manipulation Shortcut Keys
ShortCut
Description
Shift-Tab
Moves current line or selected lines one tab stop to the left
Backspace or
Deletes one character to the left of the cursor
Shift-Backspace
Ctrl-G
Go to Particular line
Ctrl-K, Ctrl-C
Marks the current line or selected lines of code as a comment, using the correct comment syntax for the programming language
Ctrl-K, Ctrl-U
Removes the comment syntax from the current line or currently selected lines of code
Ctrl-T or
Swaps the characters on either side of the cursor. (For example, AC|BD becomes AB|CD.) Available only in text editors
Shift-Enter
Ctrl-K, Ctrl-L
Removes all unnamed bookmarks in the current document
Ctrl-M, Ctrl-O
Automatically determines logical boundaries for creating regions in code, such as procedures, and then hides them. This collapses all such regions in the current document
Alt-Right Arrow or
Displays statement completion based on the current language or autocompletes word if existing text unambiguously identifies a single symbol
Ctrl-Spacebar
Ctrl-K, Ctrl-\
Removes horizontal whitespace in the selection or deletes whitespace adjacent to the cursor if there is no selection
Ctrl-K, Ctrl-F
Applies the indenting and space formatting for the language as specified on the Formatting pane of the language in the Text Editor section of the Options dialog to the selected text.
Ctrl-L
Cuts all selected lines or the current line if nothing has been selected to the clipboard
Ctrl-Shift-L
Deletes all selected lines or the current line if no selection has been made
Ctrl-Enter
Inserts a blank line above the cursor
Ctrl-Shift-Enter
Inserts a blank line below the cursor
Shift-Alt-T
Moves the line containing the cursor below the next line
Ctrl-J
Lists members for statement completion when editing code
Ctrl-U
Changes the selected text to lowercase characters
Ctrl-Shift-U
Changes the selected text to uppercase characters
Ctrl-Shift-Spacebar
Displays a tooltip that contains information for the current parameter, based on the current language
Ctrl-M, Ctrl-U
Removes the outlining information for the currently selected region
Ctrl-M, Ctrl-P
Removes all outlining information from the entire document
Ctrl-R, Ctrl-P
Swaps the anchor and endpoint of the current selection
Ctrl-M, Ctrl-L
Toggles all previously marked hidden text sections between hidden and display states
Ctrl-K, Ctrl-K
Sets or removes a bookmark at the current line
Ctrl-M, Ctrl-M
Toggles the currently selected hidden text section or the section containing the cursor if there is no selection between the hidden and display states
Ctrl-K, Ctrl-H
Sets or removes a shortcut in the tasklist to the current line
Ctrl-R, Ctrl-R
Enables or disables word wrap in an editor
Ctrl-R, Ctrl-W
Shows or hides spaces and tab marks
Ctrl-Delete
Deletes the word to the right of the cursor
Ctrl-Backspace
Deletes the word to the left of the cursor
Ctrl-Shift-T
Transposes the two words that follow the cursor. (For example, |End Sub would be changed to read Sub End|.)
 
Text Navigation Shortcut Keys
ShortCut
Description
Ctrl-End
Moves the cursor to the end of the document
Ctrl-Home
Moves the cursor to the start of the document
Ctrl-G
Displays the Go to Line dialog. If the debugger is running, the dialog also lets you specify addresses or function names to go to
Ctrl-]
Moves the cursor to the matching brace in the document. If the cursor is on an opening brace, this will move to the corresponding closing brace and vice versa
Ctrl-K, Ctrl-N
Moves to the next bookmark in the document
Ctrl-K, Ctrl-P
Moves to the previous bookmark
Ctrl-K, Ctrl-I
Displays Quick Info, based on the current language
Ctrl-Down Arrow
Scrolls text down one line but does not move the cursor. This is useful for scrolling more text into view without losing your place. Available only in text editors
Ctrl-Up Arrow
Scrolls text up one line but does not move the cursor. Available only in text editors
Ctrl-Right Arrow
Moves the cursor one word to the right
Ctrl-Left Arrow
Moves the cursor one word to the left
Ctrl-Shift-1
Navigates to the next definition, declaration, or reference of an item. Available in the object browser and Class View window. Also available in source editing windows if you have already used the Edit.GoToReference (Shift-F12) shortcut
Ctrl-Shift-2
Navigates to the previous definition, declaration, or reference of an item
 
General Shortcut Keys
ShortCut
Description
Ctrl-X or Shift-Delete or Ctrl-L
Cuts the currently selected item to the clipboard
Ctrl-Del
Delete next "word"
Ctrl-C or Ctrl-Insert
Copies the currently selected item to the clipboard
Ctrl-V or Shift-Insert
Pastes the item from the clipboard at the cursor location
Ctrl-Z or Alt-Backspace
Undo the previous editing action
Ctrl-Space
To see intelligence dialog
Ctrl-Y or Ctrl-Shift-Z
Redo the previous undo action
Ctrl-S
Saves the current selected file
Ctrl-Shift-S
Saves all files and projects
Ctrl-P
Displays the Print dialog
F7
Switches from the design view to the code view in the editor
Shift-F7
Switches from the code view to the design view in the editor
Shift-F8 or F8
Navigate to compile time errors
Alt-Shift-A
Add Existing Item(file) to selected project
Ctrl-Shift-A
Add New Item(file) to selected project
Shift-F9
Display the selected item quick output means contains value while debugging
F12
Moves the cursor to the selected method, variable, class definition.
Shift-F12
Finds the reference to the selected method, variable, class or the item under the cursor
Ctrl-}
Match curly braces, brackets or compiler directives
Ctrl-Shift-}
Select text between matched braces, brackets or compiler directives
 
1
Add a comment...

asp.net Tutorial

Shared publicly  - 
 
jquery DataTable PlugIn in ASP.Net using C# or jQuery DataTables and ASP.NET Integration for GridView
///////////////////////////////////////////////////////////////////////////////
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridTable.aspx.cs"  
Inherits="DT_Pagination.GridTable" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">     <title></title>     <%--<link href="media_ColVis/css/ColVisAlt.css" rel="stylesheet" type="text/css" />-%>     <link href="media_ColVis/css/ColVis.css" rel="stylesheet" type="text/css" />     <link href="media/css/TableTools.css" rel="stylesheet" type="text/css" />     <link href="media/css/TableTools_JUI.css" rel="stylesheet" type="text/css" />     <link href="Scripts/css/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />     <link href="Scripts/css/themes/smoothness/jquery.ui.theme.css" rel="stylesheet" type="text/css" />          <link href="Scripts/css/jquery.dataTables_themeroller.css" rel="stylesheet" type="text/css" />     <script src="Scripts/js/jquery.js" type="text/javascript"></script>     <script src="Scripts/js/jquery.dataTables.min.js" type="text/javascript"></script>     <script src="media/js/ZeroClipboard.js" type="text/javascript"></script>     <%-<script src="media/js/TableTools.min.js" type="text/javascript"></script>-%>     <script src="media/js/TableTools.js" type="text/javascript"></script>     <script src="Scripts/js/jquery.dataTables.columnFilter.js" type="text/javascript"></script>     <script src="Scripts/js/jquery-ui-1.9.2.custom.min.js" type="text/javascript"></script>     <script src="Scripts/js/FixedHeader.js" type="text/javascript"></script>     <script src="media_ColVis/js/ColVis.js" type="text/javascript"></script>     <style type="text/css">         .ui-datepicker-calendar tr, .ui-datepicker-calendar td, .ui-datepicker-calendar td a, .ui-datepicker-calendar th         {             font-size: inherit;         }         div.ui-datepicker         {             font-size: 10px;         }         .ui-datepicker-title span         {             font-size: 10px;         }                  .my-style-class input[type=text]         {             color: green;         }     </style>     <script type="text/javascript">         var oTable;         $(document).ready(function () {             $.datepicker.regional[""].dateFormat = 'dd/mm/yy';             $.datepicker.setDefaults($.datepicker.regional['']);             TableTools.DEFAULTS.aButtons = [ "copy", "csv", "xls", "pdf", "print", { "sExtends": "collection", "sButtonText": "Save", "aButtons": [                                                 "csv",                                                 "xls", //"pdf",                                                 {                                                 "sExtends": "pdf",                                                 //"sPdfOrientation": "landscape",                                                 "sPdfMessage": "Your custom message would go here."                                              },                                  "print"                                             ] }]             //TableTools.DEFAULTS.aButtons = [ "copy", "csv", "xls",  "pdf" ];             /          Main Functionality       /             $('#GridView1').dataTable({                 //"oLanguage": { "sSearch": "Search the nominees:" },                 "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],                 "iDisplayLength": 10,                 "aaSorting": [[0, "asc"]],                 "bJQueryUI": true,                 "bAutoWidth": false,                 "bProcessing": true,                 "sDom": '<"top"i><"title">lt<"bottom"pf>',                 "sPaginationType": "full_numbers",                 "bRetrieve": true,                 //Scrolling--------------                 "sScrollY": "250px",                 "sScrollX": "100%",                                 "sScrollXInner": "100%",                 "bScrollCollapse": true,                 // ---  Print_Export_Copy  --                                 "sDom": 'T<"clear"><"H"lfr>t<"F"ip>',                 //"sDom": '<"top"iflp<"clear">>rt<"bottom"iflp<"clear">>',                                             // --- Column Visiblity ----                                 //"sDom": '<"H"Cfr>t<"F"ip>',                 //                "oColVis": //                { //                    //"sDom": 'C<"clear">lfrtip', //                    "activate": "mouseover"                     //             //"bJQueryUI": true //                },                 //- Dynamic Language---------                 "oLanguage": {                     "sZeroRecords": "There are no Records that match your search critera",                     "sLengthMenu": "Display _MENU_ records per page&nbsp;&nbsp;",                     "sInfo": "Displaying _START_ to _END_ of _TOTAL_ records",                     "sInfoEmpty": "Showing 0 to 0 of 0 records",                     "sInfoFiltered": "(filtered from _MAX_ total records)",                     "sEmptyTable": 'No Rows to Display.....!',                     "sSearch": "Search all columns:"                 },                 / Column Sorting And Searching /                 //      "aoColumns": [                 // { "bSearchable": false }, //Disable search on this column 1                 // {"bSortable": false }, //Disable sorting on this column 2                                // {"asSorting": ["asc"] }, //Allow only "asc" sorting on column 2                 // null,                 // { "sSortDataType": "dom-text", "sType": "numeric" },                 // { "iDataSort": 4 }, //Use column 4 to perform sorting                 // null,                 // null                 // ],                 /  Column Visibilities /                 // "aoColumns": [                 // / Sno /{"bSearchable": false, "bVisible": false},                 // / Engine /   null,                 // / Browser /  null,                 // / Platform / { "bSearchable": false, "bVisible":    false },                 // / Version /  { "bSearchable": false, "bVisible":    false },                 // / Grade /     null,                 //      / Share /    null,                 //      / Date /    null                 // ],                 "oSearch": {                     "sSearch": "",                     "bRegex": false,                     "bSmart": true                 },                 //------------------------Total in footer                                 "fnFooterCallback": function TotalCalc(nRow, aaData, iStart, iEnd, aiDisplay) {                     / Calculate the total market share for all browsers in this table (ie inc. outside the pagination) /                     var iTotalMarket = 0;                     for (var i = 0; i < aaData.length; i++) {                         //alert('Length : ' + aaData.length + ', Row No : ' + i + ', Share : ' + aaData[i][6]);                         iTotalMarket += parseInt(aaData[i][6]);                     }                     / Calculate the market share for browsers on this page /                     var iPageMarket = 0;                     for (var i = iStart; i < iEnd; i++) {                         iPageMarket += parseInt(aaData[aiDisplay[i]][6]);                         //alert('Length : ' + iStart + ', Row No : ' + i + ', Share : ' + aaData[aiDisplay[i]][6] + 'Total : ' + iPageMarket);                     }                     / Modify the footer row to match what we want /                     var nCells = nRow.getElementsByTagName('td');                     nCells[0].innerHTML = 'Total : '+parseInt(iPageMarket * 100) / 100 + '% (' + parseInt(iTotalMarket * 100) / 100 + '% Grand Total)';                 } // End of Footer Footer             });             // ----- Header Buttons ---------             $('<a id="btnDelete" style="padding: 0px; display:none;" class="ui-button ui-widget ui-state-default ui-corner-all'             + 'ui-button-text-only" href="javascript:void(0)"><span style="font-size: small; padding: 2px 5px;"'             + 'class="ui-button-text"> Delete selected Row</span></a>&nbsp;&nbsp;<button id="refresh">Refresh</button>').appendTo('div.dataTables_length');             //$('<button id="refresh">Refresh</button>').appendTo('div.dataTables_length'); //ReFresh Button             $("table#GridView1").dataTable().columnFilter(                 {                     //sPlaceHolder: "foot:before",                     "aoColumns": [                                     null, //{ "type": "number-range" },                                     {"type": "text", width: "50px" },                                     { "type": "select" },                                     { "type": "text" }, //null, //{ "type": "date-range", width: "50px" },                                     {"type": "number-range", width: "50px" },                                     { "type": "select" },                                     { "type": "select" },                                     { "type": "date-range"}   //{ "type": "date-range", width: "50px" },                                     ]                 });             // -----------  Fixed Header   ----------- //            oTable = $('#GridView1').dataTable(); //            new FixedHeader(oTable);             //$('#GridView1 div.title').text("This is a table title");             / Add a click handler to the rows - this could be used as a callback /             $("#GridView1 tbody tr").click(function (e) {                 if ($(this).hasClass('row_selected')) {                     $(this).removeClass('row_selected');                     $('#btnDelete').hide();                 }                 else {                     oTable.$('tr.row_selected').removeClass('row_selected');                     $(this).addClass('row_selected');                     $('#btnDelete').show();                 }             });             / Add a click handler for the delete row /             $('#btnDelete').click(function () {                 var anSelected = fnGetSelected(oTable);                 if (anSelected.length !== 0) {                     / Nedd Ajax Call To perform in serverSide/                     if (confirm('Are you sure you wish to delete this row?')) {                         / do the delete /                         oTable.fnDeleteRow(anSelected[0]);                     }                     else {                         $("#GridView1 tbody tr").removeClass('row_selected');                         $('#btnDelete').hide();                     }                 }             });             $.fn.dataTableExt.oStdClasses["filterColumn"] = "my-style-class";             $('#GridView1 tbody td').click(function () {                 / Get the position of the current data from the node /                 var aPos = oTable.fnGetPosition(this);                 var aData = oTable.fnGetData(aPos[0]);                 //alert(aData[0]);             });             / Init the table /             oTable = $('#GridView1').dataTable();         });         function fnGetSelected(oTableLocal) {             return oTableLocal.$('tr.row_selected');         }         //$("div.tools").html('Organize by: <select id="booking_status"><option value="">All Bookings</option><option value="confirmed">Upcoming</option><option value="arrived">Arrived</option><option value="rejected">Rejected</option></select>');     </script>      </head> <body>     <form id="form1" runat="server">      <div class="Shadow">         <asp:GridView ID="GridView1" runat="server" OnPreRender="GridView1_PreRender" 
ShowFooter="true" AutoGenerateColumns="false">             <Columns>                                 <asp:TemplateField HeaderText="S.No">                     <ItemTemplate>                         <%# Eval("id") %>                     </ItemTemplate>                 </asp:TemplateField>                                 <asp:TemplateField HeaderText="Engine">                     <ItemTemplate>                         <%# Eval("engine")%>                     </ItemTemplate>                 </asp:TemplateField>                                 <asp:TemplateField HeaderText="Browser">                     <ItemTemplate>                         <%# Eval("browser")%>                     </ItemTemplate>                 </asp:TemplateField>                                 <asp:TemplateField HeaderText="Platform">                     <ItemTemplate>                         <%# Eval("platform")%>                     </ItemTemplate>                 </asp:TemplateField>                                 <asp:TemplateField HeaderText="Version">                     <ItemTemplate>                         <%# Eval("version")%>                     </ItemTemplate>                 </asp:TemplateField>                                 <asp:TemplateField HeaderText="Grade">                     <ItemTemplate>                         <%# Eval("grade")%>                     </ItemTemplate>                 </asp:TemplateField>                                 <asp:TemplateField HeaderText="Market Share">                     <ItemTemplate>                         <%# Eval("marketshare")%>                     </ItemTemplate>                 </asp:TemplateField>                                 <asp:TemplateField HeaderText="Date">                     <ItemTemplate>                         <%# Eval("RDate")%>                     </ItemTemplate>                                    </asp:TemplateField>             </Columns>         </asp:GridView>     </div>     </form> </body> </html>

Code Behind :

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; namespace DT_Pagination {     public partial class GridTable : System.Web.UI.Page     {         protected void Page_Load(object sender, EventArgs e)         { string strConnect = "server=.\\MYDATABASE; user id=sa; pwd=***; database=aspdotnetDB;";             DataSet dataset = new DataSet(); SqlDataAdapter da = new SqlDataAdapter("select *,convert(varchar(10),released,103) as RDate from ajax", strConnect);             da.Fill(dataset, "ajax");             GridView1.DataSource = dataset;             GridView1.DataBind();         }         protected void GridView1_PreRender(object sender, EventArgs e)         {             GridView1.UseAccessibleHeader = false;             GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;             GridView1.FooterRow.TableSection = TableRowSection.TableFooter;             int CellCount = GridView1.FooterRow.Cells.Count;             GridView1.FooterRow.Cells.Clear();             GridView1.FooterRow.Cells.Add(new TableCell());             GridView1.FooterRow.Cells[0].ColumnSpan = CellCount - 1;             GridView1.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Right;             GridView1.FooterRow.Cells.Add(new TableCell());             TableFooterRow tfr = new TableFooterRow();             for (int i = 0; i < CellCount; i++)             {                 tfr.Cells.Add(new TableCell());                 //tfr.Cells[i].i                 //tfr.Cells[i].ColumnSpan = CellCount;                 //tfr.Cells[0].Text = "Footer 2";             }             GridView1.FooterRow.Controls[1].Controls.Add(tfr);         }     } }
1
Add a comment...

asp.net Tutorial

Shared publicly  - 
 
Sunday, December 16, 2012
How solve Unexpected Logout issues
When you set the Session TimeOut to 20, you would expect the Session to expire after 20 minutes of inactivity. However, you're using Session State Mode InProc (the default value), which means that the SessionState is stored in memory. When the Application Pool recycles. all Sessions stored in Memory will be lost. There can be many reasons why the Application Pool recycles.

http://blogs.msdn.com/b/johan/archive/2007/05/16/common-reasons-why-your-application-pool-may-unexpectedly-recycle.aspx

Also, in a shared hosted environment, Application Pools recycles frequently. To overcome both problems, you should consider to use another SessionState Mode:

http://msdn.microsoft.com/en-us/library/ms178586(v=vs.100).aspx

But this has nothing to do with authentication, as already stated! When you set the forms authentication to 20 minutes, it means that the user will be logged out anywhere between 10 to 20 minutes of inactivity. This is because the authentication ticket is only reset after more than half of the timeout has expired.

http://msdn.microsoft.com/en-us/library/system.web.configuration.formsauthenticationconfiguration.slidingexpiration.aspx

But sometimes the authentication ticket seems to expire unexpectedly also, forcing the user to the login page.. To understand why this happens, you need to understand how authentication works.

When you login, an authentication ticket is created in a cookie. By default, this authentication ticket encrypted using the machinekey section in web.config. When this section is not specified in web.config, ASP.NET will generate one for you. If the application pool recycles, sometimes ASP.NET will generate a new machinekey (although MSDN says different!) especially in shared hosted environment. But with this new key, the authentication ticket cannot be decrypted anymore, so the user is redirected to the login page. To overcome this, simply add a machinekey section in your web.config, so the same key is used on each and every request:

http://www.developmentnow.com/articles/machinekey_generator.aspx
1
Add a comment...

asp.net Tutorial

Shared publicly  - 
 
Total sum in Gridview Footer in Asp.net C#
Using Jquery

var result = from p in st.prices select new { p.id, p.Name, p.price1 }; GridView1.DataSource = result; GridView1.DataBind(); double sum=0; foreach(var item in result){   sum += Convert.ToDouble(item.price1); } Label lblsum = (Label)GridView1.FooterRow.FindControl("Lbltotal"); lblsum.Text = sum.ToString();



1
Add a comment...

asp.net Tutorial

Shared publicly  - 
 
Sending HTML formatted mails

Sending rich and colorful  emails with your own logos and banners is not a very difficult task if you know how to send HTML formatted mails through .NET
 Follow the steps below::
 
Step1:Simply design the format u want to send in html. Add the images banners etc that you want to add.
 
Step2: import the namespace using System.Net.Mail; in your .cs page
 
Step3: append the html table in string like following and send mail with allowing html in body
 
public string fnProjectRoot(string PATH_INFO)
       {
         string rootProject = "";
         string[] tmpStr = PATH_INFO.Split('/');
         rootProject = tmpStr[1];
         return "/" + rootProject + "/";
        }
public void sbDoMail()
        {
          string currentPath = "http://" + Request.ServerVariables["HTTP_HOST"] + fnProjectRoot(Request.ServerVariables["PATH_INFO"]);
          StringBuilder confirmMail = null;
          confirmMail.Append( "<table style='width: 100%; position: static; height: 100%'>" );
         confirmMail.Append( "<tr>" );
         confirmMail.Append( "<td style='width: 29px; height: 21px'>" );
         confirmMail.Append( " </td>" );
         confirmMail.Append( "<td class='smallbluetext1' style='height: 21px; text-decoration: underline'>" );
         confirmMail.Append( "<p style='background-color: silver'>" );
         confirmMail.Append( "Welcome and email validation email:</p>" );
         confirmMail.Append( "</td>" );
         confirmMail.Append( "<td>" );
         confirmMail.Append( " </td>" );
         confirmMail.Append( "</tr>" );
         confirmMail.Append( "<tr>" );
         confirmMail.Append( "<td>" );
         confirmMail.Append( "</td>" );
        confirmMail.Append( "<td>" );
        confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt'>" );
        confirmMail.Append( "<img alt='' border='0' src="+ currentPath+"MFlogo[1].gif/>&nbsp;</p>" );
        confirmMail.Append( "</td>" );
        confirmMail.Append( "<td>" );
        confirmMail.Append( "</td>" );
        confirmMail.Append( "</tr>" );
        confirmMail.Append( "<tr>" );
        confirmMail.Append( "<td>" );
        confirmMail.Append( "</td>" );
        confirmMail.Append( "<td>" );
        confirmMail.Append( "<span style='font-size: 12pt;" );
        confirmMail.Append( "" );
        confirmMail.Append( "</span></td>" );
        confirmMail.Append( " <td>" );
        confirmMail.Append( "</td>" );
        onfirmMail.Append( "</tr>" );
        confirmMail.Append( "<tr>" );
       confirmMail.Append( "<td >" );
                  confirmMail.Append( "</td>" );
       confirmMail.Append( "<td class='smallnormalbrowntext'>" );
       confirmMail.Append( "<p>" );
        confirmMail.Append( "Dear&nbsp; <span class='headline' style='color:Blue;'> NAME </span>,</p>" );
        confirmMail.Append( "<p>" );
        confirmMail.Append( "<?xml namespace='' ns='urn:schemas-microsoft-com:office:office' prefix='o' ?><?xml namespace='' prefix='O' ?><o:p></o:p>" );
        confirmMail.Append( "</p>" );
        confirmMail.Append( "<p>" );
        confirmMail.Append( "You are welcome " );
        confirmMail.Append( " We are sending this mail to validate" );
        confirmMail.Append( "your email address given.</p>" );
        confirmMail.Append( "<p>" );
       confirmMail.Append( "Our e-mail validation is intended to confirm that the email entered in your profile" );
        confirmMail.Append( "is authentic. This procedure adds credence to your contact information.</p>" );
        confirmMail.Append( "<p>" );
        confirmMail.Append( "" );
         confirmMail.Append( "</td>" );
        confirmMail.Append( "<td >" );
        confirmMail.Append( "</td>" );
        confirmMail.Append( "</tr>" );
        confirmMail.Append( "<tr>" );
        confirmMail.Append( " <td style='width: 29px; height: 92px'>" );
       confirmMail.Append( "</td>" );
       confirmMail.Append( "<td style='height: 92px'>" );
       confirmMail.Append( " <table class='smallnormalbrowntext'>" );
        confirmMail.Append( "<tr>" );
        confirmMail.Append( "<td style='width: 251px'>" );
        confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt'>" );
       confirmMail.Append( "<b style='<u>Login information<o:p></o:p></u></b></p>" );
       confirmMail.Append( "</td>" );
         confirmMail.Append( "<td rowspan='3' style='width: 52px'>" );
         confirmMail.Append( "</td>" );
         confirmMail.Append( "</tr>" );
         confirmMail.Append( "<tr>" );
         confirmMail.Append( "<td class='headline'>" );
         confirmMail.Append( "<span class='smallbluetext1' >Username:</span>" );
         confirmMail.Append( "</td>" );
         confirmMail.Append( "</tr>" );
         confirmMail.Append( "<tr>" );
        confirmMail.Append( "<td class='headline'>" );
       confirmMail.Append( "<span class='smallbluetext1'>Password:</span>" );
       confirmMail.Append( "" );
      confirmMail.Append( "</td>" );
     confirmMail.Append( "</tr>" );
     confirmMail.Append( "</table>" );
      confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt'>" );
      confirmMail.Append( "</td>" );
      confirmMail.Append( "<td style='width: 23px; color: #000000; height: 92px'>" );
      confirmMail.Append( "</td>" );
     confirmMail.Append( "</tr>" );
     confirmMail.Append( "<tr style='color: #000000'>" );
      confirmMail.Append( "<td style='width: 29px'>" );
      confirmMail.Append( "</td>" );
                  confirmMail.Append( "<td class='smallnormalbrowntext'>" );
        confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt'>" );
       confirmMail.Append( "With regards,</p>" );
       confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt'>" );
      confirmMail.Append( "<b style='<span style='color: #0000ff '>" );
     confirmMail.Append( "team<o:p></o:p></span></b></p>" );
     confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt'>" );
   confirmMail.Append( "<o:p></o:p>" );
   confirmMail.Append( "</p>" );
confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt'>" );
     confirmMail.Append( "<o:p></o:p>" );
      confirmMail.Append( "</p>" );
      confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt'>" );
       confirmMail.Append( "<span style='/span>Need Help ? Please write to us at" );
     confirmMail.Append( "" );
      confirmMail.Append( "</p>" );
       confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt'>" );
       confirmMail.Append( "<span style='&nbsp; </span>" );
        confirmMail.Append( "</p>" );
        confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt'>" );
       confirmMail.Append( "" );
       confirmMail.Append( "" );
       confirmMail.Append( "" );
        confirmMail.Append( "</p>" );
        confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt; text-indent: 0.5in'>" );
       confirmMail.Append( "" );
       confirmMail.Append( "</p>" );
       confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt'>" );
       confirmMail.Append( "<span style='</span>&nbsp;</p>" );
                  confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt'>" );
      confirmMail.Append( "" );
      confirmMail.Append( "</p>" );
      confirmMail.Append( "<p class='MsoNormal' style='margin: 0in 0in 0pt; text-indent: 0.5in'>" );
      confirmMail.Append( "" );
      confirmMail.Append( "</p>" );
      confirmMail.Append( "</td>" );
      confirmMail.Append( "<td style='width: 23px'>" );
      confirmMail.Append( "</td>" );
      confirmMail.Append( "</tr>" );
     confirmMail.Append( "</table>" );
                 
                  string add = "username@gmail.com";
                  try
                  {
                        System.Net.Mail.MailMessage mMailMessage = new System.Net.Mail.MailMessage();
                        SmtpClient mSmtpClient = new SmtpClient();
                        mSmtpClient.Host = "192.168.10.1";
                        mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

                        mMailMessage.From = new MailAddress("monalisab@mindfiresolutions.com");
                        mMailMessage.To.Add(new MailAddress(add));
                        mMailMessage.Subject = "Congratulations";
                        mMailMessage.Body = confirmMail.ToString();
                        mMailMessage.IsBodyHtml = true;//----------Line for sending html formatted mail
                        mSmtpClient.Send(mMailMessage);
                  }
                  catch (Exception ex)
                  {
                        Response.Write(ex);
                  }
            }
1
Add a comment...

asp.net Tutorial

Shared publicly  - 
 
Methods in Global.asax
This blog is intended to spread some light on the various methods which are available in global.asax file in ASP.NET. It’s very important to understand the methods in global.asax so that we as programmers can handle some application level events very efficiently. I said application level events and reason for using application word is that global.asax is an application level file and methods in it are used to handle application level events and these methods are not at all specific to any aspx page. Some of the common methods in the order in which they are executed are listed below
Application_Start
Application_BeginRequest
Application_AuthenticateRequest
Session_Start
Application_EndRequest
Session_End
Application_End
Application_Error
Now let’s see what is the major difference between these methods or events. Oh I forgot to say, these are actually events not methods which get raised when a particular event gets triggered. Before we see the various methods in Global.asax I would like to tell you that Global.asax is actually derived from a class called “HttpApplication”. The above listed methods are only a few methods which I am gonna talk about. The listing of other methods can be found at the end of the blog. Now lets see the above mentioned events one by one.

Application_Start
Application_Start event gets triggered only once during the life cycle of the application. This once happens when the first request for any resource in the application comes. Resource can be a page or an image in the application. When the very first request for a resource, say a web page, is made by a user “Application_Start” is triggered after which this event is not at all executed. If by any chance the server where the application is hosted is restarted then this event is fired once again i.e. when the very first request for any resource in the application is made after the server is reset.

Application_BeginRequest
“Application_BeginRequest” is the second event which gets fired after “Application_Start”. Unlike the “Application_Start”, “Application_BeginRequest” is triggered for each and every request which comes to the application. Since this method is fired for any request made to the application you can use this method to keep track of what and all resources are accessed through this method.

Application_AuthenticateRequest
“Application_AuthenticateRequest” is the next event in line which is triggered after “Application_BeginRequest” is triggered. “Application_AuthenticateRequest” is also fired for each and every request. This event can be used to write code in scenarios where you want to do something when the user is getting authenticated.

Session_Start
The next event in line which gets triggered after “Application_AuthenticateRequest” is “Session_Start”. Session start event is fired only when a new session for a user starts. Once “Session_Start” for a user is fired then if the user makes subsequent request to any resource within the application this event is not at all triggered. The event is triggered only when the user’s session expires and then the user tries to access any resource in the application again.
This event can be used when you want to do something when the user visits you site/application for the first time or when his session starts. This event doesn’t get triggered if you are not using sessions which can be disabled in the web.config.

Application_EndRequest
The next event in line which gets fired once the request for the user is processed is “Applicatin_EndRequest”. This event is the closing event of “Applicatin_BeginRequest”. This event is also fired for each and every request which comes for the application.

Session_End
The closing event of “Session_Start” event. Whenever a user’s session in the application expires this event gets fired. So anything you want to do when the user’s session expires you can write codes here. The session expiration time can be set in web.config file. By default session time out is set to 20 mins.

Application_End
The same as “Application_Start”, “Application_End” is executed only once, when the application is unloaded. This event is the end event of “Application_Start”. This event is normally fired when the application is taken offline or when the server is stopped.

Application_Error
Now we come to the last event mentioned in this blog and that is “Application_Error”. This event gets fired when any unhandled exception/error occurs anywhere in the application. Any unhandled here means exception which are not caught using try catch block. Also if you have custom errors enabled in your application i.e. in web.config file then the configuration in web.config takes precedence and all errors will be directed to the file mentioned in the tag.
Lets see with an e.g. how these events get fired.
Suppose “A”, “B” and “C” are users who are going to access a site named “My Site”. “A” is the very first user to visit “My Site” and he/she is accessing “productlist.aspx” page. At this time the flow of the request is as follows. The “Application_Start” event is triggered, since “A” is the very first user to visit the application, after this “Application_BeginRequest”, then “Application_AuthenticateRequest”, then “Session_Start”, “productlist.aspx” page level events are processed and then “Application_EndRequest” event is triggered. After accessing “productlist.aspx” if “A” access some other page then for those page request the flow will be first “Application_BeginRequest”, “Application_AuthenticateRequest” then the page processing (page level events) and then “Application_EndRequest”. For every subsequent request this pattern is followed.

When “B” accesses some resource in the site, say “default.aspx”, then first “Applicatin_BeginRequest”, second “Application_AuthenticateRequest”, third “Session_Start” then “default.aspx” page level events are executed and after that “Application_EndRequest” is executed. After accessing “default.aspx” “B” access “productlist.aspx” then first “Application_BeginRequest”, second “Application_AuthenticateRequest” then “productlist.aspx” and then “Application_EndRequest” event is triggered. He refreshes the page the same events are executed in the same order.
The above same process is repeated for “C” also.
Suppose you have an unhandled exception and you don’t have custom errors enabled in web.config then when a user accesses a resource the flow will be first “Application_BeginRequest”, “Application_AuthenticateRequest”, page level event and an error occurs in the page then it goes to “Application_Error” after that “Application_EndRequest”.
The order mentioned above is how the events are triggered. So with this I hope you would have got a clear idea on how these events are triggered.
Some other events which are part of the HttpApplication class are as follows
PostAuthenticateRequest
AuthorizeRequest
PostAuthorizeRequest
ResolveRequestCache
PostResolveRequestCache
PostMapRequestHandler
AcquireRequestState
PostAcquireRequestState
PreRequestHandlerExecute
PostRequestHandlerExecute
ReleaseRequestState
PostReleaseRequestState
UpdateRequestCache
PostUpdateRequestCache
LogRequest. (Supported in IIS 7.0 only.)
PostLogRequest (Supported in IIS 7.0 only.)
1
Add a comment...
In their circles
14 people
Have them in circles
132 people
ASP.NET SOLUTIONS's profile photo
cliff edghill's profile photo
DNN Corp.'s profile photo
George Parker's profile photo
Martin Lucarini's profile photo
Ranga Rajesh Kumar's profile photo
James Nagy's profile photo
木头人's profile photo
Haythem Hassan's profile photo

asp.net Tutorial

Shared publicly  - 
1
Add a comment...

asp.net Tutorial

Shared publicly  - 
 
Test the WebService by passing the parameter value in the URL
1
Add a comment...

asp.net Tutorial

Shared publicly  - 
 
Total sum in Gridview Footer in Asp.net C#
Using C#

protected void grd_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { // GET THE RUNNING TOTAL OF PRICE FOR EACH PAGE. if (e.Row.RowType == DataControlRowType.DataRow) { Label lblPgTotal = (Label)e.Row.FindControl("lblTotalPrice"); dPageTotal += Decimal.Parse(lblPgTotal.Text); }
1
Add a comment...

asp.net Tutorial

Shared publicly  - 
 
How to know which control has triggered the Post back event.


There may be scenarios where we require to know which control has triggered the postback event both at client-side and server-side.
 
For all the controls except button and Imagebutton
Below are two hidden fields added by Asp.net.
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
"__EVENTTARGET" value is set to the control id who has triggered the postback. This happens for all the controls except button and Imagebutton.
At client side, on body onbeforeunload event, check the value of "__EVENTTARGET" , this will return the control ID of the control which has triggered the postback.

document.getElementById('__EVENTTARGET').value;
At server side, on Page_load check the control which has caused the postback.
string controlName = Request.Params.Get("__EVENTTARGET");
For Buttons and Imagebuttons
For Buttons and ImageButton, the "__EVENTTARGET" is set to empty string.
In this case, we need to check the data posted to server ( key/value pair).
In postbacks triggered by other controls except button and Image button, the key-value pair does not contains the buttonid and its value.
But if the postback is triggered by a button/imagebutton in this case the posted data has the buttonID and value it empty string.
At server side check the "__EVENTTARGET", If its empty and check the control type to know which button has triggered the postback
foreach (string controlID in Request.Form) {   Control objControl = Page.FindControl(controlID);   if (objControl is Button)   {     control = objControl;     break;   } }
Explanation:
In ASP.net, all the controls (except buttons and imagebuttons) uses javascript function  __doPostBack to trigger postback.
If the web page has any control whose AutoPostBack Property is set to true then the below is the code snippet added to page by ASP.net
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['Form1'];
if (!theForm) {
    theForm = document.Form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>
The Button controls and ImageButton controls does not call the __doPostBack function. Because of this, the _EVENTTARGET will always be empty.
When the user clicks on the "Submit" button, the content of the form is sent to the server. The form's action attribute defines the name of the file to send the content to.
The form data (key-value pair) sent to the server has the button control id that has caused the triggered postback.
1
Add a comment...

asp.net Tutorial

Shared publicly  - 
 
Exploring Session in ASP.NET

Download sample application - 2.85 KB
Table of Contents
Introduction
What is Session?
Advantages and disadvantages of Session
Storing and retrieving values from Session
Session ID
Session Mode and State Provider
Session State
Session Event
Session Mode
InProc Session Mode
Overview of InProc session mode
When should we use InProc session mode?
Advantages and disadvantages
StateServer Session Mode
Overview of StateServer session mode
Configuration for StateServer session mode
How StateServer session mode works
Example of StateServer session mode
Advantages and Disadvantages
SQL Server Session Mode
How SQLServer session mode works
When should we use SQL Server session mode?
Configuration for SQL Server session mode
Advantages and disadvantages
Custom Session Mode
How Custom session mode works?
When should we use Custom session mode?
Configuration for Custom session mode
Advantages and disadvantages
Overview of production deployment
Application Pool
Identity of Application Pool
Creating and assigning Application Pool
Web Garden
How to create a web garden
How Session depends on a web garden
Web Farm and Load Balancer
Handling Session in web farm and load balancer scenarios
Session and Cookies
What is Cookie-Munging?
How Cookie-Munging works in ASP.NET
Removing Session
Enabling and disabling Session
Summary
Introduction
First of all, I would like to thank all the readers who have read and voted for my articles. In the Beginner's Guide series, I have written some articles on state management. Probably this is my last article on state management.
This article will give you a very good understanding of session. In this article, I have covered the basics of session, different ways of storing session objects, session behavior in web farm scenarios, session on Load Balancer, etc. I have also explained details of session behavior in a live production environment. Hope you will enjoy this article and provide your valuable suggestions and feedback.
What is Session?
Web is stateless, which means a new instance of a web page class is re-created each time the page is posted to the server. As we all know, HTTP is a stateless protocol, it can't hold client information on a page. If the user inserts some information and move to the next page, that data will be lost and the user would not be able to retrieve that information. What do we need here? We need to store information. Session provides a facility to store information on server memory. It can support any type of object to store along with our own custom objects. For every client, session data is stored separately, which means session data is stored on a per client basis. Have a look at the following diagram:
Fig: For every client, session data is stored separately
State management using session is one of the best ASP.NET features, because it is secure, transparent from users, and we can store any kind of object in it. Along with these advantages, some times session can cause performance issues in high traffic sites because it is stored in server memory and clients read data from the server. Now let's have a look at the advantages and disadvantages of using session in our web applications.
Advantages and disadvantages of Session?
Following are the basic advantages and disadvantages of using session. I have describe in details with each type of session at later point of time.
Advantages:
It helps maintain user state and data all over the application.
It is easy to implement and we can store any kind of object.
Stores client data separately.
Session is secure and transparent from the user.
Disadvantages:
Performance overhead in case of large volumes of data/user, because session data is stored in server memory.
Overhead involved in serializing and de-serializing session data, because in the case of StateServer and SQLServer session modes, we need to serialize the objects before storing them.
Besides these, there are many advantages and disadvantages of session that are based on the session type. I have discussed all of them in the respective sections below.
Storing and retrieving values from Session
Storing and retrieving values in session are quite similar to that in ViewState. We can interact with session state with the System.Web.SessionState.HttpSessionState class, because this provides the built-in session object in ASP.NET pages.
The following code is used for storing a value to session:
Collapse | Copy Code
//Storing UserName in Session Session["UserName"] = txtUser.Text;
Now, let's see how we can retrieve values from session:
Collapse | Copy Code
//Check weather session variable null or not if (Session["UserName"] != null) { //Retrieving UserName from Session lblWelcome.Text = "Welcome : " + Session["UserName"]; } else { //Do Something else }
We can also store other objects in session. The following example shows how to store a DataSet in session.
Collapse | Copy Code
//Storing dataset on Session Session["DataSet"] = _objDataSet;
The following code shows how we to retrieve that DataSet from session:
Collapse | Copy Code
//Check weather session variable null or not if (Session["DataSet"] != null) { //Retrieving UserName from Session DataSet _MyDs = (DataSet)Session["DataSet"]; } else { //Do Something else }
References:
MSDN (read the session variable section)
Session ID
ASP.NET uses an 120 bit identifier to track each session. This is secure enough and can't be reverse engineered. When a client communicates with a server, only the session ID is transmitted between them. When the client requests for data, ASP.NET looks for the session ID and retrieves the corresponding data. This is done in the following steps:
Client hits the web site and information is stored in the session.
Server creates a unique session ID for that client and stores it in the Session State Provider.
The client requests for some information with the unique session ID from the server.
Server looks in the Session Providers and retrieves the serialized data from the state server and type casts the object.
Take a look at the the pictorial flow:
Fig: Communication of client, web server, and State Provider
References:
SessionID in MSDN
Session Mode and State Provider
In ASP.NET, there are the following session modes available:
InProc
StateServer
SQLServer
Custom
For every session state, there is a Session Provider. The following diagram will show you how they are related:
Fig: Session state architecture
We can choose the session state provider based on which session state we are selecting. When ASP.NET requests for information based on the session ID, the session state and its corresponding provider are responsible for sending the proper information. The following table shows the session mode along with the provider name:
Apart from that, there is another mode Off. If we select this option, the session will be disabled for the application. But our objective is to use session, so we will look into the above four session state modes.
Session States
Session state essentially means all the settings that you have made for your web application for maintaining the session. Session State itself is a big thing. It says all about your session configuration, either in the web.config or from the code-behind. In the web.config, <SessionState> elements are used for setting the configuration of the session. Some of them are Mode, Timeout, StateConnectionString, CustomProvider, etc. I have discussed about each and every section of the connection string. Before I discuss Session Mode, take a brief overview of session events.
Session Event
There are two types of session events available in ASP.NET:
Session_Start
Session_End
You can handle both these events in the global.asax file of your web application. When a new session initiates, the session_start event is raised, and the Session_End event raised when a session is abandoned or expires.
Collapse | Copy Code
void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started } void Session_End(object sender, EventArgs e) { // Code that runs when a session ends. }
References:
Application and Session Events
Session Mode
I have already discussed about session modes in ASP.NET. Following are the different types of session modes available in ASP.NET:
Off
InProc
StateServer
SQLServer
Custom
If we set session Mode="off" in web.config, session will be disabled in the application. For this, we need to configure web.config the following way:
InProc Session Mode
This is the default session mode in ASP.NET. Its stores session information in the current Application Domain. This is the best session mode for web application performance. But the main disadvantage is that, it will lose data if we restart the server. There are some more advantages and disadvantages of the InProc session mode. I will come to those points later on.
Overview of InProc session mode
As I have already discussed, in InProc mode, session data will be stored on the current application domain. So it is easily and quickly available.
InProc session mode stores its session data in a memory object on the application domain. This is handled by a worker process in the application pool. So if we restart the server, we will lose the session data. If the client request for data, the state provider read the data from an in-memory object and returns it to the client. In web.config, we have to mention the session mode and also set the timeout.
The above session timeout setting keeps the session alive for 30 minute. This is configurable from the code-behind too.
Collapse | Copy Code
Session.TimeOut=30;
There are two types of session events available in ASP.NET: Session_Start() and Session_End and this is the only mode that supports the Session_End() event. This event is called after the session timeout period is over. The general flow for the InProc session state is like this:
When the Session_End() is called depends on the session timeout. This is a very fast mechanism because no serialization occurs for storing and retrieving data, and data stays inside the same application domain.
When should we use the InProc session mode?
InProc is the default session mode. It can be very helpful for a small web site and where the number of users is very less. We should avoid InProc in a Web Garden scenario (I will come to this topic later on).
Advantages and disadvantages
Advantages:
It store session data in a memory object of the current application domain. So accessing data is very fast and data is easily available.
There is not requirement of serialization to store data in InProc session mode.
Implementation is very easy, similar to using the ViewState.
Disadvantages:
Although InProc session is the fastest, common, and default mechanism, it has a lot of limitations:
If the worker process or application domain is recycled, all session data will be lost.
Though it is the fastest, more session data and more users can affect performance, because of memory usage.
We can't use it in web garden scenarios.
This session mode is not suitable for web farm scenarios.
As per the above discussion, we can conclude that InProc is a very fast session storing mechanism but suitable only for small web applications. InProc session data will get lost if we restart the server, or if the application domain is recycled. It is also not suitable for Web Farm and Web Garden scenarios.
Now we will have a look the other options available to overcome these problems. First comes the StateServer mode.
StateServer Session Mode
Overview of StateServer session mode
This is also called Out-Proc session mode. StateServer uses a stand-alone Windows Service which is independent of IIS and can also be run on a separate server. This session state is totally managed by aspnet_state.exe. This server may run on the same system, but it's outside of the main application domain where your web application is running. This means if you restart your ASP.NET process, your session data will still be alive. This approaches has several disadvantages due to the overhead of the serialization and de-serialization involved, it also increases the cost of data access because every time the user retrieves session data, our application hits a different process.
Configuration for StateServer session mode
In StateServer mode, session data is stored in a separate server which is independent of IIS and it is handled by aspnet_state.exe. This process is run as a Windows Service. You can start this service from the Windows MMC or from the command prompt.
By default, the "Startup Type" of the ASP.NET state service is set to Manual; we have to set it to Automatic.
From the command prompt, just type "net start aspnet_state". By default, this service listens to TCP port 42424, but we can change the port from the Registry editor as show in the picture below:
Now have a look at the web.config configuration for the StateServer setting. For the StateServer setting, we need to specify the stateConnectionString. This will identify the system that is running the state server. By default, stateConnectionString used the IP 127.0.0.1 (localhost) and port 42424.
When we are using StateServer, we can configure the stateNetworkTimeOut attribute to specify the maximum number of seconds to wait for the service to respond before canceling the request. The default timeout value is 10 seconds.
For using StateServer, the object which we are going to store should be serialized, and at the time of retrieving, we need to de-serialize it back. I have described this below with an example.
How the StateServer Session Mode works
We use the StateServer session mode to avoid unnecessary session data loss when restarting our web server. StateServer is maintained by the aspnet_state.exe process as a Windows service. This process maintains all the session data. But we need to serialize the data before storing it in StateServer session mode.
As shown in the above figure, when the client sends a request to the web server, the web server stores the session data on the state server. The StateServer may be the current system or a different system. But it will be totally independent of IIS. The destination of the StateServer will depend on the web.config stateConnectionString setting. If we set it to 127.0.0.1:42424, it will store data in the local system itself. For changing the StateServer destination, we need to change the IP, and make sure aspnet_state.exe is up and running on that system. Otherwise you will get the following exception while trying to store data on session.
When we are storing an object on session, it should be serialized. That data will be stored in the StateServer system using the State Provider. And at the time of retrieving the data, the State Provider will return the data. The complete flow is given in the picture below:
Example of StateServer Session Mode
Here is a simple example of using the StateServer session mode. I have created this sample web application directly on IIS so that we can easily understand its usage.
Step 1: Open Visual Studio > File > New > Web Sites. Choose Location as HTTP and create the web application.
Now if you open IIS, you will see a virtual directory created with the name of your web application, in my case it is StateServer.
Step 2: Create s simple UI that will take the roll number and the name of a student. We will store the name and roll number in a state server session. I have also created a class StudentInfo. This class is listed below:
Collapse | Copy Code
[Serializable] public class StudentInfo { //Default Constructor public StudentInfo() { } /// <summary> /// Create object of student Class /// </summary> /// <param name="intRoll">Int RollNumber</param> /// <param name="strName">String Name</param> public StudentInfo(int intRoll, string strName) { this.Roll = intRoll; this.Name = strName; } private int intRoll; private string strName; public int Roll { get { return intRoll; } set { intRoll = value; } } public string Name { get { return strName; } set { strName = value; } } }
Now have a look at the code-behind. I have added two buttons: one for storing session and another for retrieving session.
Collapse | Copy Code
protected void btnSubmit_Click(object sender, EventArgs e) { StudentInfo _objStudentInfo = new StudentInfo(Int32.Parse( txtRoll.Text) ,txtUserName.Text); Session["objStudentInfo"] = _objStudentInfo; ResetField(); } protected void btnRestore_Click(object sender, EventArgs e) { StudentInfo _objStudentInfo = (StudentInfo) Session["objStudentInfo"]; txtRoll.Text = _objStudentInfo.Roll.ToString(); txtUserName.Text = _objStudentInfo.Name; }
Step 3: Configure your web.config for state server as I have already explained. And please make sure aspnet_state.exe is up and running on the configured server.
Step 4: Run the application.
Enter the data, click on Submit.
There are the following tests that I have made which will totally explain how exactly StateServer is useful.
First: Remove the [Serializable ] keyword from the StudentInfo class and try to run the application. When you click on the Submit button, you will get the following error:
Which clearly says that you have to serialize the object before storing it.
Second: Run the application, store data by clicking on the Submit button. Restart IIS.
In the case of InProc, you will have already lost your session data, but with StateServer, click on Restore Session and you will get your original data, because StateServer data does not depend on IIS. It keeps it separately.
Third: Stop aspnet_state.exe from the Windows Services MMC and submit the data. You will get the following error:
because your State Server process is not running. So please keep in mind these three points when using StateServer mode.
Advantages and Disadvantages
Based on the above discussion:
Advantages:
It keeps data separate from IIS so any issues with IIS will not hamper session data.
It is useful in web farm and web garden scenarios.
Disadvantages:
Process is slow due to serialization and de-serialization.
State Server always needs to be up and running.
I am stopping here on StateServer, you will find some more interesting points on it in the Load Balancer, Web Farm, and Web Garden section.
References:
State Server Session Mode
ASP.NET Session State
SQLServer Session Mode
Overview of SQL Server Session Mode
This session mode provide us more secure and reliable session management in ASP.NET. In this session mode, session data is serialized and stored in A SQL Server database. The main disadvantage of this session storage method is the overhead related with data serialization and de-serialization. It is the best option for using in web farms though.
To setup SQL Server, we need these SQL scripts:
For installing: InstallSqlState.sql
For uninstalling: UninstallSQLState.sql
The easiest way to configure SQL Server is using the aspnet_regsql command.
I have explained in detail the use of these files in the configuration section. This is the most useful state management in web farm scenarios.
When should we use SQLServer Session Mode?
SQL Server session mode is a more reliable and secure session state management.
It keeps data in a centralized location (database).
We should use the SQLServer session mode when we need to implement session with more security.
If there happens to be frequent server restarts, this is an ideal choice.
This is the perfect mode for web farm and web garden scenarios (I have explained this in detail later).
We can use SQLServer session mode when we need to share session between two different applications.
Configuration for SQLServer Session Mode
In SQLServer session mode, we store session data in SQL Server, so we need to first provide the database connection string in web.config. The sqlConnectionString attribute is used for this.
After we setup the connection string, we need to configure the SQL Server. I will now explain how to configure your your SQL Server using the aspnet_regsql command.
Step 1: From command prompt, go to your Framework version directory. E.g.: c:\windows\microsoft.net\framework\<version>.
Step 2: Run the aspnet_regsql command with the following parameters:
Have a look at the parameters and their uses:
After you run the command, you will get the following message:
That's all.
Step 3: Open SQL Server Management Studio, check if a new database ASPState has been created, and there should be two tables:
ASPStateTempApplications
ASPStateTempSessions
Change the configuration string of the StateServer example and run the same sample application.
Just store the roll number and user name and click on the Submit button. Open the ASPStateTempSessions table from SQL Server Management Studio.. here is your session data:
Now do the following test that I have already explained in the StateServer mode section:
Remove the Serialize keyword from the StydentInfo class
Reset IIS and click on Restore Session
Stop SQL Server Services
I think I have explained the SQLServer session mode well.
Advantages and Disadvantages
Advantages:
Session data not affected if we restart IIS.
The most reliable and secure session management.
It keeps data located centrally, is easily accessible from other applications.
Very useful in web farms and web garden scenarios.
Disadvantages:
Processing is very slow in nature.
Object serialization and de-serialization creates overhead for the application.
As the session data is handled in a different server, we have to take care of SQL Server. It should be always up and running.
References:
Read more about SQLServer mode
Custom Session Mode
Overview of Custom Session Mode
Commonly we use the InProc, StateServer, or SQLServer session modes for our application, but we also need to know the fundamentals of the Custom session mode. This session mode is quite interesting, because Custom session gives full control to us to create everything, even the session ID. You can write your own algorithm to generate session IDs.
You can implement custom providers that store session data in other storage mechanisms simply by deriving from the SessionStateStoreProviderBase class. You can also generate a new session ID by implementing ISessionIDManager.
These are the methods called during the implementation of Custom session:
In the Initialize method, we can set a custom provider. This will initialize the connection with that provider. SetItemExpireCallback is used to set SessionTimeOut. We can register a method that will be called at the time of session expiration. InitializeRequest is called on every request and CreateNewStoreData is used to create a new instance of SessionStateStoreData.
When should we use Custom Session Mode?
We can use Custom session mode in the following cases:
We want to store session data in a place other than SQL Server.
When we have to use an existing table to store session data.
When we need to create our own session ID.
What configuration do we need for it?
We need to configure our web.config like this:
If you want to explore this more, please check the References section.
Advantages and Disadvantages
Advantages:
We can use an existing table for storing session data. This is useful when we have to use an existing database.
It's not dependent on IIS, so restarting the web server does not have any effect on session data.
We can crate our own algorithm for generating session ID.
Disadvantages:
Processing of data is very slow.
Creating a custom state provider is a low-level task that needs to be handled carefully to ensure security.
It is always recommended to use a third party provider rather than create your own.
References:
Custom Mode
Overview of production deployment
Production environments are where we deploy our applications on a live production server. It is a major and big challenge for web developers to deploy their applications on a live server, because in a big production environment, there are a large number of users and it is hard to handle the load for so many users with a single server. Here comes in the concepts of Web Farm, Load Balancer, Web Garden, etc.
Just a few months back, I deployed a web application in a live production environment which is accessed by millions of user and there were more than 10 Active Directory instances, more than 10 web servers over a Load Balancer, and several database server, Exchange Server, LCS Server, etc. The major risk involved in multiple servers is session management. The following picture shows a general diagram of production environments:
I will try to explain the different scenarios that you need to keep in mind while deploying your application.
Application Pool
This is one of the most important things you should create for your applications in a production environment. Application pools are used to separate sets of IIS worker processes that share the same configuration. Application pools enable us to isolate our web application for better security, reliability, and availability. The worker process serves as the process boundary that separates each application pool so that when one worker process or application has an issue or is recycled, other applications or worker processes are not affected.
Identity of Application Pool
Application pool identity configuration is an important aspect of security in IIS 6.0 and IIS 7.0, because it determines the identity of the worker process when the process is accessing a resource. In IIS 7.0, there are three predefined identities that are the same as in IIS 6.0.
Creating and assigning Application Pool
Open IIS Console, right click on Application Pool Folder > Create New.
Give the Application Pool ID and click OK.
Now, right click on the Virtual Directory (I am using StateServer web sites) and assign StateServerAppPool to the StateServer Virtual Directory.
So this StateServer web site will run independently with StateServerAppPool. Any problem related with other applications will not affect this application. This is the main advantage of creating application pools separately.
Web Garden
By default, each application pool runs with a single worker process (W3Wp.exe). We can assign multiple worker processes with a single application pool. An application pool with multiple worker processes is called a Web Garden. Many worker processes with the same Application Pool can sometimes provide better throughput performance and application response time. And each worker process should have its own Thread and memory space.
As shown in the picture, in IIS, there may be multiple application pools and each application pool will have at least one worker process. A Web Garden should contain multiple worker processes.
There are certain restrictions in using a Web Garden with your web application. If we use the InProc session mode, our application will not work correctly because the session will be handled by a different worker process. To avoid this problem, we should use the OutProc session mode and we can use a session state server or SQL-Server session state.
Main advantage: The worker processes in a Web Garden share the requests that arrive for that particular application pool. If a worker process fails, another worker process can continue processing the requests.
How to Create a Web Garden?
Right click on Application Pool > Go to Performance tab > Check Web Garden section (highlighted in picture):
By default, it would be 1. Just change it to more than one.
How Session depends on Web Garden?
I have already explained that InProc is handled by a worker process. It keeps data inside its memory object. Now if we have multiple worker processes, then it would be very difficult to handle the session because each and every worker process has its own memory, so if my first request goes to WP1 and it keeps my session data and the second request goes to WP2, I am trying to retrieve session data and it will not be available, which will throw an error. So please avoid Web Gardens in InProc session mode.
We can use StateServer or SQLServer session modes in Web Gardens because as I have already explained, these two session modes do not depend on worker processes. In my example, I have also explained that if you restart IIS, you are still able to access your session data.
In short:
Web Farm and Load Balancer
This is the most common terms that are used in production deployments. These terms come in when we are using multiple web servers for deploying our applications. The main reason for using these is we have to distribute the load over multiple servers. A Load Balancer is used to distribute the load on multiple servers.
If we take a look at the above diagram, the client request the URL and it will hit a Load Balancer, which decides which server to access. The load balancer will distribute the traffic over all the different web servers.
Now how does this affect Session?
Handling Session in web farm and load balancer scenarios
Handling session is one of the most challenging jobs in a web farm.
InProc: In InProc session mode, session data is stored in an in-memory object of the worker process. Each server will have its own worker process and will keep session data inside its memory.
If one server is down, and if the request goes to a different server, the user is not able to get session data. So it is not recommended to use InProc in Web Farms.
StateServer: I have already explained what a state server is and how to configure a state server, etc. For web farm scenarios, you can easily understand how much this is important because all session data will be stored in a single location.
Remember, in a web farm, you have to make sure you have the same <machinekey> in all your web servers. Other things are the same as I have describe earlier. All web.config files will have the same configuration (stateConnectionString) for session state.
SQL Server: This is another approach, the best that we can use in a web farm. We need to configure the database first. The required steps have been explained covered.
As shown in the above picture, all web server session data will be stored in a single SQL Server database. And it is easily accessible. Keep one thing in mind, you should serialize objects in both StateServer and SQLServer modes. If one of the web servers go down, the load balancer will distribute the load to other servers and the user will still be able to read session data from the server, because data is stored in a centralized database server.
In summary, we can use either StateServer or SQLServer session mode in a web farm. We should avoid InProc.
Session and Cookies
Clients use cookies to work with session. Because clients need to present the appropriate session ID with each request. We can do this in the following ways:
Using cookies
ASP.NET creates a special cookie named ASP.NET_SessionId automatically when a session collection is used. This is the default. Session ID is transmitted through that cookie.
Cookie munging
Some older browsers do not support cookies or the user may disable cookies in the browser, in that case, ASP.NET transmits session ID in a specially modified (or “munged”) URL.
How Cookie Munging works?
When the user requests for a page on a server, the server encoded the session ID and adds it with every HREF link in the page. When the user clicks on a link, ASP.NET decodes that session ID and passes it to the page that the user is requesting. Now the requesting page can retrieve session variables. This all happens automatically if ASP.NET detects that the user's browser does not support cookies.
How to implement Cookie Munging?
For this, we have to make our session state cookie-less.
Removing Session
Following are the list of methods that are used to remove Session:
Enabling and disabling Session
For performance optimization, we can enable or disable session because each and every page read and write access of the page involves some performance overhead. So it is always better to enable and disable session based on requirements rather than enable it always. We can enable and disable session state in two ways:
Page level
Application level
Page level
We can disable session state in page level using the EnableSessionState attribute in the Page directive.
This will disable the session activities for that particular page.
The same way, we can make it read-only also. This will permit to access session data but will not allow writing data on session.
Application level
Session state can be disabled for the entire web application using the EnableSessionState property in Web.Config.
Generally we use page level because some pages may not require any session data or may only read session data.
References:
How to Disable ASP.NET Session State
Summary
Hope you are now really familiar with Session, its use, how to apply it in web farms, etc. To summarise:
The in-process (InProc) session provider is the fastest because of everything being stored inside memory. Session data will be lost if we restart the web server or if the worker process is recycled. You can use this in small web applications where the number of users is less. Do not use InProc in web farms.
In StateServer session mode, session data is maintained by aspnet_state.exe. It keeps session data out of the web server. So any issues with the web server does not affect session data. You need to serialized an object before storing data in StateServer session. We can use this safely in web farms.
SQLServer session modes store data in SQL Server. We need to provide the connection string. Here we also need to serialize the data before storing it to session. This is very useful in production environments with web farms.
We can use a Custom provider for custom data sources or when we need to use an existing table to store session data. We can also create custom session IDs in Custom mode. But it is not recommended to create your own custom provider. It is recommended to use a third party provider.
Hope you have enjoyed the article. Please give your suggestions and feedback for further improvements. Again thanks for reading.
Further study and references
I have already added some in the various sections. Here I am giving a few more links which will really help you for further study:
ASP.NET Session Overview
ASP.NET Session State Overview
Different Session Modes
Web Farm-Load Balancing in ASP.NET
Enabling and Disabling Session Mode
Configuring Session Modes
1
Add a comment...
People
In their circles
14 people
Have them in circles
132 people
ASP.NET SOLUTIONS's profile photo
cliff edghill's profile photo
DNN Corp.'s profile photo
George Parker's profile photo
Martin Lucarini's profile photo
Ranga Rajesh Kumar's profile photo
James Nagy's profile photo
木头人's profile photo
Haythem Hassan's profile photo
Contact Information
Contact info
Email
Story
Tagline
Collection of Asp.net and C# articals