Social Icons

Friday, 28 June 2013

Asp.Net Cookies Overview

Introduction:

In my previous articles i explained Client - Side State management techniques like How to use ViewState in Asp.Net, How to Use Hidden Field  in Asp.Net, beginners tutorial to understand QueryString Concept in Asp.Net and Various Examples on Asp.Net and WPF.

         Now we will Purpose of Cookies in Asp.Net, different ways to Create Cookie, How to read Cookie values,How to set Expiration date for Cookie, How to store multiple values in Cookie and Advantages and Disadvantages of Cookies.

Description:

It is one of Client - Side state management techniques in Asp.Net. It is a small text file stored in the client machine and sent by webserver.

             Generally cookies are used to store information like User Visiting pages, User Preferences,authentication , Shopping Cart Details and etc.

Using Cookies:

Different Approaches for Creating/Writing Cookies:

Approach1: 

we will use HttpCookie Class to create Cookie like below

Approach 2:

we can directly add cookie to Response Object like below

Approach 3:Storing Multiple Values in Single Cookie:

Reading Values from Cookies:

Approach1: Reading Single Valued Cookie by using Index 


  Approach2 : Reading Multi Valued Cookies by using Index and by Names of Sub Keys.

If a cookie contains any sub cookies we can access them by using their names only.

Approach 3: Reading Multi Valued Cookies By using name of Cookie and Names of Sub Keys.

  Deleting Cookies:

In the above you saw the code for creating and reading cookies, now see for deleting cookie

 

How to identify whether browser will accept cookies or not:

Request.Browser.Cookies Property will return true if client browser accepts the Cookies.

  HttpCookie class Properties:

HttpCookie class has some important properties 
  • Name: It Contains name of the cookie.
  • Path: Contains the Virtual path of the Cookie.
  • HasKeys: It Contains True if Cookie has Sub Keys.
  • Domain: Contains the Domain Name of the Cookie.
  • Expires:  Contains Expiration Time of Cookie.
  • Secure: It contains true if the cookie needs to pass in secured connection only.

Advantages of using Cookies:

  • Simplicity.Cookie is Light weight, text based structure with simple key-value pairs.
  • No Server Resource required.
  • Configurable expiration. Cookie can be expired based on time set by developers.

Disadvantages of using Cookies:

  • Size limitation of 4 KB. Storing huge information is not possible.
  • User can disable browser Cookies.
  • Total 20 Cookies can be used on a single website. If exceed older cookies will deleted.
  • As it is a text file it can easily tampered. 

Conclusion:

I hope this information about Cookies will give Overview on Cookies in Asp.Net. 

Related Tutorials

Wednesday, 26 June 2013

How to pass multiple values in QueryString in Asp.Net Example

Introduction:

In my previous articles i explained Various articles on WPF and Asp.Net.Now we will see how to pass mutliple values or parameters to another page using QueryString.You can find In dept about QueryString Usage here.

Description:

Take a web form which contains multiple textboxes for entering username,password,age, and contact number.

Multiple parameters in querystring

HTML Code


In Button click event Write below code

Now take another form name it welcome.aspx and take four labels to display querystring values in it.
HTML code


In page load write below code


Now run the first page and enter all details then press submit now page is redirected to welcome.aspx where querystring values are displayed.



Related Tutorials

Beginers tutorial for how to use QueryString in Asp.Net,Encoding,Decoding, Pass Multiple Values throgh QueryString

QueryString Usage in Asp.Net

Introduction:

In my previous article i explained Architecture of wpf, Asp.Net Ajax Tutorial,ViewState in Asp.Net Tutorial,Hidden Field in Asp.Net and various articles on WPF and Asp.Net and Ajax. Now we will see about QueryString Usage in Asp.Net i.e  what is the purpose of Querystring, when to use Querystring in asp.net, what is the need of encoding and decoding QueryString in Asp.Net, Ways to pass values to different pages or same page for different requests,How to pass multiple values through QueryString, How to Build QueryString in Asp.Net.

Description:

QueryString:

It is one of the client - side statemanagement techniques in asp.net. It is one of technique to send variable values to another pages in Asp.Net through URL. Variable values are appended to URL while redirecting or Transfering to another webform or html page in Asp.Net.

       For Sending values to another page we will use QueryString Property of Request Object. While searching on the net you may see URL's like
" http://www.sitename.com/welcome.aspx?firstname=aspdotnet&lastname=sekhar".
  • In the above URL the information after ? symbol is called as QueryString.
  • QueryString has two parts i.e Key and Value.
  • We can send multiple values through QueryString Seperated By & Symbol.
  • QueryString Collection is a NameValueCollection internally where we can access keys using by name or by index.

How to add QueryString Information to URL:

     We know Response.Redirect("URL") is used to transfer current user to another page. We can use same to send user to another page along with QueryString Key- Values. All we have to do is to add QueryString information to URL and add this URL to Response.Redirect.
 To append QueryString information to URL or to add multiple values to URL we will follow below approaches.

We can directly place the querystring information from controls to Url like below


Another way is using string.Format method to send multiple values through

Reading QueryString Values from URL:

In the target page we can read QueryString Values from QueryString Collection using Request.QueryString[] Property. We can read QueryString Values through key name or by index.
example: 

  To Read encoded QueryString use Server.UrlDecode()


  Optimized way of reading QueryString:

It is better to check whether QueryString value is supplied or not before using QueryString Values using string.IsNullOrEmpty() method.

Need of Encoding and Decoding QueryString:

  • URL of QueryString should contain only ASCII characters only. So if you are passing other than ASCII Characters then they need to be encoded.
  • .Net Framework provides HttpServerUtility.UrlEncode and HttpServerUtility.UrlDecode methods for encoding and decoding Non - ASCII characters

Advantages of QueryString:

  • Light weight and not consume any server resource.
  • Easy to use and Efficient Technique.
  • Broad Support. Almost all browsers and devices supports this.
  • Seo Friendly. where user can bookmark pages easily.

Disadvantages of QueryString: 

  •  Limited Capacity: Most browsers and client - devices allow upto 255 characters limit on URL Length.
  • We can pass information in string format only, to pass objects and large volumes of data we have to do lot more things.
  • Security: Information send through QueryString will seen by Users and can be trapped.

Conclusion:

I hope you got atmost details about QueryString usage in Asp.Net, How to pass multiple values throgh querystring, How to append information to QueryString, Reading QueryString Values, Encoding and Decoding QueryString, Advantages and Disadvantages of QueryString etc.

   

Related Tutorials

Monday, 24 June 2013

Hidden Field in Asp.Net

Hidden Field:

ASP.NET allows you to store information in Hidden Field Control, which renders as standard html control on the page. This Hidden Field Control can not be visible in browser. It can be used to store small amount of information which is not secured.
          When a page is submitted to server, content of the hiddenfield is sent in Http form collection along with the values of the other controls. It act as a repository to store any page specific information if you want to store on the page. If a page contains any hidden field then it must be submitted in POST method only.

Advantages of Using Hidden Fields are:

  • No Server resources are required as information is stored in the page.
  • All most all browsers and devices support forms with hidden fields.
  • Simple implementation

Disadvantages of using hidden fields are:

  • Security. Hidden field information is easily tampered if you see the source code of the page.
  • Limited Storage Structure   Hidden field can store single variable value, if you want to store in multiple values you must implement delimited strings and the code to parse strings.
  • Performance. If you try to store large values page will slow down.
Example for Storing values in  Hidden Filed :
  1. Take a form with a hidden field control, label,textbox for taking input and a button control for submittion.


Now in the button click event store the value of textbox in hidden field

 HiddenField1.Value = TextBox1.Text;

If you see the page source after submitting the page then Hidden Field Control renders like this


 
  Hidden Field default value was 0.

Related Tutorials


Access Specifiers in C# with Diagramatic format

Introduction:

In my previous articles i explained WPF architecture overview,Working or Consuming three layered architecture in .Net Windows Application using C#,Asp.Net Ajax Tutorial, Asp.Net validation controls Tutorial and various articles on WPF and Asp.Net. Now i will explain Access Specifiers in C# in pictorial format.

Description:

Access specifiers used to define the scope of types as well as their members i.e who can access it and who can not.

C# supports five access specifiers
  1. Private
  2. Protected
  3. Internal
  4. Protected Internal
  5. Public
Here is the diagram depicting scopes these five access specifiers

Access Specifiers Accessing Levels in diagram format
 
Here is the five cases

Case1: Accessing members within the same class

Case2: Accessing members within child class of same Project.

Case3: Accessing members within non - child class of same project.

Case4: Accessing members within child class of other project.

Case5: Accessing members within non- child class of other project.

You can find in details about each of these five access specifiers with examples at Access Specifiers in C#

Conclusion:

I hope this will help you to get overview of access specifiers with in short time.

Related Tutorials

Acess Specifiers(Access Modifiers) in C#

Access Specifiers or Access Modifiers in C#

What is an access specifier?

Access Modifiers are used to define scope of types as well as their members i.e who can access them and who can not.

C# supports five access specifiers
  • Private
  • Protected
  • Internal
  • Protected Internal
  • Public
Note: Members defined in a type with any scope or specifier are always accessible with in the type(Same Class), restrictions comes into picture only when they try to access them outside of the type.

 Private Access Specifier in C#:

Members declared as private under a class or structure can not be accessed outside of type in which they are defined and  moreover their default scope is private only.Types can't declared as private(classes,structures), so private is applicable to members only(variables,methods,functions).
Note:Interface can not contain any private members and default scope for interface members is public. See in Diagram format

Example For Private Access Modifier:

  Protected Access Specifier in C#:

Members declared as protected under a class can be accessed only with in the class or a child class, non - child class can not consume them. Types can't be declared as protected also, so this is applicable to only members. See in Diagram format

Example for Protected Access Specifier:


In the ProtectedTypes class we declared two variables Protected x1 and public y1. In the childprotectedtype class both variables were consumed. In the childprotectedtype class we declare another protected variable z1. see in Sum() method we accessed all of the variables either protected or public. protected members can access only in children class. we can not access them using object of the class. In TestChildProtectedTypes class i can access only Y1 we can't access any protected variables, You see public variable can be accessed by anywhere.   

Internal Access Specifier in C#:

Members and types that are declared as Internal can be accessed only within the project both from child or non - child classes. The default scope for any type in c# is Internal only. These members can be accessed by using inheritance and by creating object of the class or type. This can be accessed only with in the project outside of the project it can't be accessed. See in Diagram format

Protected Internal Access Specifier in C#:

Members declared as protected internal will have dual scope. i.e with in the project they behave as internal providing access to any where in the project, outside the project they will change to protected and still provide access to their child classes. Types(Class and Structures) can not be declared as protected internal also.  See in Diagram format

   Example for Protected Internal access specifier



In the above example I created a class Protected_Internal where i declared all types of members and i accessed all of these members using inheritance and in non- child class.

Public Access Specifier in C#:

A type or a member of a type if declared as public is global in scope which can be accessed from any where. See in Diagram format

 an example public access specifier



See in the above example we can access all the public members of publictype class in child class and nonchildclass.

Conclusion:

I hope you got some information regarding access specifiers in C# through this example. You can find diagramatic format of Access Specifiers explaination here

Related Tutorials

Friday, 21 June 2013

Storing DataTable in ViewState in Asp.net Example using C#.Net

How to store datatable in viewstate:

Introduction:

In my previous article i explained what is viewstate in asp.net, viewstate postback count example in asp.net,WPF Architecture,Asp.Net Validation Controls Tutorial,Asp.Net Ajax Tutorial and various articles on WPF and Asp.Net. Now i will explain how to store datatable in viewstate and retrieve it back.

Description:

In this example i will get data table from database and store it in viewstate in page load event and then in button click event i will get data table from viewstate and populates it in gridview. This is the example how to store large volumes of data in viewstate.

     Take form which contains a label,a button and gridview like below

In the page load event and button click event write below code


In the page load event i am getting datatable from emp table and storing it in viewstate. in button click event i am getting viewstate emptable and binding that table to gridview. In this way you can store large amount of data in viewstate but it will increase the page size.


Conclusion:

I hope you got an idea how to store large data like datatables in viewstate.

Related Tutorials

 

PageViews Count Example using Viewstate in Asp.net

PageViewsCount Example Using ViewState:

Introduction:

In my previous articles i explained various concepts on WPF,Asp.Net and Ajax.Now i will explain pageviewscount example using ViewState in Asp.Net. You can find full details regarding ViewState here.

Description:

ViewState:

It is the process of tracking the values of the controls on the page for multiple requests for the same page.
In this example we will calculate total postback count on that page. For this take two labels and a button on the page. Page looks like below


 In the form load event write like this


 For every button click event postback occurs and then displays the postback count in the label.

ViewState Postback count example

Conclusion:

I hope you got an idea on how read and write values to and from viewstate in asp.net.

Related Tutorials

 

 

Thursday, 20 June 2013

How to use ViewState in Asp.net with example

ViewState in Asp.Net

Introduction:

In my previous articles i explained Asp.Net Ajax Tutorial,difference between web.config and machine.config,Working or Consuming three tier architecture in .Net Applicaiton and various articles regarding Asp.Net and WPF. Now i will explain what is ViewState in Asp.Net,when to use viewstate, advantages and disadvantages of viewstate,Enabling and disabling viewstate and enableviewstate vs ViewStateMode etc.

Description:

It is the one of the client- side statemanagement techniques in Asp.Net. Web Forms page provides built - in property ViewState for automatically retaining the values between multiple page requests for the same page. we can say it is the process of tracking the values of the controls on the page for multiple requests for the same page. We can also add custom values to ViewState. Asp.Net Framework automatically takes care of storing controls information in viewstate and retrieving it before rendering on postback.
       ViewState uses Hiddenfield to store information in key/value format. The value stored in encrypted format.
If you open the pagesource it will look like this

Difference between web.config and machine.config in asp.net

Difference between web.config and machine.config:

Introduction:

In my previous articles i explained about WPF Architecture, Working or Consuming three tier architecture in .Net Applications, Asp.Net Ajax Tutorial and difference between app.config and web.config.Now i would like to Explain difference between web.config and machine.config Server Configuration files in Asp.Net Applications.

Description:

.Net Framework provides three types of files known as Configuration Files which contains information for controlling and managing .Net Applications i.e.Windows(Windows Forms and WPF) and Web Applications(Asp.Net,MVC). These are Machine.Config, App.Config and Web.Config.

        These files are XML based configuration files.All the information is in the format of XML which can be easily modified and readable by .Net Applications.

Machine.Config:

  • This file is the top in the hierarchy of configuration files.
  • It is a global settings file for all the applications that run on the .Net Framework on the machine.
  • All the .Net application will use this configuration information in their applications.