Tuesday, January 31, 2012

Implementing Date Search functionality using Asp.net and c#

developerscode

Implemeting search functionality is common in web or windows applications.Here i would like to explain the procedure of implementing date Search functionality to bind the data in gridview using Asp.net and c#.


First ,Add two Textbox controls and one button and change the properties as follows

For First Textbox ,change the name as TxtFromDate and Add the Ajax Calendar Extender to that Textbox.

For Second Textbox ,change the name as TxtToDate and Add the Ajax Calendar Extender to that Textbox.

For Button,change the name as Search

Next, Bind the dates for the following Textboxes controls.For this write the following code in BindDates() method


private void BindDates() 
{

        DateTime FromDate = DateTime.Now.ToUniversalTime().AddHours(5.5);

        TxtFromDate .Text = FromDate.ToString("dd/MM/yyyy"); 

        DateTime ToDate = DateTime.Now.ToUniversalTime().AddHours(5.5);

        TxtToDate .Text = ToDate.ToString("dd/MM/yyyy"); 
}



Next,Write the following code in Search button click Event

string[] strFromDate = TxtFromDate .Text.Split('/');
                 string[] strToDate =TxtToDate.Text.Split('/');

                DateTime fromDate = Convert.ToDateTime(strFromDate[1] + '/' + strFromDate[0] + '/' + strFromDate[2]);
               DateTime toDate = Convert.ToDateTime(strToDate[1] + '/' + strToDate[0] + '/' + strToDate[2]);

           Sqlcommand com = new SqlCommand("Select * from Employee Where DateTime Between '" + fromDate + "' and '" + toDate + "' ",con)
            {
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(com );
                da.Fill(ds, "Employee");
                Gridview.DataSource = ds.Tables["Employee"];
               Gridview.DataBind();
                da.Dispose();
                ds.Dispose();

            }


That's it...

Labels: , , , , , , , ,

1 Comments:

At February 3, 2012 at 9:19 AM , Anonymous Anonymous said...

Thats easy Nice Code...

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home