19 November 2013


Enumerating Session Management Techniques

Before we proceed, let us see what all session management techniques are present in the ASP.NET framework.
·         In-Proc.
·         SQLServer.
·         StateServer.

How to configure Sessions

To configure the session management we need to specify the settings in the web.config file. Typical settings inweb.config looks like:
http://www.codeproject.com/images/minus.gif Collapse | Copy Code
<sessionState mode="InProc" 
                stateConnectionString="tcpip=127.0.0.1:42424" 
                sqlConnectionString="Data Source=.\SQLEXPRESS;Trusted_Connection=Yes;" 
                cookieless="false" 
                timeout="100"/>
Let us see what each of these attributes mean.

Mode
This specifies the type of session management we want to use. it could be InProc,SQLServer, and StateServer
stateConnectionString
If we use StateServer as session management technique then this specifies the location of the server that is handling the session data.
sqlConnectionString
If we use SQLServer as session management technique then this specifies the databaseconnectionstring that will store the session data.
cookieless
This specifies whether we will be using cookies to identify sessions or we want session info appended in URL. It could be true or false.
Timeout
This specifies the time for which the session should be active. after this much time of inactivity the session will expire. 







Hi, session object is mainly used to store the data and pass it from one page to another page in your applications.
syntax to use sessions
session["username"]=txtusername.Text;
To retrive the value in the session: string username=convert.tostring(session["username"]);
U can store a single variable or objects like dataset,datatable, array etc also in the sessions.
Once u close u r application, u r session data will be lost.
u have following types of session management in asp.net which u can define in your web.config file
session mode="inproc"...means the session will be stored on the webserver within u r application
session mode="outproc"....means session will be stored on the server outside u r application
session mode="stateserver"...means session will be stored in a temporary memory in the database
session mode="sqlserver"...means session will be stored in the databsae permanently.


17 November 2013

Find Control or Datakeys on ItemCommand Event of ListView


          To Find Control in ItemCommand Event of ListView is little simple than finding in gridview but finding a DataKeys from ListView are not simple as find control :
1. To Find a Control From ListView is as follows

  Label LabelEmail = (Label)e.Item.FindControl("LabelEmail"); 

And Now You Can Access that Properties of that Control. 
   LabelResult.Text = LabelEmail.Text; 

2. For Finding a DataKeys the following way is used First Create a ListViewDataItem object.
For Eg: 
   ListViewDataItem dataItem = (ListViewDataItem)e.Item;

3. Finally You Can get datakeys of that ListView using that dataitem DisplayIndex.
For Eg: 
   string code = ListView1.DataKeys[dataItem.DisplayIndex].Values[0].ToString();

Display/Show Toolbox in Visual Studio + Shortcut Key

To display Toolbox in Visual studio IDE, Follow the below steps:
Step 1. Go to View menu
Step 2: Click on Toolbox option,  the toolbox will appear in left hand side of visual studio.


There is keyboard shortcut  also to display toolbox
Press CTRL key + W + X from keyboard


Confirmation Before Deleting Record Using Grid View

<Column>
<asp:TemplateField> 
<ItemTemplate> 
<asp:Button ID="btnDel" runat="server" Text="Delete CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete?');" /> </ItemTemplate>
</asp:TemplateField>
</Column>

Show Line number in Visual Studio Editor 2005/2008/2010

              You can display line numbers in your visual studio editor. Just follow the following steps to display line numbers. by default line number are hidden.
Step 1: Go to Tools menu at the top of VS.
Step 2: Go to Options
Step 3: Go to Text Editor and then select C# or your language.
Step 4: Select Line number under display options.
Click on OK and it will display he line number in editor.



Displaying line numbers in editor:


How to store Datatable in session, How to retrieve data from session to Datatable & how to search data in Datatable

H


      private void add_rooms1(int p,string room_name,int room_type_id,DateTime dateTime,                    DateTime dateTime_2,  string p_2, string p_3)
      {
            try
            {

                DataTable dt = new DataTable();


                if (Session["room_booking"] == null)
                {

                    dt.Columns.Add("Room_id", typeof(int));
                    dt.Columns.Add("Room_name", typeof(string));
                     dt.Columns.Add("room_type_id", typeof(int));
                    dt.Columns.Add("CheckInDate", typeof(DateTime));
                    dt.Columns.Add("CheckOutDate", typeof(DateTime));
                    dt.Columns.Add("Extra_bed", typeof(int));
                    dt.Columns.Add("Extra_charge", typeof(decimal));

                    dt.Rows.Add(p, room_name, room_type_id,dateTime, dateTime_2,                              nvert.ToInt32(p_2), Convert.ToDecimal(p_3));

                    Session["room_booking"] = dt;
                    grand_total = 0;
                    bind_booking_cart();
                }
                else
                    if (Session["room_booking"] != null)
                    {
                        DataTable dt1 = new DataTable();
                        dt1 = Session["room_booking"] as DataTable;
                      
                          bool AlreadyExists=check_cart(p);
                          if (AlreadyExists == true)
                                {
                                    lblerror.Visible = true;
                                    lblerror.ForeColor = System.Drawing.Color.Red;
                                    lblerror.Text = "Room alredy added";
                                    grand_total = 0;
                                    bind_booking_cart();
                                }
                                else
                                {
                                    dt1.Rows.Add(p,room_name,room_type_id, dateTime, dateTime_2,           Convert.ToInt32(p_2), Convert.ToDecimal(p_3));
                                    Session["room_booking"] = dt1;
                                    grand_total = 0;
                                    bind_booking_cart();
                                }
                      
                    }

            }
            catch (Exception ex)
            {
                lblerror.Visible = true;
                lblerror.ForeColor = System.Drawing.Color.Red;
                lblerror.Text = ex.Message;
            }
        }

protected bool check_cart(int id)
        {
            //try
            //{
                bool AlreadyExists = false;
                DataTable dt1 = new DataTable();
                dt1 = Session["room_booking"] as DataTable;
                foreach (DataRow row1 in dt1.Rows)
                {
                    Int32 array1 = Convert.ToInt32(row1["Room_id"]);
                    if (array1 == id)
                    {
                        AlreadyExists = true;
                        break;
                    }
                    else
                    {
                        AlreadyExists = false;
                       
                    }

                }
                return AlreadyExists;
            //}
            //catch (Exception ex)
            //{
            //    lblerror.Visible = true;
            //    lblerror.ForeColor = System.Drawing.Color.Red;
            //    lblerror.Text = ex.Message;
            //}

        }



How to find days between two dates in c#.net

How to find days between two dates in c#.net
DateTime dt1 = new DateTime(2011, 01, 01);
DateTime dt2 = new DateTime(2011, 01, 20);
int ireturn = (int)dt2.Subtract(dt1).TotalDays;

(or)
var elapsedDays = (endDate - initialDate).TotalDays;

(or)
DateTime start = DateTime.Now;
DateTime end = DateTime.Now.AddDays(5);
TimeSpan span = end.Subtract(start);
(or)

DateTime dt1 = Convert.ToDateTime(Textbox1.text);
DateTime dt2 = Convert.ToDateTime(Textbox1.text);
TimeSpan ts = dt2.subtract(dt1);

(or)

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Label ID="Label9" runat="server">
    </asp:Label>
    <asp:Label ID="Label10" runat="server">
    </asp:Label><br />
    <br />
    <asp:Label ID="Label1" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label2" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label3" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label4" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label5" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label6" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label7" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label8" runat="server">
    </asp:Label><br />
</asp:Content>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime startTime = Convert.ToDateTime("12 Aug 2012 12:30 PM");
        DateTime endTime = Convert.ToDateTime("11 Sep 2013 15:30 PM");
        Label9.Text = "StartTime :" + startTime;
        Label10.Text = "EndTime :" + endTime;
        //We subtract the time.
        TimeSpan span = endTime.Subtract(startTime);

        //This will just return the difference selected option
        Label1.Text = "Time Difference (seconds): " + span.Seconds;
         Label2.Text = "Time Difference (minutes): " + span.Minutes;
         Label3.Text = "Time Difference (hours): " + span.Hours ;
         Label4.Text = "Time Difference (days): " + span.Days ;

        //This will return the total difference based on selected options
         Label5.Text = "Total milliseconds: " + Math.Round(span.TotalSeconds, 2).ToString() ;
         Label6.Text = "Total Minutes: " + Math.Round(span.TotalMinutes, 2).ToString() ;
         Label7.Text = "Total hours: " + Math.Round(span.TotalHours, 2).ToString();
         Label8.Text = "Total days: " + Math.Round(span.TotalDays, 2).ToString();
    }
}