Introduction:
In the dynamic realm of web development, the ability to generate unique and sequential numbers is often a critical requirement. This article delves into the intricacies of creating an auto-generate number feature in ASP.NET using C# coding. Follow along as we explore the SQL Server setup, CSS styling, and the backend C# logic required to seamlessly implement this functionality in your web application.
Creating the AutoGenerateTable in SQL Server:
To begin, we need a table that will store our auto-generated numbers. The provided SQL script creates a table named 'autogeneratornumber' with columns for 'id' (auto-incremented), 'recordid,' and 'recordname.' Additionally, a stored procedure 'proc_AutoGenNumber' is defined to fetch the maximum 'recordid' from the table, serving as the basis for our auto-increment logic.
CREATE TABLE [dbo].[autogeneratornumber](
[id] [int] IDENTITY(1,1) NOT NULL,
[recordid] [nchar](5) NOT NULL,
[recordname] [nvarchar](50) NULL
CONSTRAINT [PK_autogeneratornumber] PRIMARY KEY CLUSTERED
(
[recordid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CSS Code: Designing the Frontend:
A visually appealing user interface is crucial for a positive user experience. The included CSS code enhances the aesthetics of the ASP.NET page, defining styles for tables, buttons, and textboxes. The design is clean, organized, and user-friendly.
<style>
#table1
{
border-collapse:collapse;
}
#table1 th, #table1 td, td1, td2, #table1 #td1, #td2
{
border:1px solid #dddddd;
}
#table1 th
{
padding:5px 7px 5px 7px;
color:cyan;
font-weight:bold;
background:#000000;
}
#table1 td, td1, td2, #table1 #td1, #td2, #table1 #txtrecordname, #table1 #txtrecordname:hover
{
padding:5px;
}
#table1 #td1, #td2
{
font-weight:bold;
background:#fafafa;
}
#table1 #btn_autogeneratenumber
{
padding:5px 7px 5px 7px;
border-color:#2c2c2c;
}
#table1 #txtrecordname, #table1 #txtrecordname:hover
{
width:200px;
}
#table1 #txtrecordname:hover, #table1 #btn_autogeneratenumber
{
background:#2c2c2c;
color:#ffffff;
}
#table1 #btn_autogeneratenumber:hover
{
background:#0000cc;
padding:5px 7px 5px 7px;
color:#fafafa;
border-color:#0000cc;
}
</style>
HTML Markup: Building the Page Structure:
The HTML markup lays the foundation for the ASP.NET page. It includes form elements, labels, textboxes, and a button for creating new records. The page structure aligns with best practices, providing a clear and intuitive layout for users.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoGenerateNumberForm.aspx.cs" Inherits="AutoGenerateNumberForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="CSS/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="table1">
<tr>
<th colspan="3">Creating Auto Generate Number Through SQL SERVER</th>
</tr>
<tr>
<td id="td1">Record ID : </td>
<td colspan="2"><asp:Label ID="lblrecordid" runat="server"></asp:Label></td>
</tr>
<tr>
<td id="td2">Record Name :</td>
<td colspan="2"><asp:TextBox ID="txtrecordname" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="3"><asp:Button ID="btn_autogeneratenumber" Text="Create Record"
runat="server" style="text-align:center;"
onclick="btn_autogeneratenumber_Click" /></td>
</tr>
</table>
</div>
<asp:Label ID="lblerror" runat="server"></asp:Label>
<br />
<br />
<br />
<asp:HyperLink ID="LinkButton1" NavigateUrlrl="~/AutoGenerateNumberForm.aspx"
runat="server" NavigateUrl="~/AutoGenerateNumberForm.aspx">Create New Record </asp:HyperLink>
</form>
</body>
</html>
C# Code: Bringing It to Life:
The C# code is the backbone of our auto-generate number feature. Let's break down the key components:
Namespace and Page Load Event:
Importing necessary namespaces and setting up the page load event to ensure that auto-generation is initiated when the page is loaded for the first time.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class AutoGenerateNumberForm : System.Web.UI.Page
{
int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
generateautonumber();
}
}
}
AutoGenerate Number Method:
The generateautonumber method connects to the SQL Server, executes the 'proc_AutoGenNumber' stored procedure to retrieve the maximum 'recordid,' increments it, and displays the result on the ASP.NET page.
private void generateautonumber()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbCon"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("proc_AutoGenNumber", con);
cmd.CommandType = CommandType.StoredProcedure;
string value = cmd.ExecuteScalar().ToString();
i++;
int rv = Int32.Parse(value) + 1;
lblrecordid.Text = rv.ToString();
}
Create Button Click Event:
The btn_autogeneratenumber_Click event handles the creation of new records. It connects to the database, executes the 'proc_InsertAutoGenNumber' stored procedure, and inserts the incremented 'recordid' along with the 'recordname' into the 'autogeneratornumber' table.
protected void btn_autogeneratenumber_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbCon"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("proc_InsertAutoGenNumber", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@recordid", SqlDbType.VarChar).Value = lblrecordid.Text;
cmd.Parameters.Add("@recordname", SqlDbType.VarChar).Value = txtrecordname.Text;
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
lblerror.Text = "Record Inserted Successfully...";
}
else
{
lblerror.Text = "Error in Record Inserting...";
}
con.Close();
}
}
Conclusion:
By following this comprehensive guide, you can seamlessly integrate auto-generated numbers into your ASP.NET application. From SQL Server setup to frontend styling and backend logic, each step is carefully explained. Elevate your web application's functionality and user experience by implementing this valuable feature. Unlock the power of automation and provide users with a smoother, more efficient experience.
0 Comments