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.....




Friday, 28 March 2014

To start autoincrement field from 1 in sqlserver.

To reset autoincrement value from 1 in sql server:


Step1: In query Designer write this Query
Syntax:
DBCC CHECKIDENT('DATABASENAME.DBO.TABLENAME',RESEED,number)


Eg:DBCC CHECKIDENT('DATABASENAME.DBO.TABLENAME',RESEED,0)

Step 2: Click on Execute.

After this query will executed our autoincremented field will start from 1.

If number=0 then the autoincrement value will start from 1. if number=101 then the autoincrement field will start from 102.

Thank You......

Wednesday, 26 March 2014

Open DataReader Error.......

Error:- There is already an open DataReader associated with this command which must be closed first.

Solution:

If we use datareader in if else condition and we close the reader in if condition then also this error occurs so the solution is write below code in your else condition

if (!reader.IsClosed)
                    {
                        reader.Close();
                    }

error solved.....

Saturday, 22 March 2014

To find Current Auto increment Value in Table.

string que = "Select IDENT_CURRENT('Table Name')"; //It will return Last Identity Value in your table