AutoIncrement.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoIncrement.aspx.cs" Inherits="AutoIncrement" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
height: 26px;
}
.auto-style3 {
width: 103px;
}
.auto-style4 {
height: 26px;
width: 103px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>How To Create Auto Increment Value of TextBox After Inserting Data In Database</h1>
<table class="auto-style1">
<tr>
<td class="auto-style3">ID</td>
<td>
<asp:TextBox ID="IDTextBox" runat="server" ReadOnly="True"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style4">NAME</td>
<td class="auto-style2">
<asp:TextBox ID="NAMETextBox" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style3">AGE</td>
<td>
<asp:TextBox ID="AGETextBox" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style3"> </td>
<td>
<asp:Button ID="INSERTButton" runat="server" OnClick="INSERTButton_Click" Text="INSERT" />
<asp:Button ID="UPDATEButton" runat="server" Text="UPDATE" OnClick="UPDATEButton_Click" />
<asp:Button ID="DELETEButton" runat="server" Text="DELETE" OnClick="DELETEButton_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField HeaderText="SELECT" ShowSelectButton="True" />
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="LabelID" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="LabelNAME" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AGE">
<ItemTemplate>
<asp:Label ID="LabelAGE" runat="server" Text='<%# Eval("age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
AutoIncrement.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class AutoIncrement : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
getID();
bindGridView();
}
}
void getID()
{
SqlConnection con = new SqlConnection(cs);
string query = "select id from increment";
SqlDataAdapter sda = new SqlDataAdapter(query,con);
DataTable data = new DataTable();
sda.Fill(data);
if (data.Rows.Count < 1)
{
IDTextBox.Text = "1";
}
else
{
string query1 = "select max(id) from increment";
SqlCommand cmd = new SqlCommand(query1, con);
con.Open();
//Scalar kab use karta ha jab hamara query ka andar koi aggregat function ho like max
int i = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
i = i + 1;
IDTextBox.Text = i.ToString();
}
}
protected void INSERTButton_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
string query = "insert into increment values(@id,@name,@age)";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id",IDTextBox.Text);
cmd.Parameters.AddWithValue("@name", NAMETextBox.Text);
cmd.Parameters.AddWithValue("@age", AGETextBox.Text);
con.Open();
//This method is use to insert
int a = cmd.ExecuteNonQuery();//incorect
if(a > 0)
{
Response.Write("<script>alert('Insert !!')</script>");
//Id me automatic icrement hota ha isila liya id wala textbox empty nahe karbana ha
getID();
bindGridView();
NAMETextBox.Text ="";
AGETextBox.Text="";
}
else
{
Response.Write("<script>alert('Not Insert !!')</script>");
}
con.Close();
}
//Function for Gridview
void bindGridView()
{
SqlConnection con = new SqlConnection(cs);
string query = "select * from increment";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable data = new DataTable();
sda.Fill(data);
GridView1.DataSource= data;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
Label lblId = (Label)row.FindControl("LabelID");
Label lblName = (Label)row.FindControl("LabelNAME");
Label lblAge = (Label)row.FindControl("LabelAGE");
IDTextBox.Text = lblId.Text;
NAMETextBox.Text = lblName.Text;
AGETextBox.Text = lblAge.Text;
}
protected void UPDATEButton_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
string query = "update increment set name=@name,age=@age where id=@id";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", IDTextBox.Text);
cmd.Parameters.AddWithValue("@name", NAMETextBox.Text);
cmd.Parameters.AddWithValue("@age", AGETextBox.Text);
con.Open();
//This method is use to insert
int a = cmd.ExecuteNonQuery();//incorect
if (a > 0)
{
Response.Write("<script>alert('Updated !!')</script>");
//Id me automatic icrement hota ha isila liya id wala textbox empty nahe karbana ha
getID();
bindGridView();
NAMETextBox.Text = "";
AGETextBox.Text = "";
}
else
{
Response.Write("<script>alert('Not Updated !!')</script>");
}
con.Close();
}
protected void DELETEButton_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
string query = "delete from increment where id = @id";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", IDTextBox.Text);
con.Open();
//This method is use to insert
int a = cmd.ExecuteNonQuery();//incorect
if (a > 0)
{
Response.Write("<script>alert('Deleted !!')</script>");
//Id me automatic icrement hota ha isila liya id wala textbox empty nahe karbana ha
getID();
bindGridView();
NAMETextBox.Text = "";
AGETextBox.Text = "";
}
else
{
Response.Write("<script>alert('Not Deleted !!')</script>");
}
con.Close();
}
}
0 Comments