public static string GenProductID(TextBox txtProductID) { int iicount = 0; string ID = ""; db = new db_MiletecDataContext(); var query = db.sp_generate_ProductID(); foreach (sp_generate_ProductIDResult count in query) { iicount = count.count.Value; iicount = iicount++; } ++iicount; if (iicount.ToString().Length.Equals(1)) { ID = "ProductID-" + iicount.ToString().PadLeft(4, '0').ToString(); } else if (iicount.ToString().Length.Equals(2)) { ID = "ProductID-" + iicount.ToString().PadLeft(3, '0').ToString(); } else { ID = "ProductID-" + iicount.ToString().PadLeft(2, '0').ToString(); } return(txtProductID.Text = ID); }