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