hello;
how we can generate serial no like 1,2,3,4,..........n. in c#
Loading
Know the answer? Post it. Somebody with the same question will find it here.

Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Jan 23, 2012, 6:22 PM EST
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Generate_id._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
namespace Generate_id
{
public partial class _Default : System.Web.UI.Page
{
protected void generateid_Click(object sender, EventArgs e)
{
TextBox1.Text = GenerateSequenceNumber().ToString();
}
private int GenerateSequenceNumber()
{
string file2 = (Server.MapPath("test.txt"));
FileInfo fiRead = new FileInfo(file2);
string input = string.Empty;
int num = 1;
if (fiRead.Exists)
{
input = File.ReadAllText(file2);
}
if (input != string.Empty)
{
num = Convert.ToInt32(input);
}
File.WriteAllText(file2, (num + 1).ToString());
return num;
}
}
}
Thanks
Satyapriya NayakRank #47 · 31.5K reputationPosted Jan 23, 2012, 5:59 AM EST
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebApplication3
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
int count;
protected void GenerateId_Click(object sender, EventArgs e)
{
autogenerated();
}
void autogenerated()
{
SqlConnection con = new SqlConnection(strConnString);
str = "select count(*) from employee";
com = new SqlCommand(str, con);
con.Open();
count = Convert.ToInt16(com.ExecuteScalar()) + 1;
txt_empid.Text = count.ToString();
txt_empid.Enabled = false;
con.Close();
}
protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "insert into employee values('" + txt_empid.Text.Trim() + "','" + txt_empname.Text.Trim() + "'," + txt_sal.Text.Trim() + ")";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
Label4.Text = "Records successfully Inserted";
txt_empid.Text = "";
txt_empname.Text = "";
txt_sal.Text = "";
}
}
}
Thanks