Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to generate a 6 digit sequence number with a prefix PB...
I have a found a solution in code project but its showing error...


C#
public string serialId()
        {
            string Id = GenerateId("customer_details", "id", 6, "PB", true);
            return Id;
        }

        public string GenerateId(string TableName, string ColumnName, int ColumnLength, string Prefix, bool Padding)
        {
            string query, con, Id;
            con = "Data Source=.;Initial Catalog=login_;Integrated Security=True";
            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;
        }


In the above code while I debug it throws an exception in the above bold line...
The exception is "invalid column name id"...

can anyone help me in clearing this problem?

Regards,
Sajin A

What I have tried:

I have tried debugging and changing the code many times
Posted
Updated 2-Nov-16 20:46pm
v2

Instead of messing around with that, give your table an Identity value (that will provide the numeric part of your code) and then add a Computed column: https://technet.microsoft.com/en-GB/library/ms191250%28v=sql.105%29.aspx?f=255&MSPPError=-2147217396[^]
This will maintain the code for you as if it was a single column without you having to risk SQL Injection as you do with the code you show.
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
I think you will find this question interesting How do i create a custom primary key?[^] and the answer from Maciej Los which is mainly a link.
Custom Auto-Generated Sequences with SQL Server - SQLTeam.com[^]
 
Share this answer
 
C#
string id = string.Format("PT{0}", sequenceNumber.ToString().PadLeft(13, '0'));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900