i've a table in which country code and city code exists. there is a hotel code field as well.
i wants to generate autonumber including country and city code.
for example:-
"IND-DEL-0001"
1ST PART IS COUNTRY CODE 2ND PART IS CITY CODE. 3RD PART IS AUTOGENERATED AND STARTING FROM "0001".
please suggest code to achieve this.
thank you.


VulpesRank #55 · 29.2K reputationPosted Sep 14, 2012, 1:51 AM EDT
using System;
class Test
{
static void Main()
{
Console.WriteLine(GenerateCode("IND", "DEL"));
Console.WriteLine(GenerateCode("USA", "WAS"));
Console.ReadKey();
}
static int lastNumber;
static string GenerateCode(string countryCode, string cityCode)
{
lastNumber++;
if (lastNumber > 9999) lastNumber = 1;
return String.Format("{0}-{1}-{2:D4}", countryCode, cityCode, lastNumber);
}
}
However, unless the numbers are to be reset each time the application runs, you'd need to save the last number used to a file and then recover it (in a static constructor say) when the applicatlon next runs.
Anis Smile30 reputationPosted Sep 16, 2012, 9:58 PM EDT
===create a cls file and paste these codes===
//here we establish a sql database connection
public clsDataware()
{
InitDB();
sqlCon =new SqlConnection();
sqlCon.ConnectionString = "Data Source=" + DBSource + ";Initial Catalog=" + DBName + ";Integrated Security=True";
sqlCon.Open();
}
private void InitDB()
{
DBName = "URDatabasename";
DBSource = "URDBSourcename";
}
//here it create a empty table and assign a table value through marked query
public DataTable CreateDatatable(string SqlQuery, string strTable)
{
SqlDataAdapter sqlAdapter=new SqlDataAdapter();
DataTable dt=new DataTable ();
SqlCommand cmd = new SqlCommand(SqlQuery, sqlCon);
sqlAdapter.SelectCommand = cmd;
dt.TableName = strTable;
sqlAdapter.Fill(dt);
return dt;
}
//here we found a maximum id from our table and increment by 1
public long Getcode(string sqlQuery)
{
DataTable dt=new DataTable();
dt = CreateDatatable(sqlQuery,"ID");
if (!DBNull.Value.Equals(dt.DefaultView[0].Row["id"]))
{
return (int.Parse(dt.DefaultView[0].Row["id"].ToString()) + 1);
}
else
{
return 1;
}
}
=== cls ends here===
===paste this code in ur code behind page i.e wr u pin ur insert code===
//here we creating object for class
private clsDataware mDataware;
long hotelid= mDataware.GetCode("Select max(id) from URTABLENAME")
string autogen="IND-DEL-"+ hotelid +""
Here the 3rd part will be automatically generated.
Hope its helpfull. Post ur comment.