Tuesday, May 31, 2011

.NET/ASP.NET interview Questions - Show us simple insert, update and delete operation using LINQ?

Answer:

Let us assume that we have the following table of Student with their respective fields.




First open visual studio > New > project > select ASP.NET empty web
application.

Add > New item > select Web Form and create a simple form like below.




Once you have done creating the form, now you have to add LINQ to SQL Classes in your project.
Add > New item > select LINQ to SQL Classes.
As soon as you click on add a new window will appear from that just click on server explorer expand the data connection and select your database then drag and drop the table on the .dbml file like below diagram.



Now you have to add a class library file in your project.
Add > New item > select Class.

usingSystem.Data.Linq;
usingSystem.Data.Linq.Mapping;

namespaceLinq_to_insert
{
 [Table(Name="Student")]
publicclassClass1
 {
privatestring _Name;
privatestring _Add;

publicstring Name
     {
get{ _Name = value;}                                               
get{return _Name;}
      }
publicstring Add
     {
set{ _Add = value; }
get{return _Add;}
     }
 }
}


Write code for insert update and delete with their respective buttons.
For adding record.

protectedvoidbtnAdd_Click(object sender, EventArgs e)
     {
DataClasses1DataContextObjDataContext = newDataClasses1DataContext();
StudentObjStud = newStudent();
ObjStud.StudentAdd = TextBox2.Text;
ObjStud.StudentName = TextBox1.Text.ToString();
ObjDataContext.GetTable().InsertOnSubmit(ObjStud);
ObjDataContext.SubmitChanges();
Response.Write("Record Successfully Inserted");

     }


For updating record.
protectedvoidbtnUpdate_Click(object sender, EventArgs e)
     {
DataClasses1DataContextObjDataContext = newDataClasses1DataContext();
StudentObjStud = newStudent();
string Name1 = TextBox1.Text;
string Address = TextBox2.Text;
var Update = ObjDataContext.Students.Single(p =>p.StudentName == Name1);
Update.StudentAdd = Address;
ObjDataContext.SubmitChanges();
Response.Write("Record Successfully Updated");

     }


For deleting record.
protectedvoidbtnDelete_Click(object sender, EventArgs e)
     {
DataClasses1DataContextObjDataContext = newDataClasses1DataContext();
StudentObjStud = newStudent();
string Name1 = TextBox1.Text.ToString();
var delete = ObjDataContext.Students.Single(s =>s.StudentName == Name1);
ObjDataContext.Students.DeleteOnSubmit(delete);
ObjDataContext.SubmitChanges();
Response.Write("Record Successfully deleted");


     }

You will be also interested in watching the below video, which are also asked in most of the interviews and favourable question of interviewers.




Please click here to see more .NET and ASP.NET interview questions
Regards,

Visit Authors blog for more .NET and ASP.NET interview Question


Monday, May 30, 2011

.NET and SQL Server interview questions - Can you describe Sql Server Views are updatable?

Answer:

View is a virtual table, which can contains data (rows with columns) from one or more table and you can retrieve data from a view.

Let’s demonstrate a simple example in which we will see that how to create a view on single table and will also see that if we update the view the respective table on which the view is created is updated or not.


Now let first see how to create view.

Go to View folder in SQL Server > Right click on it > select New View.





As soon as you click on New View, the following window will appear like below.




Now, just select the table name from the list on which you wish to create a View and Click on Add then click on close. Once you click on close a new window will appear which allow you to create View on the respective column.



After selecting the column name just save the view and give View a nice name.


Once you have completed the above step you will see that the respective View is added in the View folder.


Now let’s see that when we update the view the respective table is also updated or not.

Query:-

Update [Practice].[dbo].[Cust_View] set Customer_Contact = 96641122 where
Customer_Name = 'Feroz'
Now just go to the table on which the view was created and check whether the table is updated or not, you will see that the table is also updated when you update the View.
Now let’s create a view based on two tables and try to update a view.

create view View_Cust as SELECT    dbo.Customer.Customer_Name, dbo.Customer.
Customer_Contact,dbo.[Order].Product_Name,dbo.[Order].Price
FROM dbo.Customer
INNER JOIN dbo.[Order] ON dbo.Customer.Order_ID = dbo.[Order].Order_ID

Let’s try to Update View:


Query:-
Update [Practice].[dbo].[View_Cust] set Customer_Contact = 098767554,
Price = 4000 where Customer_Name = 'Feroz'

As you can see in the above query, I am trying to update a column from the Product table and another column from the Order table and try to execute the query the compiler will throw the below error.

Error Message:- View or function 'Practice.dbo.View_Cust' is not updatable because the modification affects multiple base tables.

This means that when you try to update both the table’s column from the view then it is not allowed but you can update single table column.

Please click here to see more .NET and SQL Server interview questions
Regards,

Visit Authors blog for more .NET and SQL Server interview Question


Sunday, May 29, 2011

.NET and SQL Server interview questions - Show SQL query in order to obtain names of employees with top 10 salaries?

Answer:

Let’s see a simple example to see, how exactly we can obtain entire employee
name who obtains top 10 salaries.

Assuming that we have following table for Employee.




Query:-

select Employee.EmpName,Employee.EmpSalary from Employee where Employee.EmpSalary in
(select distinct top(10)Employee.EmpSalary from Employee order by Employee.EmpSalary desc)

OUTPUT:-




You will be also interested in watching the below video, which are also
asked in most of the interviews and favourable question of interviewers.




Please click here to see more .NET and SQL Server interview questions

Regards,

Visit Authors blog for more .NET and SQL Server interview Question

Friday, May 27, 2011

ASP.NET/.NET interview Question - Encryption of connection strings in web.config file?

Answer:

When you host your site on server, sometimes happens that your site is handled by an third party or any other individual who is responsible to handle the server. As your site is handled or manage by another person it can be possible for that person to view your connection string and can make changes to the same. So in order avoid this we need to encrypt your connection string.

This can be achieved by using aspnet_regiis tool provided by the ASP.NET.

It’s a very simple 3 steps process.



Step 1:- Let first define the connection string in web.config file like below
code snippet.


<connectionStrings>

<add name="Constr" connectionString="Data Source=localhost;Initial
Catalog=YourDataBaseName; Integrated Security=True"/>

</connectionStrings>

In the above snippet code you can see that the connection string easily visible by anyone because it is in decrypted format. Hence to prevent or protect the connection string we need to encrypt connection string in such a way that it is not visible to anyone.


Step 2: - Just go to Visual Studio Command Prompt and use aspnet_regiis tool to encrypt the defined connection string like below diagram.



Now just execute the Visual Studio Command Prompt and if the encryption is done successfully a message will be displayed like below diagram.



Step 3:- Go to web.config file and you will see that connection string is now in encrypted format like below diagram.




You will be also interested in watching the below video, which are also asked in most of the interviews and favourable question of interviewers.


Please click here to see more ASP.NET and .NET interview questions
Regards,

Visit Authors blog for more ASP.NET/ .NET interview questions

Thursday, May 26, 2011

.NET and ASP.NET interview Question - Can you explain threading and show its practical implementation?

Threading is a parallel processing unit and helps you to access multiple tasks at a one moment of time. When we want to access parallel processing in our application we use Threading.
In order to use threading we first have to import System.Threading namespace in our application like below code. using System.Threading;
Below is the simple class in which I have defined two functions as Fun1 and Fun2.i have also created a DoTest function with an instance of thread as t1 and t2.finally I have invoked DoTest function in Main Method.
class Program 
{
public void Fun1()     
{
 for (int i = 0; i < 10; i++)        
{             
Console.WriteLine("i"+"="+i.ToString());             
Thread.Sleep(500);        
}      
}
public void Fun2()      
{
 for (int j = 0; j < 10; j++)          
{              
Console.WriteLine("j" + "=" +j.ToString());            
Thread.Sleep(500);     
}       
}
 static void Main(string[]args)      
{
  Program p = new Program();       
  p.DoTest();
  Console.ReadLine();      
}
 public void DoTest()      
{
 Thread t1 = new Thread(new ThreadStart(Fun1));    
 Thread t2 = new Thread(new
 ThreadStart(Fun2));
 t1.Start();
 t2.Start();      
}
}

Once you have completed the above steps just run your application and you will see the output like below diagram.
Output:



You can see that in the above diagram both the function Fun1 and Fun2 run at single moment of time with one after another and display their respective outputs.
You will be also interested in watching the below video, which are also asked in most of the interviews and favourable question of interviewers.




Please click here to see more .NET and ASP.NET interview questions

Regards,

Visit Authors blog for more .NET and ASP.Net interview questions