Monday, October 31, 2011

.NET interview questions: - Parse data in Threading.

This is a semi-asked .NET interview questions and has been asked in most of the interviews to check your skills on parsing data in threading.

As a senior developer you would be interested to know how exactly we can parse data in threading.
Below is the code snippet for simple use of threading: -

    class Program
    {
        static void Main(string[] args)
        {
            Thread objThread = new Thread(CallMe);
            objThread.Start();
        }
        public static void CallMe()
        {
            for (int i = 0; i <= 5; i++)
            {
                Console.WriteLine("Threading.......");
                Thread.Sleep(2000);
            }
        }
    }

In the above code snippet you can see that I have created a “CallMe” function which has a for loop and getting executed till the value of “i” is 5 and simply print a line of text to the output screen after every 2 seconds using Thread.Sleep(2000);.

Now, when you execute this above code snippet you will see result like below diagram.


The above code snippet seems to be works fine but what if there is a situation that you want to execute for loop according to the data passed by the Thread.

The data can be parsed in threading by the following two ways

1. You can parse the value by defining object variable.


2. You can use Lambda Expression.

So, let’s take the above point’s one by one try to understand it in better manner.

In order to see it practically you just need to follow the following steps.

Step1: - Create a simple Console Application for that just open Visual Studio >> go to >> File >> New >> Project >> Windows >> Select Console Application.




Step2: - let us first prove the point of passing the data using object variables.

Now, simply just add the below code snippet in to program.cs file of your Console Application.


    class Program
    {
        static void Main(string[] args)
        {
            Thread objThread = new Thread(CallMe);
            //Passing the value 5.
            objThread.Start(5);
        }
        //Created a CallMe function with a Object parameter "k".
        public static void CallMe(object k)
        {
            //creted a int type variable and type caste it.
            int j = (int)k;
            for (int i = 0; i <= j; i++)
            {
                Console.WriteLine("Threading.......");
                Thread.Sleep(2000);
            }
            Console.ReadLine();
        }
    }

Now, just simply execute your console application and will see result like below diagram.


This is how you can parse the data using Object Variables.

Step3: - similarly, let’s take the second point of parsing the data using Lambda Expression.

Now, simply just change your program.cs file code snippet like below code snippet.

    class Program
    {
        static void Main(string[] args)
        {
       //Created a Lambda Expression and parse the value as 7.
           Thread objThread = new Thread(() => CallMe(7));
            objThread.Start();
        }
     //Created a CallMe function with a int parameter "k".
        public static void CallMe(int k)
        {
     //Created a int type variable which hold the value of k.
            int j = k;
            for (int i = 0; i < j; i++)
            {
                Console.WriteLine("Threading.......");
                Thread.Sleep(2000);
            }
            Console.ReadLine();
        }
    }

In the below diagram you can see that how I have used the Lambda Expression in order to 
parse the data in Threading.


Now, when you run your Console Application you will see the result like below diagram.



View the following video on thread, background thread and foreground thread in .NET: -



Click for more Dotnet interview questions and answers

Regards,

Visit for more author’s blog on Most asked Dotnet interview question

Friday, October 28, 2011

ASP.NET interview questions: - Unique Code in ASP.NET.

This is one of the requirements that would come across you many a times while working on .NET projects. Some of the senior developers will find this question very easy but many of the developer friends find it difficult to answer the above question.

So we have tried to give an answer for this ASP. NET interview questions in the practical form as follows: -

So take a small example to see how exactly we can create a Unique Code like C001, C002……., C00n. 
In order to see it practically you just need to follow the following steps: -

Step1: - Create an ASP.NET Web Application for that just open Visual Studio >> go to >>File >> New >> Project >> Web>> Select ASP.NET Empty Web Application. 




Step2: - Now simply just add a Web Form in to your Web Application for that just go to Solution Explorer >> Right click on the Project name>> ADD >> New Item>> Select Web Form.
Step3: - Assume that we have the table of Customer like below diagram.


Step4: - Design your Web Form like below diagram.



Also add a GridView Control to your WebForm.aspx page.


Step5: - This is the most important step for creating Unique Customer code.
Add the below code snippet in to your WebForm.aspx.cs file.
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AutoGenerateCode();
            }
        }
        public void AutoGenerateCode()
        {
            string Unique Code = "";
            SqlConnection con = new SqlConnection(str);
            con.Open();
            SqlCommand com = new SqlCommand();
            com.CommandText = "select CustCode from Customer where CustomerID in (select max(CustomerID) from Customer)";
            com.Connection = con;
            SqlDataReader objRead = com.ExecuteReader();
            if (objRead.Read())
            {
                UniqueCode = objRead[0].ToString();
                string sd = UniqueCode.Remove(0, 1);
                int sd1 = Int32.Parse(sd) + 1;
                UniqueCode = sd1.ToString();
                if (UniqueCode.Length == 3)
                {
                    UniqueCode = "C" + UniqueCode;
                }
                if (UniqueCode.Length == 2)
                { 
                    UniqueCode = "C" + "0" + UniqueCode; 
                }
                if (UniqueCode.Length == 1)
                {
                    UniqueCode = "C" + "0" + "0" + UniqueCode; 
                }
            }
            else 
            {
                UniqueCode = "C001"; 
            }
            objRead.Close();
            con.Close();
            TextBox3.Text = UniqueCode;
            //The below line will not allow 
            //the user to modify the Unique Code.
            TextBox3.ReadOnly = true;
        }
Once you have done with all the above steps simply run the application and will see the result like below diagram.

In the above code snippet you can clearly see that the Unique Customer Code is been generated.

See the following video on ASP.NET Authentication, Authorization, Principal and Identity objects as follows: -



Visit for more ASP.NET interview questions

Regards,

Get more from author’s blog for ASP.NET interview questions.

Tuesday, October 25, 2011

ADO.NET interview questions: - What are the ways to implement optimistic locking in ADO.NET?

An interesting ADO.NET interview questions mostly asked by the interviewer during an interview. Following are the ways to implement optimistic locking using ADO.NET: -

  • When we call “Update” method of Data Adapter it handles locking internally. If the Dataset values are not matching with current data in Database, it raises concurrency exception error. We can easily trap this error using Try. Catch block and raise appropriate error message to the user.
     
  • Define a Date time stamp field in the table. When actually you are firing the UPDATE SQL statements, compare the current timestamp with one existing in the database. Below is a sample SQL which checks for timestamp before updating and any mismatch in timestamp it will not update the records. This I the best practice used by industries for locking.
   Update table1 set field1=@test where Last Timestamp=@Current Timestamp
  • Check for original values stored in SQL SERVER and actual changed values. In stored procedure check before updating that the old data is same as the current Example in the below shown SQL before updating field1 we check that is the old field1 value same. If not then some one else has updated and necessary action has to be taken.
Update table1 set field1=@test where field1 = @oldfield1value




Locking can be handled at ADO.NET side or at SQL SERVER side i.e. in stored procedures.

 Know more on UML’s object diagram by viewing the following video: -



For more on ADO.NET interview questions, so click and visit.

Regards,

Author’s more on ADO.NET interview questions.

Saturday, October 22, 2011

ASP.NET interview questions: - How to make GridView Editable, Updateable and Deleteable in ASP.NET?


The process of making GridView Editable, Updateable and Deleteable is almost used in the entire ASP.NET project. So, making GridView Editable, Updateable and Deleteable is one of the most important aspects according to any ASP.NET project.

Let’s create a simple demonstration to understand the concept of making GridView Editable, Updateable and Deleteable in much better manner.

In order to see it practically you just need to follow the following steps.

Step1: - Create a simple ASP.NET Web Application for that just open Visual Studio >>go to >> File >> New >> Project >> Web >> Select ASP.NET Empty Web Application.



Step2: - Now, simply just add a Web Form to your ASP.NET
Empty Web Application for that just go to >> Solution Explorer >> Right Click on the project name >> Add >> New Item >> Select Web Form.


Now, simply just drag and drop GridView to your Web Form and allow the below
properties to true.

1. Allow AutoGenerateEditButton to True.

2. Allow AutoGenerateDeleteButton to True.


Now, as soon as you set the above two properties you will see the GridView like below diagram.


Step3: - Now, let bind the GridView with Data for that just add the below
code snippet in to your WebForm.aspx.cs file.

        //Below variable holds the Connection String.
string str = ConfigurationManager.AppSettings["Connect"];

protected void Page_Load(object sender, EventArgs e)
 {
  if (!IsPostBack)
  {
      GridViewData();
  }
}

//Created a GridViewData method to bind data to GridView
//from the SQL Server DataBase Table.
public void GridViewData()
{
  SqlConnection con = new SqlConnection(str);
  con.Open();
  SqlCommand com = new SqlCommand();
  com.CommandText = "select * from Book";
  com.Connection = con;
  com.ExecuteNonQuery();
  SqlDataAdapter adap = new SqlDataAdapter(com);
  DataSet ds = new DataSet();
  adap.Fill(ds);
  GridView1.DataSource = ds;
  GridView1.DataBind();
}

Step4: - This is the most important step while making GridView Editable, Updateable and Deleteable.

Now, simply just add the below Events of the GridView control in to your
Web Application.

1. OnRowEditing.

2. OnRowCancelingEdit.

3. OnRowUpdating.

4. OnRowDeleting.


Step5: - now, just add the below code snippet to make necessary changes on the respective events of the
GridView control.

1. OnRowEditing add the below code snippet.

 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
 {
  GridView1.EditIndex = e.NewEditIndex;
  GridViewData();
  }

2. OnRowCanelingEdit add the below code snippet.

 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
  GridView1.EditIndex = -1;
  GridViewData();
}

3. OnRowDeleting add the below code snippet.

 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
  //The below line of code will hold the Cells[1] data of th GridView Control.
  string id = GridView1.Rows[e.RowIndex].Cells[1].Text;
  SqlConnection con = new SqlConnection(str);
  con.Open();
  SqlCommand com = new SqlCommand();
  com.CommandText = "delete from Book where Book_Id = '"+id+"'";
  com.Connection = con;
  com.ExecuteNonQuery();
  GridViewData();
 }

4. OnRowUpdating add the below code snippet.

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  string id = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
  string bookname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;            string author = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
  string price = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
  string quantity = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
  SqlConnection con = new SqlConnection(str);
  con.Open();
  SqlCommand com = new SqlCommand();
  com.CommandText = "update Book set Book_Name='" + bookname + "',Book_Author='" + author + "',Book_Price='" + price + "',Book_Quantity='" + quantity + "' where Book_Id= '" + id + "'";
  com.Connection = con;
  com.ExecuteNonQuery();
  GridViewData();
}

Note: - You can modify your code according to your requirements.

Step6: - Now, let’s run your Web Application and see the respective results.

Let’s first see the result for deleting.


The above diagram is my output of the loaded GridView and note that I am going to delete the
circled row data from the GridView control.

Now, as soon as you click on the delete link will see the result like below diagram.




In the above result of diagram you can see that the data has been deleted.

Similarly, let’s see the result for updating the below circled row data in the
GridView control.


Now, as soon as you click on the edit link you will see something like below diagram.


Now, let’s modify the Book_Author Name as Kalim Shaikh and click on the Update link and see whether the data is updated or not.


In the above output diagram you can clearly see that now the Book_Author name is modified to
Kalim Shaikh.

See the following video on ASP.NET Tracing and instrumentation.



Visit for more ASP.NET interview questions.

Regards,

For more author's blog on ASP.NET interview questions

Thursday, October 20, 2011

C# interview questions: - What is the difference between “int” and “Int32” in C#?

What is the difference between “int” and “Int32” in C#?


for more .net and c# interview questions videos click on .NET and c# interview questions

.NET interview questions: - Uses of the Params Keyword in C#.

This is the one the typical .NET interview questions and also the favorable question of the interviewers.

Params: - The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.

So, let’s take a scenario where we can have the use of the Params keyword.
Below is the scenario for the same.

    class Program
    {
        static void Main(string[] args)
        {
            int y = Add(10, 20);
            Console.WriteLine("The Addition of two Numbers is : " + y);
            Console.ReadLine();
        }
        static int Add(int Num1, int Num2)
        {
            return Num1 + Num2;
        }
    }

In the above code snippet you can see that I have created a function “Add”
which takes two parameters as “Num1” and “Num2” and return the addition of two numbers. The value for the 
parameters are been passed from the Main and it simply print the output to the screen of the “y” variable.

Now, simply just run your Application and will see the result like below diagram.



The above code snippet seems to be working fine but what if want to pass multiple arguments at a single instance. So, the Params Keyword helps us to achieve the above scenario in much better manner.

Let’s create a simple example to understand the use of Params Keyword in much better manner.
In order to perform practically you just need to follow the following steps.

Step1: - create a simple Console Application for that just open Visual Studio go to >> File >> New >> Project >> Windows >> Select Console Application.





Step2: - Now, simply just add the below Code Snippet in to Program.cs file of your Console Application.


    class Program
    {
        static void Main(string[] args)
        {
//Passed 5 different values.
            int y =  Add(10,20,30,40,50);
            Console.WriteLine("The Addition of  Numbers are : " + y);
            Console.ReadLine();
        }
//Created a listNumbers variable as declared as Params.
        static int Add(params int[] listNumbers)
        {
            int Total = 0;
            foreach (int i in listNumbers)
            {
                Total = i + Total;
            }
            return Total;
        }
    }

In the above code snippet you can see that I have created a function “Add” with a Variable “listNumbers” declared using params.

Now, simply just run your Console Application and will see the result like below diagram.


In the above diagram of output you can see the addition of the five numbers using params keyword.

If not understood from the above article, then see the following video on params keyword: -



Get our articles on  Most asked Dotnet interview question for preparation.

Regards,

See author’s other important Dotnet interview questions

Wednesday, October 19, 2011

Learn .NET in 60 days - Day 1

This class is for beginners who want to learn .NET.

for more .net and c# interview questions videos click on .NET and c# interview questions

Test yourself for Sharepoint 2010 interviews

This test will help you to check if u will ready for Sharepoint 2010 interviews,try to get 100% marks to ensure you are completely ready.

Best of luck


for more .net and c# interview questions videos click on .NET and c# interview questions

.NET and c# interview question practice test - 1

.NET and c# interview question practice test.
for more .net and c# interview questions videos click on .NET and c# interview questions

ASP.NET interview questions test :- ASP.NET Page life cycle

Arrange the page life cycle sequence in a proper manner. Hint :- SILVER
for more .net and c# interview questions videos click on .NET and c# interview questions

C# and .NET interview questions: - Explain with practical Lambda Expression in C#?

Lambda expression: - A Lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.
In simple words Lambda expression is nothing but it is an extended version of delegates or in other words it making delegates simpler.

All Lambda expression uses the lambda operator (=>).

The left side of the lambda operator specifies the input parameters (if any) and the right side hold the expression or statement block.

Let’s create a simple example to understand the concept of Lambda expression in much better manner.

In order to see it practically you just need to follow the following steps.

Step1: - Create a simple Console Application for that just open Visual Studio >> go to >> File >> New >> Project >> Windows >> Select Console Application.

lambda


lambda
Step2: - Now, simply just add the below code snippet in to program.cs file of your Console Application.
class Program
 {
     //created a delegate.
     delegate void PointToCallMe(string str);
     static void Main(string[] args)
     {
         //Delegate,which points to the CallMe Function.
         PointToCallMe objDelegate = new PointToCallMe(CallMe);
         objDelegate.Invoke("Called from Delegate");
         Console.ReadLine();
     }
     public static void CallMe(string str)
     {
         Console.WriteLine(str);
     }
 }
In the above code snippet you can see that I have created a function CallMe with a string parameter “str” and I have created a delegate which points toward the CallMe function.
Now, simply just run your console application and you should see the result as shown in the below diagram.
lambda

The above code is nice it work’s properly but the concern here is the code complexity; you can see that for a simple line of output we have created extra function, which makes your code little lengthy and not very easy to read.
So Lambda Expression helps us to solve the above problem in simplified manner and making our code more readable and understandable.
Let’s see how exactly Lambda Expression help to solve the above problem for that you just need to follow the following step.
Step3: - Now, just simply modify the program.cs file like below code snippet.
    class Program
 {
     //created a delegate.
     delegate void PointToCallMe(string str);
     static void Main(string[] args)
     {
         //Lambda Expression.
         PointToCallMe objLambda = str => Console.WriteLine(str);
         objLambda.Invoke("Called from Lambda");
         Console.ReadLine();
     }
 }
Now, in the above code snippet you can see that I have just eliminated the extra created CallMe function and just written a few line of Lambda Expression, which makes your code more readable and understandable with a few line of code.
Now, simply just run your console application and will see result like below output diagram.

lambda
Watch the following interesting video on threading in C#.NET: -


Get our more important Dotnet interview questions  materials.

Regards,

Visit more author's other Most asked Dotnet interview question

Monday, October 17, 2011

C# and .NET interview questions: - What is the need of Casting in C#? What is casting?

This is one of the typical .NET interview questions and has been asked in many of the mid .NET and c# interviews, also the favorable question of the interviewers where they check your skills on casting. Many of our developer friends who are having 1 to 2 years of experience in development also fail to answer this question.

So, let me explain first explain the need of casting.

Let’s create a small and simple demonstration in order to understand the need of
casting in much better manner.

In order to see it practically just follow the following steps.

Step1: - Create a Console Application for that just open Visual Studio >> go to >> File >> New >> Project >> Windows >> Select Console Application.








Step2: - Now, simply just add the below code snippet in to program.cs file of your Console Application.

static void Main(string[] args)
{
int i = 45.12;
}

In the above code snippet you can see that I have created an “int” variable “i” and we are trying to move data with decimal part in to the ‘int’ data type.
As you know that the “int” data type can only contain positive or negative value in it and not the decimal part.

Now if you try to compile the code you should see the below error message.






In the above error message diagram you can clearly see that the compiler says that it cannot implicitly convert type ‘double to int’ and are you missing a cast.

So how do we solve the above problem, simple by using CASTING.

Casting: - In simple words casting is nothing but converting data of one data type to another data type.

In order that the above code works you need to specify the integer casting as show in the below code snippet.

static void Main(string[] args)
{
int i = (int) 45.12;
}

When you compile the above code snippet you will find that the compilation has been done successfully like below diagram.




Now the next question which will come to your mind , ‘int’ says he will not take decimals and now the above code is compiling and it’s trying to push decimal data in to INT , so what will get stored in to ‘i” variable.

Now if we put a debug point and see the data in add watch as shown in the below images.







You can clearly see that the value of “i” variable is 45. Which means the fraction part has been eliminated: in other words there has been data loss.

In other words when we do casting there can be loss of data.

Following is the video on regular expressions for preparation on .NET: -



Click and view for more c# interview questions and answers

Regards,

View more author’s Most asked c# interview questions

Friday, October 14, 2011

ASP.NET interview questions: - Can you explain the concept of trace listener?

‘Tracelistener’ are objects that get tracing information from the trace class
and they output the data to some medium. For instance you can see from the
figure ‘TraceListener’ how it listens to the trace object and outputs the same
to UI, File or a windows event log. There are three different types of
‘tracelistener’ first is the ‘defaulttracelistener’ (this outputs the data to
UI), second is ‘textwritertracelistener’ (this outputs to a file) and the final
one is ‘Eventlogtracelistener’ which outputs the same to a windows event log.




Figure: -Trace listeners

Below is a snap shot for ‘textwritertracelistener’ and ‘eventlogtracelistener’. Using ‘textwritertracelistener’ we have forwarded the trace’s to ‘ErrorLog.txt’ file and in the second snippet we have used the ‘Eventlogtracelistener’ to forward the trace’s to windows event log.




Figure: - Tracelistener in action

See the following video on Web.config transformation as follows: -





See ASP.NET interview questions for interview preparation

Regards,
View more author’s blog on ASP.NET interview questions

Thursday, October 13, 2011

ASP.NET interview questions: - What is CSS (Cascading Style Sheet) and how to create a CSS in ASP.NET?

CSS (Cascading Style Sheet): - Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation semantics (the look and formatting) of a document written in a markup language.

In simple words CSS (Cascading Style Sheet) is used to apply layout and visual style of elements.

CSS (Cascading Style Sheet) is supported by almost all the browsers.

Now, let’s create a simple example to see how exactly we can add a CSS (Cascading Style Sheet) file in your ASP.NET application.

In order to see it practically just follow the following steps.

Step1: - Create a simple ASP.NET web application for that just go to >> File >> New >> Project >> Web >> Select ASP.NET Empty Web Application.





Step2: - Now add a simple WebForm page in to your Web Application for that just go to >> Solution Explorer >> Right Click on your Project >> Add >> New Item >> Select WebForm.




After adding the WebForm page in to your application, now design your page like below diagram.



Step3: - This is the most important step while using the CSS (Cascading Style Sheet), in this step we just need to add a style sheet for that just go to >> Solution Explorer >> Right Click on your Project >> Add >> New Item >> Select Style Sheet.




Now, let us assume that we have change the background color and text color of the Button control and the TextBox control. In order to achieve the above point we have to add below code snippet in to your StyleSheet1.css file.

.buttonInsert {color:Black;background-color:Green;}
.buttonDelete {color:Black;background-color:Red;}
.buttonUpdate {color:Black;background-color:Yellow;}
.text {color:Black;background-color:Blue;}

In the above code snippet you can clearly see that I have created three different styles for the button control and a single text style for the TextBox control.

Step4: - Now, let’s see how exactly we can use CSS in to your Web Application.





Now, when you run your web application you will see the result like below diagram.




In the above diagram of the result you can clearly see the effect of the CSS declared on the described controls.

Similarly, you can doo a lot with the CSS (Cascading Style Sheet) to make your page attractive and helpful to the user.

See the following video on questions asked in C# and .NET interviews: -





Get more materials on ASP.NET interview questions


Regards,


See more stuffs on author’s blog for ASP.NET interview questions