Friday, May 11, 2012

Cool Progress Bar in C#,

 This example shows you how to display a interactive progress bar while work is in progress.For doing this you need a gif image file which gives a good look for progress.
Like this one :
Then You must have an update panel in web form.Now implement the code given below.You can change image and text as per your need.

<asp:UpdateProgress ID="UPdtProgress" runat="server"
                <img ID="imgProgress" runat="server" alt="Work is in Progress" 
                           src="~/PayRoll/images/progressbar.gif" height="17" width="100" /><br />
                 <span style="color: #FF0000;  font-size: small;">  Please Wait</span>

Output will look like this -:

Tuesday, May 8, 2012

Open doc,txt or pdf files in C#

This example shows you how to open any doc,txt or pdf file which is saved in any specified location.In this example I m using a GridView which contains a template field which holds FileName.

<asp:TemplateField HeaderText="Application Name" SortExpression="ApplicationName">
                       <asp:LinkButton ID="lbOpenApplication" runat="server" Text='<%# Bind("ApplicationName") %>'  CommandArgument='<%# Bind("ApplicationName") %>' CommandName="ViewApplication"></asp:LinkButton>

In Gridview's RowCommand event we have to write code snippet given below.

protected void gvViewLeave_RowCommand(object sender, GridViewCommandEventArgs e)
        if (e.CommandName == "ViewApplication")
            string filename = e.CommandArgument.ToString();
            string path = Server.MapPath("~/PayRoll/Applications/");
            System.Diagnostics.Process.Start(path + filename);

Calculate No of Days Between Two Dates in Sql Server

Monday, May 7, 2012

C Sharp Interview Questions with Answers

Here I am going to include some important questions asked in recent interview.This is beneficial for both freshers and experienced.By answering these questions You will leave a good impression on interviewer.
So,now no more talks..start reading and digesting.

What is Mutex?

Mutex is the short form for MUTual EXclusion.
Mutual exclusion refers to the problem of ensuring that no two processes or threads can be in their critical section at the same time. Here, a critical section refers to a period of time when the process accesses a shared resource, such as Shared Memory. The problem of mutual exclusion was first identified and solved by Edsger W. Dijkstra in his seminal 1965 paper titled: Solution of a problem in concurrent programming control.
In C# there is Mutex class defined within System.Threading Namespace

What do You mean by LOCK and Wait?

A Lock is a synchronization mechanism for enforcing limits on access to a resource in an environment where there are many threads of execution. Locks are one way of enforcing concurrency control policies.

A process (or task) may wait on another process to complete its execution. In most systems, a parent process can create an independently executing child process. The parent process may then issue a wait system call, which suspends the execution of the parent process while the child executes. When the child process terminates, it returns an exit status to the operating system, which is then returned to the waiting parent process. The parent process then resumes execution.

Define Static members of the class in the terms of C# -:
Static members belong to whole class rather than to individual object. For example, if you have a static phoneNumber field in your Student class, then there will be the single instance of this field and all the objects of this class will share this single field. Changes made by one object to phone Number will be realized by the other object.
Class Student
    Public static int phoneNumber;
    Public int rollNumber;
Class Test
    Public  static void Main()
        Student  st1=new Student();
        Student st2=new Student();
Here you can see that the phoneNumber is accessed without any reference to the object but with the name of the class it belongs. Static methods are very useful while programming. In fact, the WriteLine() and ReadLine() methods are static methods of Console Class. Even Main () is declared as static as CLR calls it without making any instance of class. It’s useful when we want to cache data that should be available to all objects of class.
Some precautions:
Don’t put too many static methods in your class as it is against the OO design principles and makes your class less extensible.
You tend to loose a number of OO advantages while using static methods, as static methods can’t be overridden which means it cannot be used polymorphically, something widely used in the OO paradigm of programming.

What DoYou mean by Singleton Approach?
This structural code demonstrates the Singleton pattern which assures only a single instance (the singleton) of the class can be created.

Tuesday, May 1, 2012

Bind DropDownList in Gridview

Binding values in Dropdownlist in Gridview at runtime is not a big task.We can do it using RowDatabound event.In the given example dropdownlist is being populated at the time of editing.
At first declare a dropdownlist control in EditItemTemplate.

 <asp:TemplateField HeaderText="State" SortExpression="StateName" >
      <asp:DropDownList ID="ddlStateNm" runat="server" DataTextField="StateName"    DataValueField="StateID"> </asp:DropDownList>
      <asp:Label ID="lblStateID" runat="server" Text='<%# Bind("StateID") %>' Visible="false"></asp:Label>
         <asp:Label ID="lblStateName" runat="server" Text='<%# Bind("StateName") %>'></asp:Label>

Write a Function to bind DropDownList.
 public void BindStateddl(DropDownList ddl)
        SqlDataAdapter adp = new SqlDataAdapter("SELECT [StateID],[StateName] FROM [dbo].[MST_State] order by [StateName] asc", con);
        DataSet ds = new DataSet();

        ddl.DataSource = ds;
        ddl.Items.Insert(0, new ListItem("---Select---", "0"));

Now the most important part the RowDataBound event. Here at first check for DataRow so that it will overlook HeaderRow and then check for EditIndex so that Databinding to DropDownList will be implemented to editble row only.

   protected void gvCourt_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            if (gvCourt.EditIndex == e.Row.RowIndex) //to overlook header row
                string StateId = ((Label)e.Row.FindControl("lblStateID")).Text;

                DropDownList ddlGridState = (DropDownList)e.Row.FindControl("ddlStateNm");

                ddlGridState.SelectedValue = StateId;