C# using id creation dynamically in ASP.Net.
Aspx:cs
string SelectID;
int ID;
--Database Table
Count
SelectID = "Select COUNT(*) AS Count From Sample";
SelectID = "Select COUNT(*) AS Count From Sample";
SqlDataAdapter da2 = new
SqlDataAdapter(SelectInvoiceID, cn);
DataSet dsInvoiceID = new DataSet();
da2.Fill(dsInvoiceID);
if (dsInvoiceID.Tables[0].Rows.Count != 0)
{
int a =
Convert.ToInt16(dsInvoiceID.Tables[0].Rows[0]["Count"].ToString());
int C = a + 1;
//ID
creates dynamically if ID count <10
if (C > 0 && C <= 9)
{
ID = "abc0000" + C;
}
//ID creates dynamically if ID
count 10 to 99
else if (C > 9 && C <= 99)
{
ID = "abc000" + C;
}
//ID creates dynamically if ID
count 100 to 999
else if (C > 99 && C <= 999) ID =
"abc00" + C;
//ID creates dynamically if ID
count 1000 to 9999
else if (C > 999 && C <= 9999) ID =
"abc0" + C;
//ID creates dynamically if ID
count 10000 to 99999
else if (C > 9999 && C <= 99999) ID = "abc"
+ C;
Ashish APosted Dec 13, 2013, 6:16 AM
I think, we don't need even loop. It can be done through single statement. See this link.. http://dotnetfiddle.net/6gfD1c
VijubookPosted Dec 12, 2013, 11:49 PM
Hi Dude.. Due to store my ID with string i defined as string..Am not sure but i feel loop wont be workout..
Ashish APosted Dec 12, 2013, 8:21 AM
Will not it give you an error? ID is int and you are treating it as string. Also, instead of multiple if else, can't we use loop with PadLeft?