ASP.Net Interview questions: - What is the difference between ‘Server. Transfer’ and ‘response.Redirect’?
Following are the major differences between them:-
‘Response. Redirect’ sends message to the browser saying it to move to some different page, while server. Transfer does not send any message to the browser but rather redirects the user directly from the server itself. So in ‘server. Transfer’ there is no round trip while ‘response. Redirect’ has a round trip and hence puts a load on server.
Using ‘Server. Transfer’ you cannot redirect to a different from the server itself. Example if your server is www.yahoo.com you cannot use server. Transfer to move to www.microsoft.com but yes, you can move to www.yahoo.com/travels, i.e. with in websites. Cross server redirect is possible only by using Response. Redirect.
With ‘server. Transfer’ you can preserve your information. It has a parameter called as “preserveForm”.Therefore, the existing query string etc. will be able in the calling page.
If you are navigating within the same website use “Server. Transfer” or else go for “response.Redirect ()”
ASP.NET interview questions: - Difference between ASP and ASP.NET?
This is the most practical oriented ASP.NET Interview Questions which may be asked during the Interview by the Interviewer.
ASP.NET new feature supports are as follows:-
Better Language Support
New ADO.NET Concepts have been implemented.
ASP.NET supports full language (C#, VB.NET, C++) and not simple scripting like VBSCRIPT…
Better controls than ASP
ASP.NET covers large set’s of HTML controls..
Better Display grid like Data grid, Repeater and datalist.Many of the display grid havpaging support.
Controls have events support
All ASP.NET controls support events.
Load, Click, and Change events handled by code makes coding much simpler and much better organized.
Compiled Code
ASP.NET supports forms-based user authentication, including cookie management and automatic redirecting of unauthorized logins. (You can still do your custom login page and custom user checking).
Better Authentication Support
ASP.NET supports forms-based user authentication, including cookie management and automatic redirecting of unauthorized logins. (You can still do your custom login page and custom user checking).
User Accounts and Roles
ASP.NET allows for user accounts and roles, to give each user (with a given role) access to different server code and executables.
High Scalability
Much has been done with ASP.NET to provide greater scalability.
Server to server communication has been greatly enhanced, making it possible to scale an application over several servers. One example of this is the ability to run XML parsers, XSL transformations, and even resource hungry session objects on other servers.
Easy Configuration
Configuration of ASP.NET is done with plain text files.
Configuration files can be uploaded or changed while the application is running. No need to restart the server. No more metabase or registry puzzle.
Easy Deployment
No more server restart to deploy or replace compiled code. ASP.NET simply redirects all new requests to the new code.
See the following video on ASP.NET for importance of session and view state: -
ASP.NET interview questions: - Can you explain in brief how the ASP.NET authentication process works?
ASP.NET does not run by itself, it runs inside the process of IIS. Therefore, there are two authentication layers, which exist in ASP.NET system. First authentication happens at the IIS level and then at the ASP.NET level depending on the WEB.CONFIG file.
Below is how the whole process works:-
IIS first checks to make sure the incoming request comes from an IP address that is allowed access to the domain. If not it denies the request.
Next IIS performs its own user authentication if it is configured to do so. By default IIS allows anonymous access, so requests are automatically authenticated, but you can change this default on a per – application basis with in IIS.
If the request is passed to ASP.NET with an authenticated user, ASP.net checks to see whether impersonation is enabled. If impersonation is enabled, ASP.net acts as though it were the authenticated user. If not ASP.net acts with its own configured account.
Finally, the identity from step 3 is used to request resources from the operating system. If ASP.net authentication can obtain all the necessary resources it grants the users request otherwise it is denied. Resources can include much more than just the ASP.net page itself you can also use .NET’s code access security features to extend this authorization step to disk files, Registry keys and other resources.
See the following video on ASP.NET Forms authentication by using session hijack: -
ASP.NET interview questions: - What is the difference between trace and debug in ASP.NET?
There is also a fundamental difference in thinking when we want to use trace and when want to debug. Tracing is a process about getting information regarding program’s execution. On the other hand debugging is about finding errors in the code.
Debug and trace enables you to monitor the application for errors and exception without VS. NET IDE.
In Debug mode compiler inserts some debugging code inside the executable. As the debugging code is the part of the executable they run on the same thread where the code runs and they do not given you the exact efficiency of the code ( as they run on the same thread). So for every full executable DLL you will see a debug file also as shown in figure ‘Debug Mode’.
Figure: - Debug mode
Trace works in both debug as well as release mode. The main advantage of using trace over debug is to do performance analysis which cannot be done by debug. Trace runs on a different thread thus it does not impact the main code thread.
See the following video for how to Skip debugging in ASP. NET: -
Post cache substitution is used when we want to cache the whole page but also need some dynamic region inside that cached page. Some examples like QuoteoftheDay, RandomPhotos, and AdRotator etc. are examples where we can implement Post Cache Substitution.
Post-cache substitution can be achieved by two means:
Call the new Response.WriteSubstitution method, passing it a reference to the desired substitution method callback.
Add a <asp:Substitution> control to the page at the desired location, and set its methodName attribute to the name of the callback method.
Figure: - “Writesubstitution” in action
You can see we have a static function here “GetDateToString()”. We pass the response substitution callback to the “WriteSubstitution” method. So now, when ASP.NET page framework retrieves the cached page, it automatically triggers your callback method to get the dynamic content. It then inserts your content into the cached HTML of the page. Even if your page has not been cached yet (for example, it’s being rendered for the first time), ASP.NET still calls your callback in the same way to get the dynamic content. So you create a method that generates some dynamic content, and by doing so you guarantee that your method is always called, and it’s content is never cached.
Ok the above example was by using “WriteSubstitution” now lets try to see how we can do by using “<asp:Substitution>” control. You can get the “<asp:Substitution>” control from the editor toolbox.
Figure: - Substitution Control
Figure: - Substitution in Action
Below is a sample code that shows how substitution control works. We have ASPX code at the right hand side and class code at the behind code at the left hand side. We need to provide the method name in the “methodname” attribute of the substitution control.
View following video on Web.config transformation in ASP .Net: -
In this article we will try to understand 3 important concepts association, aggregation and composition.
We will also try to understand in what kind of scenarios do we need them. These 3 concepts have really confused lots of developers and in this article my attempt would be to present the concepts in a simplified manner with some real world examples.
Extracting real world relationships from requirement
The whole point of OOP is that your code replicates the real world object, thus making your code readable and maintainable. The time we say real world, real world have relationships. Let’s consider the simple requirement listed below:-
Manager is anemployee of XYZ limited corporation.
Manager uses a swipe card to enter XYZ premises.
Manager has workers who work under him.
Manager has the responsibility of ensuring that the project is successful.
Manager’s salary will be judged based on project success.
If you flesh out the above 5 point requirement we can easily visualize 4 relationships:-
Inheritance
Aggregation
Association
Composition
Let’s understand them one by one.
Requirement 1 (The IS A relationship)
If you see the first requirement (Manager is an employee of XYZ limited corporation) it’s a parent child relationship or inheritance relationship. The sentence above specifies that Manager is a type of employee, in other words we will have two classes one the parent class “Employee” and the other a child class “Manager” which will inherit from “Employee” class.
Note: -The scope of this article is only limited to aggregation, association and composition. So we will not discuss inheritancein this article as its pretty straight forward and I am sure you can get1000 of articles on the net which will help you in understanding the same.
Requirement 2 (The Using relationship: - Association)
The requirement 2 is an interesting requirement (Manager uses a swipe card to enter XYZ premises). In this requirement the manager object and swipe card object use each other but they have their own object life time. In other words they can exist without each other. The most important point in this relationship is that there is no single owner.
The above diagram shows howthe “SwipeCard” class uses the “Manager” class and the “Manager” class uses the “SwipeCard” class. You can also see how we can create the object of the “Manager” class and “SwipeCard” independently and they can have their own object life time.
This relationship is called as the “Association” relationship.
Requirement 3 (The Using relationship with Parent: - Aggregation)
The third requirement from our list (Manager has workers who work under him) denotes the same type ofrelationship like association but with a difference that one of them is an owner. So as per the requirement the “Manager” object will own “Workers” object.
The child “Worker” objects can not belong to any other objects. For instance the “Worker” object cannot belong to the “SwipeCard” object.
But But….the “Worker” object can have his own life time which is completely disconnected from the “Manager” object. Looking from a different perspective it means that if the “Manager” object is deleted the “Worker” object does not die.
This relationship is termed as the “Aggregation” relationship.
Requirement 4 and 5 (The Deathrelationship: - Composition)
The last two requirements are actually logically one. If you read closely both the requirements which are as follows:-
Manager has the responsibility of ensuring that the project is successful.
Manager’s salary will be judged based on project success.
Below is the conclusion from analyzing the above requirements:-
Manager and the project objects are dependent on each other.
The lifetimes of both the objects are same. In other words the project will not be successful if the manager is not good and manager will not get good increments if project has issues.
Below is how the class formation will look like. You can also see when I go to create the project object it needs the manager object.
This relationship is termed as the composition relationship. In this relationship both objects are heavily dependent on each other. In other words if goes for garbage collection the other also has to garbage collected , or putting from a different perspective the life time of the objects are same. That’s why I have put in the heading “Death” relationship.
Putting things together
Below is a visual representation of how the relationships have emerged from the requirements.
The source code
You can also download source code for this article
Summarizing
To avoid confusion hence forth in these 3 terms I have put forward a table below which will help us compare them from 3 angles owner , life time and child object.
Video on Association, Aggregation and Composition
I have also added a video in case you do not want to read this long article.
.NET interview questions: - How will you elaborate project life cycle?
This is basic of .NET interview questions which are mostly asked to 1 year experience and above candidates.
So here is a simplified answer we have put in front with a diagram so that you can learn in a more easier manner.
Figure: - Life cycle of a project
There are five stages of any project initiating, planning, executing, controlling, and closeout. These are general phases and change according to domain. Example when writing a book we will have the following mappings initiating (contacting publishers, getting copy right etc), planning (Table of contents of book, Number of chapters, tool to use, chapter wise deadlines etc), executing (Actually writing the book), controlling (proof reading, language checks, page alignments etc), and closeout (Finally printing and on the shelf for sale). Therefore, this classification is at very broader level, for software development the above figure shows the mapping.
During Software project management interview, expected answer is requirement phase, design phase, coding phase, testing phase, and project closure. But you can just impress the answer by giving a general answer and then showing the mapping.
Also see a question for differences between Abstraction and Encapsulation based on OOPS as follows: -
Recently I was hunting about partial classes and the real time use of the same. Many of those postings found on google talked about the concept of partial classes and partial methods, but in what scenarios to use them very few highlighted.
In this article we will first start with fundamentals of partial classes and methods and then discuss the 4 real time use of the same.
I have also created a video of the same here where I have discussed about partial classes and shown the real time use of the same.
Fundamentals of partial classes
A partial class allows a single class to be divided in to two separate physical files. During compile time these files get compiled in to single class. For instance you can see in the below figure we have the customer class divided in to two different files “customer1.cs” and “customer2.cs”.
During compilation these files gets compiled in to single class internally. So when you create an object of the customer class you will be able to see methods lying in both the physical files. For instance you can see “Add” method belongs to “customer1.cs” and “Delete” method belongs to “customer2.cs” , but when the customer object is created we can see both “Add” and “Delete” methods.
Fundamentals of partial methods
There is one more important concept in partial classes called as partial methods. Partial methods helps us to define a method definition in one of the physical files and we can implement that method in the other physical files as shown in the below figure.
In the below figure you can see we have defined “Validate” method in “Customer1.cs” and this validate method is implemented in “Customer2.cs”. Please note the partial keywords attached to both of these methods.
Use number 1:- ASP.NET auto generated code
The biggest use of partial classes is in technologies where there is code generation. Microsoft team themselves use partial classes in ASP.NET, LINQ and EF code generation. For instance when we look at ASP.NET there are two parts one is the auto generated code of a page and the other is behind code where you write your custom logic.
The custom logic is written in the “.aspx.cs” file while the auto generated logic is in “.aspx.designer.cs” file as shown in the below figure.
As a developer you would like the auto generated code to do his work i.e. generate code when you drag and drop a button the ASP.NET designer.
Below is how the code snippet of the auto generated code looks like.
public partial class WebForm1 {
///
/// form1 control.
///
///
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
///
protected global::System.Web.UI.HtmlControls.HtmlForm form1;
///
/// Button1 control.
///
///
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
///
protected global::System.Web.UI.WebControls.Button Button1;
///
/// Label1 control.
///
///
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
///
protected global::System.Web.UI.WebControls.Label Label1;
}
At the same time you would also like to customize the code in some other file so that the auto generation part is not disturbed. For the same ASP.NET provides the “.aspx.cs” file which is a partial class where in you can go put your own custom logic.
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Your custom logic
}
}
This is only possible when the class is split in two physical files but united via the partial keyword. So if you see any ASP.NET behind code class files it’s marked by the word partial.
So by using the partial keyword in ASP.NET the Microsoft team has made VS and developer work side by side thus not fiddling with each other’s code and thus increasing productivity.
Use number 2:- LINQ and Entity framework
LINQ and EF also use partial classes and methods heavily because of the auto generation nature of these technologies. So when you drag tables in these framework they create auto generated classes as shown in the below figure.
In the below figure you can see how the auto generated code has partial classes and partial methods.
The partial methods later can be extended to put custom logic. For instance you can see in the below code for the above autogenerated class “tblCustomer” we have used partial methods to override the “OnCustomerCodeChanged” event to ensure that customer code is not more than 8 length.
public partial class tblCustomer
{
partial void OnCustomerCodeChanged()
{
if (_CustomerCode.Length > 8)
{
throw new Exception("Customer code can not be greater than 8");
}
}
}
So by using partial classes and partial methods, LINQ and EF framework keep auto generating classes and by using partial methods we can customize the class with our own logic.
Use number 3:- Better maintenance by compacting large classes
The other important use of partial classes is for better maintenance of the project. If you have large classes with lots of methods as shown in the below figure , it’s a bit pain to maintain those classes.
By using partial classes you can split them in to physical files as shown in the below figure thus making your project better and easy to maintain.
Use number 4:- Multiple people working on the same class
The last and final real time I see of partial classes is when we want simultaneously two developers to work in the same class. I agree this can be a very rare use as there are better options like using a version control software like TFS or Sub version, but in case you want something quick and local this option is not bad at all.
SQL Server interview questions: -Explain the relationship existing in database designing.
This is one of the most favorite SQL Server interview questions asked by the interviewer during the interview.
We have tried to keep answer as simple as we can. So starts the answer as, basically there are three major relationship models: -
One-to-one
Figure: - One-to-One relationship ER diagram
One-to-many
In this many records in one table corresponds to the one record in other table. Example: - Every one customer can have multiple sales. So there exist one-to-many relationships between customer and sales table.
One “Asset” can have multiple “Maintenance”. So “Asset” entity has one-to-many relationship between them as the ER model shows below.
Figure: - One-to-Many Relationship ER diagram
Many-to-many
In this, one record in one table corresponds to many rows in other table and also vice-versa. For instance :- In a company one employee can have many skills like java , c# etc and also one skill can belong to many employees.
Given below is a sample of many-to-many relationship. One employee can have knowledge of multiple “Technology”. So in order to implement this we have one more table “Employee Technology” which is linked to the primary key of “Employee” and “Technology” table.
Figure: - Many-to-Many Relationship ER diagram
Also see the following video on differences between unique key and primary key in SQL Server as follows: -
Many of the developers make a blind choice of choosing data grid directly, but that is not the right way.
Data grid provides ability to allow the end-user to sort, page, and edit its data. However, it comes at a cost of speed. Second, the display format is simple that is in row and columns. Real life scenarios can be more demanding that
With its templates, the Data List provides more control over the look and feel of the displayed data than the Data Grid. It offers better performance than data grid
Repeater control allows for complete and total control. With the Repeater, the only HTML emitted are the values of the data binding statements in the templates along with the HTML markup specified in the templates—no “extra” HTML is emitted, as with the Data Grid and Data List. By requiring the developer to specify the complete generated HTML markup, the Repeater often requires the longest development time. However, repeater does not provide editing features like data grid so everything has to be coded by programmer. However, the Repeater does boast the best performance of the three data Web controls. Repeater is fastest followed by Datalist and finally data grid.
See video on ASP.NET 4.0 web.config transformation as follows: -
In traditional ASP if you are told to create a login page and do authentication you have to do hell lot of custom coding. Now in ASP.NET that has made easy by introducing Forms authentication. So let us see in detail what form authentication is.
Forms authentication uses a ticket cookie to see that user is authenticated or not. That means when user is authenticated first time a cookie is set to tell that this user is authenticated. If the cookies expire then Forms authentication mechanism sends the user to the login page.
Following are the steps, which defines steps for Forms authentication:-
Configure Web.config file with forms authentication. As shown below in the config file you can see we have give the cookie name and loginurl page.
Remove anonymous access to the IIS web application, following are changes done to web.config file.
Create the login page, which will accept user information. You will have create your login page that is the Login.aspx, which will actually take the user data.
Finally a small coding in the login button.
Let us assume that the login page has two textboxes TX name and txtapssword.
Also, import System.Web.Security and put the following code in login button of the page.
See the following video on Single sign-on using forms authentication in ASP.NET as follows: -