Display Auto Increment Value In Text Field After Insert To Database ASP.NET
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;
namespace AUTO_INCREMENT_VALUE_ASP
{
public partial class WebForm1 : 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 tbl";
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 tbl";
SqlCommand cmd = new SqlCommand(query1,con);
con.Open();
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 tbl 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();
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
Response.Write("<script>alert('Inserted !!')</script>");
getID();
bindGridView();
NAMETextBox.Text = "";
AGETextBox.Text = "";
}
else
{
Response.Write("<script>alert('Not Inserted !!')</script>");
}
con.Close();
}
void bindGridView()
{
SqlConnection con = new SqlConnection(cs);
string query = "select * from tbl";
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 tbl 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();
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
Response.Write("<script>alert('Updated !!')</script>");
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 tbl where id = @id";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", IDTextBox.Text);
con.Open();
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
Response.Write("<script>alert('Deleted !!')</script>");
getID();
bindGridView();
NAMETextBox.Text = "";
AGETextBox.Text = "";
}
else
{
Response.Write("<script>alert('Not Deleted !!')</script>");
}
con.Close();
}
}
}
Above File is AUTO_INCREMENT_VALUE_ASP\WebForm1.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AUTO_INCREMENT_VALUE_ASP.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 400px;
}
.auto-style2 {
width: 65px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="4" cellspacing="4" class="auto-style1">
<tr>
<td class="auto-style2">ID</td>
<td>
<asp:TextBox ID="IDTextBox" runat="server" Height="23px" ReadOnly="True" Width="210px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">NAME</td>
<td>
<asp:TextBox ID="NAMETextBox" runat="server" Height="23px" Width="210px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">AGE</td>
<td>
<asp:TextBox ID="AGETextBox" runat="server" Height="23px" Width="210px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Button ID="INSERTButton" runat="server" Height="32px" OnClick="INSERTButton_Click" Text="INSERT" Width="81px" />
<asp:Button ID="UPDATEButton" runat="server" Height="32px" Text="UPDATE" Width="81px" OnClick="UPDATEButton_Click" />
<asp:Button ID="DELETEButton" runat="server" Height="32px" Text="DELETE" Width="81px" 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="Gray" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment