Auto Increment Id or Number Using Asp.Net C#

Automatically Increment Id or Number

Automatically Increment The Id Value or Number  Using Database in Asp.Net C#.

                            
                          DEMO



                        Download

                     HTML Coding


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
   
       <table><tr><td>
           <asp:Label ID="Label1" runat="server" Text="ID"></asp:Label>
           </td><td>
               <asp:Label ID="lblID" runat="server" ForeColor="#FF3399"></asp:Label>
           </td></tr>
           <tr><td>
               <asp:Label ID="Label2" runat="server" Text="Course"></asp:Label>
               </td><td>
                   <asp:TextBox ID="txtCourse" runat="server"></asp:TextBox>
               </td></tr>
           <tr><td></td><td>
               <asp:Button ID="Button1" runat="server" Text="Register" OnClick="Button1_Click" />
               </td></tr>

       </table></div>
    </form>
</body>

</html>
                        C# Coding


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getid();
        }
    }
    protected void getid()
    {
        SqlConnection  con = new SqlConnection (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());      
        con.Open();
        SqlCommand cmd = new SqlCommand("select MAX (CAST( Id as INT)) from Reg", con);
        SqlDataReader rd = cmd.ExecuteReader();
        if (rd.Read())
        {
            string Value = rd[0].ToString();
            if (Value == "")
            {
                lblID.Text = "1";
            }
            else
            {             
                lblID.Text =rd[0].ToString();
                lblID.Text = (Convert.ToInt64(lblID.Text) + 1).ToString();           
            }      
        }
        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into Reg values('"+lblID.Text+"','"+txtCourse.Text+"')", con);
        cmd.ExecuteNonQuery();
        getid();
    }

}


Create Database Table 


              



Add New Webform - Select Label ,Textbox From Toolbox






Next - Add Database  Connection - Select Maximum Id & Add Increment Values From Retriving db values








Export Gridview Data To Excel,Pdf,.Csv,Txt,Html,Doc Files Using Dropdownlist Selected Value in Asp.Net C#

Export Gridview Data To Excel,Pdf,.Csv,Txt,Html,Doc Files


Export Gridview Data To Excel,Pdf,.Csv,Txt,Html,Doc Files Whenever Select Dropdownlist Value Using asp.Net C#.

                              
                          DEMO


                           Download


    Add Pdf ITextsharp Reference Below Link

http://dotnetdrizzles.blogspot.in/2014/09/how-to-create-pdf-document-in-asp-net.html 

                      HTML Coding

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div >
       

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <AlternatingRowStyle BackColor="Gray" />
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="ID" />
                <asp:BoundField DataField="username" HeaderText="User Name" />
                <asp:BoundField DataField="course" HeaderText="Course" />
            </Columns>
            <HeaderStyle BackColor="#FFCCFF" />
        </asp:GridView>
<table> <tr><td>
      
        <asp:DropDownList ID="DropDownList1" runat="server" Style="font-size: 18px;
                cursor: pointer; background-color:silver; padding: 5px 10px 5px 10px;" Width="124px">

            <asp:ListItem Text="Word File" Value="wordfile"></asp:ListItem>
                <asp:ListItem Text="HTML File"></asp:ListItem>
                <asp:ListItem Text="PDF File" ></asp:ListItem>
                <asp:ListItem Text="Excel File"></asp:ListItem>
                <asp:ListItem Text="Text File"></asp:ListItem>
                <asp:ListItem Text="CSV File" ></asp:ListItem>
        </asp:DropDownList>
            </td></tr>
            <tr><td>
                <br />
                &nbsp;
                <asp:Button ID="Button7" Style="width: 150px; font-size: 18px;
                cursor: pointer; background-color:gray; padding: 5px 10px 5px 10px; margin-left: 0px;" runat="server" OnClick="Button7_Click" Text="Download File" />
                </td></tr>
        </table>

    </div>
    </form>
</body>
</html>
                 
                       C# Coding

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

public partial class Download : System.Web.UI.Page
{

    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    SqlDataReader rd;
    DataSet ds;
    string query;
    DataTable dtTable = new DataTable();

    public void dbcon()
    {
        string connn = (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con = new SqlConnection(connn);
        con.Open();

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        gridbind();
    }
    protected void gridbind()
    {
        dbcon();
        query = "select * from Reg";
        cmd = new SqlCommand(query, con);
        adp = new SqlDataAdapter(cmd);
        ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
   
    protected void txt()
    {
        gridbind();
        string text = string.Empty;

        foreach (TableCell Tblcell in GridView1.HeaderRow.Cells)
        {

            text += Tblcell.Text + "\t\t";
        }


        text += "\r\n";

        foreach (GridViewRow row in GridView1.Rows)
        {
            foreach (TableCell Tblcell in row.Cells)
            {
                text += Tblcell.Text + "\t\t";
            }

            text += "\r\n";
        }

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=Gridtxt.txt");
        Response.Charset = "";
        Response.ContentType = "application/text";
        Response.Output.Write(text);
        Response.Flush();
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
  
    protected void csv()
    {
        gridbind();
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridCsv.csv");
        Response.Charset = "";
        Response.ContentType = "application/text";
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        StringBuilder str = new StringBuilder();
        for (int k = 0; k < GridView1.Columns.Count; k++)
        {
            str.Append(GridView1.Columns[k].HeaderText + ',');
        }

        str.Append("\r\n");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            for (int k = 0; k < GridView1.Columns.Count; k++)
            {
                str.Append(GridView1.Rows[i].Cells[k].Text + ',');
            }

            str.Append("\r\n");
        }

        Response.Output.Write(str.ToString());
        Response.Flush();
        Response.End();
    }
    protected void doc()
    {
        gridbind();
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",
        "attachment;filename=GridDoc.doc");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-word ";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        GridView1.RenderControl(hw);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
    protected void html()
    {
        HtmlForm form = new HtmlForm();
        GridView1.AllowPaging = false;
        gridbind();
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=GridHtml.html");
        Response.Charset = "";
        Response.ContentType = "text/html";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        form.Attributes["runat"] = "server";
        form.Controls.Add(GridView1);
        this.Controls.Add(form);
        Form.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.Flush();
        Response.End(); 
    }
    protected void pdf()
    {
        gridbind();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=GridPdf.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        GridView1.RenderControl(hw);
        GridView1.HeaderRow.Style.Add("width", "15%");
        GridView1.HeaderRow.Style.Add("font-size", "10px");
        GridView1.Style.Add("text-decoration", "none");
        GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
        GridView1.Style.Add("font-size", "8px");
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }
    protected void excel()
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=GridExcel.xls");
        Response.ContentType = "application/excel";
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }
   
    private void Download_File(string FilePath)
    {
        Response.ContentType = ContentType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(FilePath));
        Response.WriteFile(FilePath);
        Response.End();
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        string strFileType;
        strFileType = DropDownList1.Text.ToString();
        switch (strFileType)
        {
            case "HTML File":
                html();
                break;
            case "Word File":
                doc();
                break;
            case "Text File":
                txt();
                break;
            case "Excel File":
                excel();
                break;
            case "PDF File":
                pdf();
                break;           
            case "CSV File":
                csv();
                break;
           
        }
    }
    }



Add New Webform - Select Gridview,Dropdownlist,Button From toolbox




Next - Add Namespaces - Bind GridView & Download All Formate Coding






Next - Add To Swith the Case Selected Dropdownlist Items