Click here to Skip to main content
16,018,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SqlConnection con = new SqlConnection("Data Source=Abc//SQLEXPRESS;Initial Catalog=Demo;Integrated Security=True");
SqlCommand cmd = new SqlCommand();

string maxid = "E00";
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{

con.Open();
cmd.Connection = con;
cmd.CommandText = "select MAX(Emp_Id) from EmpTbl";
maxid = cmd.ExecuteScalar().ToString ();
con.Close();
maxid = maxid.Substring (2);
maxid = (Convert.ToInt32(maxid) + 1).ToString ();

}

i got error in maxid = maxid.Substring (2); line saying startIndex cannot be larger than length of string.

How can i get out of it...
help me..
Or some one having code for auto generation of Id which contains alphanumeric values like "E001", "E002","E003" and go on.. Plz send me on gauravjadhav351@gmail.com.
Posted

1 solution

CODE to auto generate an alpha numeric id in C#

C#
public string GetLatestOrderId()
{
    string ReceivedId = string.Empty;
    string displayString = string.Empty;
    String query = "SELECT MAX(OrderReceivedNo) FROM [Order_Received]";
    String data = DataManager.RunExecuteScalar(ConnectionString.Constr, query);
    ReceivedId = data;
    if (string.IsNullOrEmpty(ReceivedId))
    {
        ReceivedId = "OR0000";//It is the start index of alpha numeric value
    }
    int len = ReceivedId.Length;
    string splitNo = ReceivedId.Substring(2, len - 2);//This line will ignore the string values and read only the numeric values
    int num = Convert.ToInt32(splitNo);
    num++;// Increment the numeric value
    displayString = ReceivedId.Substring(0, 2) + num.ToString("0000");//Concatenate the string value and the numeric value after the increment
    return displayString;
}



OR

public static IEnumerable<string> Numbers()
{
    return Enumerable.Range(0xA0000, 0xFFFF9 - 0xA0000 + 1)
        .Select(x => x.ToString("X"));
}
You could also have an id generator class:

public class IdGenerator
{
    private const int Min = 0xA0000;
    private const int Max = 0xFFFF9;
    private int _value = Min - 1;

    public string NextId()
    {
        if (_value < Max)
        {
            _value++;
        }
        else
        {
            _value = Min;
        }
        return _value.ToString("X");
    }
}
 
Share this answer
 
Comments
Gaurav Jadhav 5-Mar-13 2:08am    
thanx...
but what is DataManager over here.
And i want to display the result in textbox...
Azziet 5-Mar-13 2:20am    
there is class to execute query.. you can execute your query by using just simple executescaler
Gaurav Jadhav 5-Mar-13 2:46am    
how to implement that class...
there is anu special way to imlpement it....???
Gaurav Jadhav 5-Mar-13 2:52am    
String data = DataManager.RunExecuteScalar(ConnectionString.Constr, query);
wht ths means...
Azziet 5-Mar-13 2:49am    
no!! you have no need to create class... use like this

string sql =
"INSERT INTO Production.ProductCategory (Name) VALUES (@Name); "
+ "SELECT CAST(scope_identity() AS int)";
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Name", SqlDbType.VarChar);
cmd.Parameters["@name"].Value = newName;
conn.Open();
newProdID = (Int32)cmd.ExecuteScalar();
}

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