Generate Sequencial Number
-
Guys, I am using C# with MSS 2005. I need to generate a sequential number for a field (ItemNo) in the format ddmmyyyy000000x where x is a sequencial number eg (020520100000001,020520100000002, 020520100000003 ...) for records generated on May 2, 2010. Can any one help in this regard.
-
Guys, I am using C# with MSS 2005. I need to generate a sequential number for a field (ItemNo) in the format ddmmyyyy000000x where x is a sequencial number eg (020520100000001,020520100000002, 020520100000003 ...) for records generated on May 2, 2010. Can any one help in this regard.
So do it - what is the problem?
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
So do it - what is the problem?
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
I have something similar in VB6 with MDB but have difficulty using C# and MSS. See my VB6 code below and advice on how to produce something similar in C#. Public Sub GenerateCode(ByRef Coder As String) Dim strMar As String, intApr As Integer Dim strJul As String Coder = UCase(Coder) Dim rs As New ADODB.Recordset Dim cn As New ADODB.Connection On Error GoTo AddNewErr cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Manager.mdb" rs.Open "select ItemNo from Items where left(ItemNo,8) like '" + Coder + "' order by ItemNo asc", cn, adOpenKeyset, adLockOptimistic If rs.RecordCount <> 0 Then rs.MoveLast If Left(Trim(rs.Fields!ItemNo), 8) = Coder Then strMar = Right(Trim(rs.Fields!ItemNo), 6) strMar = Val(strMar) + 1 strMar = CStr(strMar) If (6 - Len(strMar)) <> 0 Then strMar = String$(6 - Len(strMar), "00000") & strMar End If Else strMar = "000001" End If Else strMar = "000001" End If Coder = Coder & strMar Editor = Coder Set rs = Nothing Set cn = Nothing Exit Sub AddNewErr: MsgBox Err.Description, vbExclamation, "Error Report" End Sub
-
Guys, I am using C# with MSS 2005. I need to generate a sequential number for a field (ItemNo) in the format ddmmyyyy000000x where x is a sequencial number eg (020520100000001,020520100000002, 020520100000003 ...) for records generated on May 2, 2010. Can any one help in this regard.
You just need to concatenate all strings, "your datetime string" + "000000" + your value of x. Are you facing any problem in this?
-
Guys, I am using C# with MSS 2005. I need to generate a sequential number for a field (ItemNo) in the format ddmmyyyy000000x where x is a sequencial number eg (020520100000001,020520100000002, 020520100000003 ...) for records generated on May 2, 2010. Can any one help in this regard.
-
Guys, I am using C# with MSS 2005. I need to generate a sequential number for a field (ItemNo) in the format ddmmyyyy000000x where x is a sequencial number eg (020520100000001,020520100000002, 020520100000003 ...) for records generated on May 2, 2010. Can any one help in this regard.
Following code will help you to achieve what you want in C#.
string temp="0000000";
int i=10;
string finalstring=DateTime.Today.ToString("ddMMyyyy") + temp.Substring(0, temp.Length - i.ToString().Length) + i.ToString();Hope this will help!
Jinal Desai - LIVE Experience is mother of sage....
-
Guys, I am using C# with MSS 2005. I need to generate a sequential number for a field (ItemNo) in the format ddmmyyyy000000x where x is a sequencial number eg (020520100000001,020520100000002, 020520100000003 ...) for records generated on May 2, 2010. Can any one help in this regard.
So create a static int variable (I would do this in a global static class), and increment it from anywhere in the code.
public static Globals
{
public static int counter = 0;
}Then, when you create your string, do it this way:
Globals.counter++;
string id = string.Format("{0}{1:0000000}", myDate.ToString("ddMMyy"), Globals.counter);.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
I have something similar in VB6 with MDB but have difficulty using C# and MSS. See my VB6 code below and advice on how to produce something similar in C#. Public Sub GenerateCode(ByRef Coder As String) Dim strMar As String, intApr As Integer Dim strJul As String Coder = UCase(Coder) Dim rs As New ADODB.Recordset Dim cn As New ADODB.Connection On Error GoTo AddNewErr cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Manager.mdb" rs.Open "select ItemNo from Items where left(ItemNo,8) like '" + Coder + "' order by ItemNo asc", cn, adOpenKeyset, adLockOptimistic If rs.RecordCount <> 0 Then rs.MoveLast If Left(Trim(rs.Fields!ItemNo), 8) = Coder Then strMar = Right(Trim(rs.Fields!ItemNo), 6) strMar = Val(strMar) + 1 strMar = CStr(strMar) If (6 - Len(strMar)) <> 0 Then strMar = String$(6 - Len(strMar), "00000") & strMar End If Else strMar = "000001" End If Else strMar = "000001" End If Coder = Coder & strMar Editor = Coder Set rs = Nothing Set cn = Nothing Exit Sub AddNewErr: MsgBox Err.Description, vbExclamation, "Error Report" End Sub
So, what you are doing is: 1) Read the last record (presumably for this month) from your database. 2) If it doesn't exist, use a default of 1. 3) If it does, strip out the last 6 characters, convert to int, add one to it and use that. 4) Reattach that to the code for this month. Ignoring that you seem to have a fetish over naming your variables after months of the year, and the "str" prefix is a bit of a waste of time, since half way through you hold integers in them. What is the bit that is giving you problems? Apart from the whole approach, which is frankly rubbish? If you must have an item number that is a string, with a date and a sequence number, then: 1) Read the last record. 2) Assume none: set integer default to 0 in variable called nextSequenceNumber 3) If record exists, extract last 6 characters, and parse to int. Store in nextSequenceNumber. 4) Return date code + (nextSequenceNumber + 1).ToString("000000") Assume the database will contain corrupt data - use TryParse rather than parse, and log any failures. Try to deal with them nicely.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
I have something similar in VB6 with MDB but have difficulty using C# and MSS. See my VB6 code below and advice on how to produce something similar in C#. Public Sub GenerateCode(ByRef Coder As String) Dim strMar As String, intApr As Integer Dim strJul As String Coder = UCase(Coder) Dim rs As New ADODB.Recordset Dim cn As New ADODB.Connection On Error GoTo AddNewErr cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Manager.mdb" rs.Open "select ItemNo from Items where left(ItemNo,8) like '" + Coder + "' order by ItemNo asc", cn, adOpenKeyset, adLockOptimistic If rs.RecordCount <> 0 Then rs.MoveLast If Left(Trim(rs.Fields!ItemNo), 8) = Coder Then strMar = Right(Trim(rs.Fields!ItemNo), 6) strMar = Val(strMar) + 1 strMar = CStr(strMar) If (6 - Len(strMar)) <> 0 Then strMar = String$(6 - Len(strMar), "00000") & strMar End If Else strMar = "000001" End If Else strMar = "000001" End If Coder = Coder & strMar Editor = Coder Set rs = Nothing Set cn = Nothing Exit Sub AddNewErr: MsgBox Err.Description, vbExclamation, "Error Report" End Sub
There is a lack of PRE tags, semi-colons and curly braces. These codez don't belong here. :(
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
There is a lack of PRE tags, semi-colons and curly braces. These codez don't belong here. :(
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
PRE tags, semi-colons and curly braces are the least of the problems! Even by VB standards, that is some clumsy code...
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy