This question set will be useful to the .Net Developer with fresher level or 1/2 years of experience. As it contain basic questions with little information.
1. Under what circumstances would you consider using a Code Generation Tool?
When we are writing repeating code or same code is reusable. Example Insert/Update/Delete/Search routine for database operation. For these we can create templates for each routine and use those template for code generation.
2. Explain the virtual function in c# with an example?
using System;
class A
{
public virtual void Test()
{
Console.WriteLine("A.Test");
}
}
class B : A
{
public override void Test()
{
Console.WriteLine("B.Test");
}
}
class Program
{
static void Main()
{
A ref1 = new A();
ref1.Test();
A ref2 = new B();
ref2.Test();
}
}
Output
--------
A.Test
B.Test
With virtual methods, the runtime type is always used to determine the best method implementation. In the main entry point, ref1 has a compile-time and runtime type of A. On the other hand, ref2 has a compile-time type of A but a runtime type of B.
With virtual methods, The runtime type is used in both invocations.
3. Explain how cookies work in ASP.NET? Also explain difference between session and persistent cookie.
Cookies are used to save user related preferences in browser & reuse those settings when user comes back on same website.
Session cookies are temporary cookies , when browser closes it loses data stored in it.
Persistent cookies are permanent cookies, saved in browser setting folder till you manually delete it or browser setting expiry date meets.
4. What is Ispostback method in ASP.Net? Why do we use that??
This is a read-only Boolean property that indicates if the page or control is being loaded for the first time, or if it is being loaded in response to a client postback.
5. Explain how the querystring is used to pass parameters between ASP.NET pages. Provide an example.
Target Page
—————
Page_Load(object sender, System.EventArgs e) {
string ID = Request.QueryString["ImageID"];
string Name = Request.QueryString["ImageName"];
Label1.Text = "ImageID: "+ ID;
Label2.Text = "Image name: "+ Name;
Image1.ImageUrl = "~/Images/"+Name+".jpg";
}
Calling Page
————–
String ImageID = "123";
String ImageName = "ProfilePic";
Response.Redirect("~/TargetPage.aspx?ImageID=" + ImageID + "&ImageName=" + ImageName);
6. Explain how a Single Sign-on login might be achieved across two separate ASP.NET applications which share the same database.
–>txtUserName is the text box ID that will carry the user name
–>txtPassword is the text box ID that will carry the password
–>Check and validate the user is exists with the right user and password or not
if (Membership.ValidateUser(_txtUserName.Text, _txtPassword.Text))
{
MembershipUser user = Membership.GetUser(_txtUserName.Text);
MembershipUser.IsApproved
if (_user != null)
{
if (_user.IsLockedOut)
{
//do something like display a message that this account is locked out
}
else
{
FormsAuthentication.SetAuthCookie(_txtUserName.Text, true);
string url=string.format(“http://SecondApplicationURL/authntication.aspx?username={0}&BackURL={1}”,_txtUserName.Text,”Http://FirstApplicationURL/deafult.aspx”);
Response.Redirect(url);
}
}
}
7. What is difference between ExecuteReader, ExecuteNonQuery and Executescalar?
ExecuteReader : Use for accessing data. It provides a forward-only, read-only, connected recordset.
ExecuteNonQuery : Use for data manipulation, such as Insert, Update, Delete.
ExecuteScalar : Use for retriving 1 row 1 col. value., i.e. Single value.
8. What is use of finally block? For what purpose you can use it?
Finally can be used to ensure that some logic is always executed before the method is exited.
9. What is difference between truncate and delete?
TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUCATE is faster.
DELETE is used to remove rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent.
10. Explain modes of firing triggers.
AFTER INSERT Trigger : This trigger is fired after an INSERT on the table.
AFTER UPDATE Trigger : This trigger is fired after an update on the table.
AFTER DELETE Trigger : This trigger is fired after a delete on the table.
11. What is ViewState? Explain with example
ViewState allows the state of objects to be stored in a hidden field on the page.
<form id="form1" runat="server">
<asp:TextBox runat="server" id="NameField" />
<asp:Button runat="server" id="SubmitForm" onclick="SubmitForm_Click" text="Submit & set name" />
<asp:Button runat="server" id="RefreshPage" text="Just submit" />
<br /><br />
Name retrieved from ViewState: <asp:Label runat="server" id="NameLabel" />
</form>
protected void Page_Load(object sender, EventArgs e)
{
if(ViewState["NameOfUser"] != null)
NameLabel.Text = ViewState["NameOfUser"].ToString();
else
NameLabel.Text = "Not set yet...";
}
protected void SubmitForm_Click(object sender, EventArgs e)
{
ViewState["NameOfUser"] = NameField.Text;
NameLabel.Text = NameField.Text;
}
12. Please explain how javascript can be added to an ASP.NET application such that its content is created dynamically at run-time and can contain database content.
Here is the code which help is to add javascript dynamically :
ClientScriptManager CS = this.ClientScript;
StringBuilder SB = new StringBuilder();
SB.Append("window.alert(\"1111\");");
CS.RegisterClientScriptBlock(this.GetType(), "MyScript", SB.ToString(), true);
13. Please explain steps required to deploy an ASP.NET application & database in an automated manner.
Using Publish Profile we can automate deployment process of our application.
Hope this will Help !!!
Jay Ganesh