Pages

Tuesday, December 18, 2012

datetime format in c#

Function                                                    Shows

DateTime.Now.TimeOfDay()                    13:39:21.0363750
DateTime.Now.ToShortTimeString()        1:39 PM
DateTime.Now.ToString("HH:mm ss tt") 13:39 21 PM
DateTime.Now.ToString("hh:mm ss tt")   01:39 21 PM
DateTime.Now.ToString("HHmm")          1339   

Thursday, December 13, 2012

What is the Parent Class of Server controls ?

System.Web.Ul.Control

How to pass '&' in QueryString?

We can use %26 for '&' symbol in QueryString,

There is another way using URL Encode

string members="mom & dad";
Response.Redirect("family.aspx?mem="+ Server.UrlEncode(members));

What is the Maximum length of QueryString?

Maximum length of Querystring is based on the browser not depend upon the asp.net.


  • Maximum length of a querystring in IE 4.0 and above is ~2048 characters
  • IE. 4,5,6,7, - 2,048 characters.
  • Opera supports - 4050 characters.
  • Netscape 6 supports - 2000 characters.Firefox Supports - 6000 characters 

Wednesday, December 12, 2012

State Management Techniques

The following are the commonly used state management techniques.
  • QueryString
  • Coockies
  • Cache
  • ViewState
  • Session state
  • Application state
  • Static variables
  • Profiles 
 Query String:

Query String will looks like

www.aspnet.com/article.aspx?id=1

In URL which comes after ? symbol is called QueryString.It will be having key,value pair.

In above example 'id' is the key and '1' is the value.we can send multiple values using QueryString

Response.Redirect("www.aspnet.com/article.aspx?id=1&name=satya");

In article.aspx page we can get id and name values as follows

Request.QueryString["id"];
Request.QueryString["name"];

It is most useful State Management technique,It is very easy to use,it will not consume any server side resources.

Disadvantages:

  • URL length limitation is there,we can't send more information through URL.
  • Information send through URL can be visible to anyone,and can be easily altered.

Cookies:

Cookies are Client Side State Management technique.

In ASP.NET we will create Cookies using HttpCookie class.Using Response,Request objects we will add and retrieve Cookies respectively.

Create Cookie:

Response.Redirect["id"].Value="10"

It will stay till browser closes,once browser colsed Cookie will expire.We can create Cookie which will stay based on time.

By making use of HttpCookie.Expires property we can set the expiry date.See below

Response.Cookies["id"].Value="10";
Response.Cookies["id"].Expires=DateTime.Now.AddDays(1);

so that 'id' value will be statying till one day.If we want to remove before that expiry date ,we need to write Expires statement once again as follows

Response.Cookies["id"].Expires=DateTime.Now.AddDays(-1);

Reading the Cookie Values:

Request.Cookies["id"].Value;





Tuesday, December 11, 2012

Restriction Operators

Where:

Using where we can filter the records in a particular set based on some condition.

EX:

For example take one array set of numbers

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

Suppose if we want to find the elements which are less than 5 in the above set.How to achieve this using where condition in LINQ.

Declare one variable and using the where condition to above set ,store the values in variable.

var lowNums = from n in numbers where n < 5 select n;

Now lowNums is having values less than 5

code:
 public void whereSimple1()
        {
            int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

            var lowNums = from n in numbers where n < 5 select n;

            Console.WriteLine("Numbers <5:");
            foreach (var x in lowNums)
            {
                Console.WriteLine(x);
            }
            Console.ReadKey();
        }

output:

Numbers <5:

4
1
3
2
0

Types of Operators in LINQ

The following types of Operators existed in LINQ

  • Restriction Operators
  • Projection Operators
  • Partitioning Operators
  • Ordering Operators
  • Grouping Operators
  • Set Operators
  • Conversion Operators
  • Element Operators
  • Generation Operators
  • Quantifiers
  • Aggregate Operators
  • Miscellaneous Operators
  • Custom Sequence Operators
  • Query Execution
  • Query Execution