Monday, 21 April 2014

To display only date in textbox not time.

When we use date datatype it takes both date and time.
So to display only date in Textbox not time use ToShortDateString().
ToShortDateString() : It Converts the value of the current DateTime object to its equivalent short date string representation.


E.g: DateTime time = DateTime.Now;     
   txtpurdt.Text = time.ToShortDateString();     
   txtpurdt.Text = time.ToString("dd/MM/yyyy");


By using time.ToString("dd/MM/YYYY") it displays our date in dd/mm/yyyy Format.

Thank You...
Happy Coding....

Display only date in Gridview

When we retrieve the data from database to gridview it displays date and time both to display only date in gridview add DataFormatString property in Boundfield of Gridview.

E.g: <asp:BoundField DataField="pos_dt" HeaderText="Sales order Date" SortExpression="pos_dt" DataFormatString="{0:d}" />

It only displays date in our Gridview.

Thank You...
Happy Coding.... 

Wednesday, 9 April 2014

Prevent Textbox to load or fetch data from cookies

Add this properties in your Textbox:

<asp:TextBox ID="txtid" runat="server" autocomplete="off" autocompletetype="None" ></asp:TextBox>


After adding this properties in your texbox it not show the cookie value.

Happy Coding......
Thank you....

To convert our database into excel file as a backup on the button Click Event.

Add this header name in your cs file:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

In your export to excel button click event write this code:

SqlConnection con;

con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from tablename", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable ds = new DataTable(); try
        {
            da.Fill(ds);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Trace.WriteLine(ex.Message);
            throw ex;
        }
        finally
        {
            con.Close();
        }
        Convert(ds, Response);
    }
public static void Convert(DataTable ds, HttpResponse Res)
    {
        Res.Clear();
        Res.Charset = "";
        Res.ContentType = "application/vnd.ms-excel";
        Res.AddHeader("Content-Disposition", "attachment;filename=\\tablename.xls");
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
        System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView();
        dg.DataSource = ds;
        dg.DataBind();
        dg.RenderControl(htmlWrite);
        Res.Write(stringWrite.ToString());
        Res.End();
    }

This Convert method convert our database to Excel format .

When you run this code and click on Export button it download one .xls file in which our table data will be saved.

Happy Coding.....

Thank You.....