This topic is locked

Alphanumeric Identity Key

4/12/2007 5:28:30 PM
ASPRunner.NET General questions
R
rob author

Hello,
User requirement is to have a unique identifier example:
ABC-07-00001
ABC will be static, 07 is the current year, and 00001 will need to be incremented by 1 each time someone clicks to add a record (the identifier also shows up on the add screen) and the 00001 resets when the year changes.
Can this be done in the events area? Any suggestions would be greatly appreciated.
Thanks!

Eugene 4/13/2007

[codebox]

public static void AddOnLoad(System.Web.UI.Page Page)

{
string sqlSelect = "select max(id) from [Users]";
ConnectionStringSettings cts = ConfigurationManager.ConnectionStrings["Project1ConnectionString"];

SqlDataSource sds = new SqlDataSource(cts.ProviderName, cts.ConnectionString, sqlSelect);

try

{

sds.DataSourceMode = SqlDataSourceMode.DataReader;
System.Data.Common.DbDataReader dbDr = (System.Data.Common.DbDataReader)sds.Select(System.Web.UI.DataSourceSelectArgume

nts.Empty);
if (dbDr.HasRows && dbDr.Read())

{

string id = dbDr["id"].ToString();

if (id.Split('-').Length != 3) return;
string newID = "";

string currentYear = DateTime.Now.ToString("yy");
string year = id.Split('-')[1];

int value = Convert.ToInt16(id.Split('-')[2]);
if (year != currentYear) value = 1;

else value++;
newID = string.Format("{0}-{1}-{2}", id.Split('-')[0], currentYear, value.ToString("00000"));
DetailsView dv = (DetailsView)Page.FindControl("UsersDetailsView");

if (dv != null)

{

TextBox txt = (TextBox)dv.FindControl("fldUserName");

if (txt != null) txt.Text = newID;

}

}
dbDr.Dispose();

}

finally

{

sds.Dispose();

}

}

[/codebox]