Showing posts with label Interview Question. Show all posts
Showing posts with label Interview Question. Show all posts

Monday, March 16, 2015

How to create One ASP.NET application / What is One ASP.NET?

Did you ever face an interview question “What is One ASP.NET?” Did you ever create One ASP.NET web application? Do you know how to create One ASP.NET web application? All these questions will be answered here.

First lets find the answer for “What is One ASP.NET?”

As the name suggests One ASP.NET, it is a one ASP.NET project for multiple .NET supported technologies. Now you have MVC, WEB API, ASP.NET etc where you can create web application. How about creating one ASP.NET project and integrating all these technologies. Microsoft was reading our developers mind and they came up with ONE ASP.NET concepts in the same way what we all are thinking.

Now you just need to create one web project and then you can decide what technology you wanted to use at any time without any migration from one to another. Which means you can pick the technology at any time no need to decide everything in advance.

In order to create One ASP.NET project you should have Visual Studio 2013 and Update 1 or above.

Now, lets see how to create ONE ASP.NET project.

It is again the standard template as usual you see when you create new project from Visual Studio.

Lets see the step by step process to create the One ASP.NET project.

First thing is, if you don’t have .NET framework 4.5.1 and visual studio 2013 with minimum update 1 then install that.

Once you install the above Open Visual Studio 2013 and go to File—> New—>Project

image

Select Visual C# and then select the .NET framework 4.5 or 4.5.1.and then select the ASP.NET Web Application. As soon as you click on “ASP.NET Web Application” template you may see the description of this template in the right pane as using this you can create ASP.NET web forms, MVC etc, type of applications.

image 

Click OK to create the project and start your application as per your requirement.

I hope now you understood what is One ASP.NET and how to create One ASP.NET project using Visual Studio 2013.

Thanks a lot for reading my article. Keep visiting my blog!

Tuesday, March 10, 2015

Can we insert explicit values into IDENTITY column in MS SQL

This is one of my favorite interview question. I hope you also would have faced this question, if not then there is a high possibility that you may face it soon!
Answer to the question Can we insert explicit values into IDENTITY column is MS SQL is YES. You can insert values into the IDENTITY column as you require.

Lets start step by step.

First lets create a table for the demo purpose,
CREATE TABLE DemoIdentity
(
    Id int IDENTITY (1, 1) not null,
    Name varchar(50) not null,
    [Address] varchar (100) not null,
    Phone char (15) not null
)
 
Table is ready, now we will insert some values to the above table.
INSERT INTO [DemoIdentity] ([Name] ,[Address] ,[Phone]) VALUES ('Asheej','Bangalore','9916123456')
INSERT INTO [DemoIdentity] ([Name] ,[Address] ,[Phone]) VALUES ('David','Birmingham','7543789234')
INSERT INTO [DemoIdentity] ([Name] ,[Address] ,[Phone]) VALUES ('Cliff','Columbus','4081234567')

Above is the standard way to insert the values into the table which has IDENTITY column.
image
Now lets try to insert a value with identity column value,

 
INSERT INTO [DemoIdentity] ([ID], [Name] ,[Address] ,[Phone]) VALUES (4,'Johnson','Perth','61478976782')

If you execute above SQL statement you will get below error,
Msg 544, Level 16, State 1, Line 11
Cannot insert explicit value for identity column in table 'DemoIdentity' when IDENTITY_INSERT is set to OFF.

Yes, now we will see the property which MS SQL provides you to insert values in IDENTITY column.
In SQL we have the property named IDENTITY_INSERT. Whenever you wanted to insert values to an IDENTITY column we just need to enable the property IDENTITY_INSERT.
Lets enable this property and insert a value in above table.
Syntax for enabling the IDENTITY_INSERT is,
SET IDENTITY_INSERT [DemoIdentity] OFF

Now we will insert the value by executing the same above statement by specifying the IDENTITY column value and see the result.
 image
One important thing you have to remember is, in order to enable or disable the IDENTITY_INSERT  property you should be part of the sysadmin group.

Make sure you disable the IDENTITY_INSERT ON once you are done with the insert statement as this column is not intended to insert the values explicitly.
Syntax for disabling the IDENTITY_INSERT is,
SET IDENTITY_INSERT [DemoIdentity] ON
 
I hope now you understood the whole concept of the property IDENTITY_INSERT and how you can insert explicit values into an IDENTITY column.

Monday, February 16, 2015

Visual Studio In-built Support for Windows Azure

As you are aware Visual Studio has evolved a lot and every new release Microsoft adds many features. Since Windows azure is the hot cake in the market, of course Microsoft added many inbuilt features in Visual Studio 2013. Today we are going to explore few important features which will help you to debug your application.
 
Lets go through one by one, lets connect to Windows Azure website form Visual Studio.

Now you can directly connect to your Azure website from Visual Studio. IN order to connect to the windows azure, first you need to sign in to your Azure account. If you are already connected then you can directly open the server explorer form View Menu.

image

Once you open Server Explorer, you can find an option named “Azure”. You may expand the Azure to find the available options under that. This links nothing but what you see once you login to your Azure account.
 
You can compare the feature list below. Except Visual Studio online everything can be found there. Visual Studio online is a different configuration so that is available under Team explorer in Visual Studio.

 image image
Now what we are going to explore is the features available under Websites.
 
Lets expand the Websites, you will notice that all the websites which is already hosted in your Azure account will listed there.
image
We will concentrate on one website now and will see what we can do directly from Visual Studio.
 
Lets right click on one website and select View Settings. You will be amazed to see the options which is opened in your right panel. Lets explore one by one now.
image
First one you would see is the Stop Website and Restart Website. If the website is in the stopped state you will find an option Start Website in place of Stop Website. As you are aware you stop the application inn windows Azure as well same like the way you do it in IIS.

I believe nothing more to explain this feature as it has got its literal meaning.

Next options which shows under Website Settings are mainly for debugging.

First option is definitely the Framework version. You will have two options either V4.5 or V3.5

image
You may choose the framework version depends on the one you want to run your application.

Next four settings are related to remote debugging.
1. Web Server Logging
2. Detailed Error Messages
3. Failed Request Tracing
4. Application Logging (File System)
5. Remote Debugging

You may enable all or the require logging options in case you are facing any issues with the application. It is very easy to know what exactly is the issue your application is facing by going through this log files. As soon as you change this logging options it will reflect in Windows Azure as well. Which means instead of logging-in to your windows Azure  account you may manage all these options from Visual Studio itself.

I will be stopping today, there are much more to explain about the log files which you are going to get by enabling this logging mechanism.

I will be writing a separate articles for each logs.
Thanks a lot of reading my article.

Tuesday, November 18, 2014

TransactionScope in ASP.NET

This is one of the good feature which will help you when you have to do multiple database operations and you wanted to make sure that all the transaction has to be either committed or roll backed as a single unit of work.
TransactionScope is exactly same as the one we use in database level. But sometimes we may not use transaction at database level if the data/table we are operating is completely different and there is no connections.
Couple of important points you have to remember while using TransactionScope is, TransactionScope class cannot be inherited, also this is part of the System.Transactions which means if you wanted to use the class TransactionScope then first you have to refer to the assembly System.Transactions.
image

Friday, April 20, 2012

How to use IN Clause with parameter in MS SQL

 

You might be wondering how to pass the value as parameter in IN clause. It is a kind of tricky one. If you directly execute your select query by passing the parameter your query will through error.

Here we will see how it can be done,

First we will create the table

CREATE TABLE [dbo].[EMP](
    [ID] [int] NULL,
    [Name] [nchar](10) NULL
) ON [PRIMARY]
 
GO

Now we will insert some values to the table

INSERT INTO [Test].[dbo].[T] ([ID],[Name]) VALUES(1,'Mike')
INSERT INTO [Test].[dbo].[EMP] ([ID],[Name]) VALUES(2,'John')
INSERT INTO [Test].[dbo].[EMP] ([ID],[Name]) VALUES(3,'Russel')
INSERT INTO [Test].[dbo].[EMP] ([ID],[Name]) VALUES(4,'Thomas')
INSERT INTO [Test].[dbo].[EMP] ([ID],[Name]) VALUES(5,'David')
GO

Finally we will query the table using IN. I want all the Employee with the ID in 3,4,5 and our query for that should be

select * from t
 
Declare @id varchar(20)
set @id= '3,4,5'
exec('SELECT * FROM EMP WHERE ID IN('+@id+')')
 

Above query was useful if you wanted to use INT or any other numeric data type in your IN clause. What you will do if you wanted to use it for char data type?

Yes we will see that as well here.

Query for Char data type should be,

DECLARE @id VARCHAR(MAX)
set @id= 'Mike,John,David'
SELECT @id = '''' + REPLACE(@id,',',''',''') + ''''
EXEC('SELECT * FROM EMP WHERE Name IN('+@id+')')
GO

Result after executing above query for Int data type,

Query

Result after executing above query for CHAR data type,

Query1

Wednesday, April 11, 2012

How to call Stored Procedure from Trigger in MS SQL

 

Another interesting Interview question “Can we call Stored procedure form Trigger in MS SQL”? What will be your answer YES or NO? Answer should always be “YES”.

You can call Stored Procedure from trigger. In this article we will go through a simple After Insert trigger as an example.

create trigger trg_Ins_CustTrigger on Customer
After Insert 
AS
Declare @Name as varchar(100)
Declare @Address as varchar(100)
select @Name=Name, @Address=Address from inserted
exec CustDet @Name, @Address
 

I believe above code is self explanatory as the table and field names I have used is a clear English text.

If you have any question please post it in comment section.

Thursday, March 22, 2012

How to list all the constraints in SQL Server Database

 

There are many methods to lists the constraints in SQL database. We will go through few methods one by one.

Method 1:

SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS

Above query will return all the constraints in all the database.

Method 2:

SELECT * FROM [DataBaseName].INFORMATION_SCHEMA.TABLE_CONSTRAINTS

Above query will return all the constraints in the specific database.

Method 3:

SELECT OBJECT_NAME(OBJECT_ID) AS [Constraint_Name],
OBJECT_NAME(parent_object_id) AS [TableName],
type_desc AS [TypeOfConstraint]
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'

This method will give bit more details about the constraints. This can be used to return all constraints in the database with Constraint Name, Table Name and Type of Constraint.

Method 4:

sp_help TableName

This query will return all the constraints in the specific table.

Sunday, January 22, 2012

Can we have primary key and clustered index on same table with different field?

In this article I am going to explain what will happen if we try to create primary key on a table which is already having clustered index in it.

Also this article is the answer to the question "Can we have primary key and clustered index on same table with different field?"

Read my article from dotnetspider

Thursday, July 14, 2011

How to check SQL table exists or not, if exists delete the same and create a new one

Nowadays it became my habit that I am posting the code or article only when people ask the questions in forums.

This is one another regular question I used to see in forums "How to check SQL table exists or not, if exists delete the same and create a new one".

What normally people suggests is query the table and if nothing returned then it doesn't exists, but if you do that execution will take some time.
But if you check directly the Object ID of the table it will be much quicker.

Below is a simple query which helps you to check whether table exists or not in the database and if table exists then this query drops the table and create a new one with the same name.
IF OBJECT_ID('dbo.Users') IS NOT NULL
DROP TABLE dbo.Users 
GO
 
CREATE TABLE dbo.Users 
(
  ID INT,
  UserID Varchar(50)
) 
GO
 
select * from Users  
GO
I believe above code is self explanatory and you don’t need much explanation about that. Happy to provide more details if you have any questions.

Saturday, January 10, 2009

When to Use Delegates Instead of Interfaces (C# )


Both delegates and interfaces allow a class designer to separate type declarations and implementation. A given interface can be inherited and implemented by any class or struct; a delegate can created for a method on any class, as long as the method fits the method signature for the delegate. An interface reference or a delegate can be used by an object with no knowledge of the class that implements the interface or delegate method. Given these similarities, when should a class designer use a delegate and when should they use an interface?
Use a delegate when:
•    An eventing design pattern is used.
•    It is desirable to encapsulate a static method.
•    The caller has no need access other properties, methods, or interfaces on the object implementing the method.
•    Easy composition is desired.
•    A class may need more than one implementation of the method.
Use an interface when:
•    There are a group of related methods that may be called.
•    A class only needs one implementation of the method.
•    The class using the interface will want to cast that interface to other interface or class types.
•    The method being implemented is linked to the type or identity of the class: for example, comparison methods.
One good example of using a single-method interface instead of a delegate is IComparable or IComparable. IComparable declares the CompareTo method, which returns an integer specifying a less than, equal to, or greater than relationship between two objects of the same type. IComparable can be used as the basis of a sort algorithm, and while using a delegate comparison method as the basis of a sort algorithm would be valid, it is not ideal. Because the ability to compare belongs to the class, and the comparison algorithm doesn’t change at run-time, a single-method interface is ideal.

Thursday, January 8, 2009

Advantages of Using Globally Unique Identifier (GUID) as a Primary Key.

Advantages of Using Globally Unique Identifier (GUID) as a Primary Key Advantages:
 
■ If you use an auto-number Id column and if many people are creating new DataRow objects, it will be difficult for you to merge data later because there may be many duplicate keys.

■ The Guid data type is a “surrogate” key, meaning that its only purpose is to define uniqueness of the row and aid in connecting multiple tables together via relationships. This means that there is no reason for a user to see and change this value, which simplifies maintenance of the DataSet. If you allow the user to change the primary key on a row, you have to propogate the change down to all of the related tables. For example, changing the CompanyId value requires the update of the Company Id value in the Employee table.

■ The use of the Guid can simplify the joining of tables, which is better than the scenarios where you use compound keys that are based on the actual data. Compound keys typically result in smaller data footprints because the key is based on actual data, whereas joining tables is usually more difficult because compound joins are required. Remember that if you are using compound keys that are based on the actual data, you inevitably need to deal with recursive updates.

Advantages of JQuery? or Why we Use JQuery?

The first thing that most Javascript programmers end up doing is adding some code to their program, similar to this:
window.onload = function(){ alert("welcome"); }
Inside of which is the code that you want to run right when the page is loaded. Problematically, however, the Javascript code isn't run until all images are finished downloading (this includes banner ads). The reason for using “window.onload” in the first place is due to the fact that the HTML 'document' isn't finished loading yet, when you first try to run your code.
To circumvent both problems, here comes jQuery...Write Less, Do More, JavaScript Library.
   1:  <HTML>
   2:  <head>
   3:   
   4:  <!--download jquery.js and use the same version.
       here i used "jquery-1.2.6.min.js"-->
   5:  <script type="text/javascript" 
       src="jquery-1.2.6.min.js"></script>
   6:   
   7:  <script type="text/javascript">
   8:   
   9:  //Mandatory function to start jquery on load.
  10:  $(document).ready( function() {
  11:   
  12:  //below line will apply style to all paragraph so 
       this should come first. 
  13:  //Try moving this line below the paragraph by id tag
       style and see the difference.
  14:  $("p").css( {color: "yellow", background: "green"} ); 
  15:   
  16:   
  17:  //Firsr and second are the name of the paragraph.
  18:  $("#First").css( {color: "red",
       background: "yellow"} );  
  19:  $("#Second").css( {color: "white",
       background: "black"} );  
  20:   
  21:  //another function to play around the anchor <a> tag
  22:     $("a").click(function(event)
  23:      {
  24:        alert("Welcome to asheej.blogspot.com");
  25:   
  26:  //comment the above single line and uncomment below
       three line and see what JQyery can do.
  27:   
  28:  //      alert("As you can see, the link no longer took
       you to jquery.com");
  29:  //      event.preventDefault();
  30:  //        $(this).hide("slow");
  31:   
  32:       }
  33:       );
  34:   
  35:  });
  36:  </script>
  37:   
  38:  <head/>  
  39:   
  40:  <BODY>  
  41:   
  42:      <DIV style="border: 3px solid Green;">  
  43:      <A href= "http://asheej.blogspot.com/">Google</a>  
  44:      </DIV>  
  45:   
  46:      <P id="First">This is First paragraph</p>  
  47:      <P id="Second">This is Second paragraph</p>  
  48:      <P>This is Third paragraph</p>  
  49:      <P>This is Fourth paragraph</p>  
  50:   
  51:  </BODY>  
  52:  </HTML>  

Sunday, July 6, 2008

Difference between Copying and Cloning the DataTable

Copying and Cloning the DataTable: You often need to create a full copy of a DataTable in your application, possibly to pass it to another application, or to use as a scratch pad for operations that may be thrown out later.

For example, you may want to assign a DataTable object to a GridView control to allow a user to edit the data, but you also may want to provide a cancel button that aborts all changes on the Web page.

A simple way to implement this functionality is to create a copy of your DataTable object and use the copy for editing. If the user clicks the cancel button, the DataTable copy is thrown out.

If the user decides to keep the changes, you can replace the original DataTable object with the edited copy.

To create a copy of a DataTable object, use the Copy method on the DataTable, which copies the DataTable object schema and data.

The following code snippet shows how to invoke the Copy method:
'VB
Dim copy as DataTable = employee.Copy( )
 
//C#
DataTable copy = employee.Copy( );

You often require a copy of the DataTable schema without the data. You can accomplish this by invoking the Clone method on the DataTable. Use this method when an empty copy of the DataTable is required and to which DataRow objects will be added at a later time.

The following code shows the Clone method:

'VB
Dim clone as DataTable = employee.Clone( )
//C#
DataTable clone = employee.Clone( );