Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 952113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:50:24+00:00 2026-05-15T23:50:24+00:00

I am working on ASP.Net using C# I want to generate a sequence id

  • 0

I am working on ASP.Net using C# I want to generate a sequence id that should be like this:

ELG0001 , ELG0002, ...

ELG is the PREFIX and 0001 should be in sequence

I am using sql server 2005

This ID will be generated and added to my database. How can I do this?

can you help me with coding?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:50:25+00:00Added an answer on May 15, 2026 at 11:50 pm

    using this code we can do it simply

    public string CJ()
        {
            string Id = GenerateId("cust", "cust_id", 6, "ELG", true);
            return Id;
        }
        public string GenerateId(string TableName, string ColumnName, int ColumnLength, string Prefix, bool Padding)
        {
            string Query, con, Id;
            con = "Data Source=CJ\\SQLEXPRESS;Initial Catalog=seq;Persist Security Info=True;User ID=sa;Password=123";
            SqlConnection cn = new SqlConnection(con);
            int preLength,padLength;
            preLength = Convert.ToInt32(Prefix.Length);
            padLength = ColumnLength - preLength;
            if (Padding == true )
            {
                 Query = "SELECT '" + Prefix + "' + REPLACE(STR(MAX(CAST(SUBSTRING(" + ColumnName + "," + Convert.ToString(preLength + 1) + "," + padLength + ") AS INTEGER))+1," + padLength + "),' ',0) FROM " + TableName;
    
            }
            else
            {
                Query = "SELECT '" + Prefix + "' + CAST(MAX(CAST(SUBSTRING(" + ColumnName + "," + Convert.ToString(preLength + 1) + "," + padLength + ") AS INTEGER))+1 AS VARCHAR) FROM " + TableName;
            }
            SqlCommand com = new SqlCommand(Query, cn);
            cn.Open();
            if (com.ExecuteScalar().ToString() == "")
            {
                Id = Prefix;
                if (Padding == true)
                {
                    for (int i = 1; i  padLength - 1; i++)
                    {
                        Id += "0";
                    }
                }
                Id += "1";
            }
            else
            {
                Id = Convert.ToString(com.ExecuteScalar());
            }
            cn.Close();
            return Id;
    }
    

    thanxx for the help
    just add the method CJ()
    as i have done here

    protected void Button1_Click(object sender, EventArgs e)
        {
            string con;
            con = "Data Source=CJ\\SQLEXPRESS;Initial Catalog=seq;Persist Security Info=True;User ID=sa;Password=123";
            using (SqlConnection cn = new SqlConnection(con))
            {
                cn.Open();
                using(SqlTransaction trans = cn.BeginTransaction())
                using (SqlCommand cmd = cn.CreateCommand())
                {
                    cmd.Transaction = trans;
                    cmd.CommandText = "INSERT INTO cust([cust_id],[cust_name],[cust_add]) VALUES(@cust_id,@cust_name,@cust_add)";
                    cmd.Parameters.Add("@cust_id",CJ());
                    cmd.Parameters.Add("@cust_name",TextBox1.Text);
                    cmd.Parameters.Add("@cust_add",TextBox2.Text);
                    cmd.ExecuteNonQuery();
                    trans.COmmit();
                }
                cn.Close();
                Response.Write("alert('DATA SAVED')");
                TextBox1.Text = "";
                TextBox2.Text = "";
            }
    
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into this dilemma when working on an ASP.net web application using Web
i am working on asp.net using c sharp language. I want to use ajax
I'm working with an asp.net website using C# in the background. I would like
I’m working on a website using VB (ASP.NET) I want to implement a password
I'm working on an ASP.NET web projec using VS2010,C#, I want my users to
I am working on a ASP.NET application using C# that is trying to read
I am working on asp.net using c#. I have one master page with 3
i've started working with ASP.NET MVC and using Entity Framework, concretely with SQLite db,
I'm working in ASP.NET MVC 1.0 using MvcContrib FluentHtml version 1.0. The following code
I am working on an ASP.NET WebForm application using MVP pattern. For every Web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.