Sunday 22 December 2013

Query String

Interview Questions & Answer on ASP.NET Query Strings

Q : Give an example of using querystrings to send data from one page to another?
Ans : Query strings are a very simple and popular technique to pass data from one Web page to the next. You send data as part of the URL. In the below example FName and LName are sent as part of the URL. In the page load of QueryStrings2.aspx we use Request.QueryString to read the values. As we are sending more than one query string we use the & symbol to seperate query strings.
Q : //Code to send query strings FName and LName as part of the URL
Ans : QueryStrings2.aspx?FName=David&LName=Boon
protected void Page_Load(object sender, EventArgs e)
{
//Code to read Query String values
string FirstName = Request.QueryString["FName"];
string LastName = Request.QueryString["LName"];
Response.Write("Data from QueryStrings1.aspx : " + FirstName + ", " + LastName);
}
Q : Give an example to send Query Strings from code?
Ans : You can send query strings from server side code using the Response.Redirect() method as shown below.
Response.Redirect("QueryStrings2.aspx?FName=David&LName=Boon");
Q : What are the advantages of using Query Strings?
Ans : 1. Query strings are easy to implement.
2. Browser support for passing values in a query string is nearly universal.
3. Query strings are contained in the HTTP request for a specific URL and do not require server resources.
Q : What are the disadvantages of using querystrings to send data from one page to another?
Ans : 1. Query strings are insecure because the information in the query string is directly visible to the user on the address line in the browser.
2. Many browsers impose a 255 URL character limit which can limit their flexibility.

Session in Dot Net

                                                                                                                                                Next......


Q : What is a Session?
Ans : A Session is a unique instance of the browser. A single user can have multiple instances of the browser running on his or her machine. If each instance visits your Web application, each instance has a unique session.A session starts when a user accesses a page on a Web site for the first time, at which time they are assigned a unique session ID. The server stores the user's session ID in the Session.SessionID property.
Q : What is the default session timeout period?
Ans : 20 minutes.
Q : Where do you generally specify the Session Timeout?
Ans : You specify the Session Timeout setting in the web.config file.
Q : Can you specify Session Timeout in a code behind file?
Ans : Yes, can specify the Session.Timeout property as shown below in a code behind file. Session.Timeout = 10;
Q : How do you end a user session?
Ans : You can call the Session.Abandon() method to end a user session. If a user then tries to access a page the server will assign them a new session ID and it will clear all the previous session variables. You'll typically use Session.Abandon() on log-out pages.
Q : What type of data can you store in Application State and Session State variables?
Ans : Application State and Session State variables are used to store data that you want to keep for the lifetime of an application or for the lifetime of a session. You can store any type of data in the Application or Session state, including objects.
Q : Are Application State or Session State variables type safe?
Ans : No, Application and Session state variables are created on the fly, without variable name or type checking.
Q : Do maintaining Session state affects performance?
Ans : Yes
Q : Can you turn of Session state?
Ans : Yes, Session state can be turned off at the application and page levels.
Q : Are Application state variables available throughout the current process?
Ans : Yes, Application state variables are available throughout the current process, but not across processes. If an application is scaled to run on multiple servers or on multiple processors within a server, each process has its own Application state.
Q : How do you disable Session state for a Web form?
Ans : To turn Session state off for a Web form set EnableSessionState property of the Page to False.
Q : How do you turn Session state off for an entire web application?
Ans : In the Web.config file, set the sessionstate tag to False.
Q : What are Application State variables?
Ans : Application State variables are global variables that are available from anywhere in the application. All Sessions can access Application State variables.
Q : How to add and remove data to Application State Variables?
Ans ://Code to add data to Application State
Application.Add("AppName", "Sample");

//Code to remove data from Application State
Application.Remove("A ppName");
Q : How do you remove all Application State Variables data?
Ans : //Code to remove all Application State Variables data
Application.RemoveAll();

Cookies

Interview Questions & Answer on ASP.NET Cookies

Q : What are Cookies in ASP.NET?
Ans : Cookies are small pieces of information stored on the client computer.Use cookies to store small amounts of information on the client’s machine. Web sites often use cookies to store user preferences or other information that is client-specific. Because cookies can be refused, it is important to check whether the browser allows them before you try to create them.They are limited to storing only character data and they are limited to 4K in size.
Q : What are different types of Cookies?
Ans : Session Cookies
Persistent Cookies
Q : What are Session Cookies?
Ans : Session cookies are stored in-memory during the client browser session. When the browser is closed the session cookies are lost.
Q : How can you create Session Cookies?
Ans : You can create session cookies by calling the Add method of the Cookies collection on the Response object. The Cookies collection contains individual cookie objects of type HttpCookie.

//Code to create a UserName cookie containing the name David.
HttpCookie CookieObject = new HttpCookie("UserName", "David");
Response.Cookies.Add(CookieObject);

//Code to read the Cookie created above Request.Cookies["UserName"].Value;
Q : What is the difference between Session Cookies and Persistent Cookies?
Ans : Persistent Cookies are same as Session Cookies except that, persistent cookies have an expiration date. The expiration date indicates to the browser that it should write the cookie to the client's hard drive. Keep in mind that because a user can delete cookies from their machine that there is no guarantee that a cookie you "drop" on a user machine will be there the next time they visit your site.
Q : What are Persistent Cookies used for?
Ans : Persistent cookies are generally used to store information that identifies a returning user to a Web site. Typical information found in Persistent Cookies includes user names or user IDs.
Q : How do you create a Persistent Cookie?
Ans : You create a persistent cookie the same way as session cookies except that you set the Expires property to a Date in the future which will store the Cookie to the client computer harddrive.

//Code to create a UserName Persistent Cookie that lives for 10 days
HttpCookie CookieObject = new HttpCookie("UserName", "David");
CookieObject.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(CookieObject);

//Code to read the Cookie created above
Request.Cookies["UserName"].Value;
Q : What is Cookie Dictionary?
Ans :A cookie dictionary is a single cookie object that stores multiple pieces of information. You use the Values property to access and assign new values to the cookie dictionary.
Q : What are the advantages of Using Cookies?
Ans : 1. Cookies do not require any server resources since they are stored on the client.
2. Cookies are easy to implement.
3. You can configure cookies to expire when the browser session ends (session cookies) or they can exist for a specified length of time on the client computer (persistent cookies).
Q : What are the disadvantages of Using Cookies?
Ans : 1. Users can delete a cookies.
2. Users browser can refuse cookies,so your code has to anticipate that possibility.
3. Cookies exist as plain text on the client machine and they may pose a possible security risk as anyone can open and tamper with cookies.
Q : How do you create a Cookie that never expires?
Ans :To create a Cookie that never expires set the Expires property of the Cookie object to DateTime.MaxValue.
Q : Are Cookies secure?
Ans : No, Cookies are not secure. You must pay attention to the type of data you store in cookies.
1. Cookies are not designed to store critical information so storing passwords in a cookie is a bad idea.
2. Keep the lifetime of a cookie as short as practically possible.
3. Encrypt cookie data to help protect the values stored in the cookie.

State Management Technique

State management Technique is a process by which we maintain the state & page into over multiple request for the same of different page.
(1) Cookies in Asp.Net:-   There are Two types of cookies in Asp.Net

  • Session Cookie/Non persistence Cookie
  • Persistence Cookie

For more Detail About Cookie Click here And For Session Click here

On the Web Form 1
protected void btnclick_click (object sender,Event args e)
{
HttpCookie M1 = new Httpcookie["userinfo"];
M1["name"] = textbox1.text;
M1["email"] = textbox2.text;
Responce.cookies.Add("M1");
Responce.redirect("webform2.aspx");
}



On the Web Form 2
protected void Page_Load (object sender,Event args e)
{
HttpCookie M1 = Request.cookies["userinfo"];
if(Cookie ! = null)
lable1.text = M1["name"] ;
lable2.text = M1["email"];

}




(2) Query String Management System:-

For more Detail About Cookie Click here

On WebForm 1
{
protected void btnclick_click (object sender,Event args e)
Responce.Redirect ( "webform2.aspx?username+" + textbox1.text + "& useremail + " textbox2.text);
}

On WebForm 2

protected void Page_Load (object sender,Event args e)
{
lable1.text = Request.querystring [ "ussername"];
lable2.text = Request.querystring [ "usseremail"];
}


(3) Session State Management System:-

On WebForm 1
{
protected void btnclick_click (object sender,Event args e)
Session [ "username"] = textbox1.text;
Session [ "useremail"] = textbox2.text;
}

On WebForm 2

protected void Page_Load (object sender,Event args e)
{
if ( Session["username"] ! = null)
{
lable1.text = Session [ "username"].ToString;
}

if ( Session["useremail"] ! = null)
{
lable2.text = Session [ "useremail"].ToString;
}
}

(4) Application State Management System:-

On WebForm 1
{
protected void btnclick_click (object sender,Event args e)
Application [ "username"] = textbox1.text;
Application [ "useremail"] = textbox2.text;
}

On WebForm 2

protected void Page_Load (object sender,Event args e)

{
lable1.text = Application [ "username"].ToString();
lable2.text = Application [ "useremail"].ToString();

}

(5) View State/Hidden Field Management System:-

It is used to maintain the state of controls during Page Post Back and if we save any Control value or anything in View state we can access those value through out the page whenever it  require for that check

For more Detail About View State  Click Here




C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...