-
July 30th, 2009, 03:07 AM
#1
Generate AutoNumber
Code:
Public Function AutoNum(tbl As String, fld As String, frmat As String, midStart As Integer) As String
Dim AutoNumRec As New ADODB.Recordset
AutoNumRec.Open "Select " & fld & " from " & tbl & " order by " & fld, con, adOpenDynamic, adLockOptimistic
Dim NNum As String
Dim LNum As Integer
If Not AutoNumRec.EOF Then
AutoNumRec.MoveLast
LNum = Val(Mid(AutoNumRec.Fields(fld), midStart))
NNum = frmat & Format(LNum + 1, "0000")
Else
NNum = frmat & "0001"
End If
AutoNum = NNum
AutoNumRec.Close
End Function
Does anyone knows how to Generate Autonumber in C#? I'm using this one but on VB6 can anyone can convert this into C#, Thanks
-
July 30th, 2009, 04:18 AM
#2
Re: Generate AutoNumber
doesn't the table create the auto number for you?
-
July 30th, 2009, 07:57 PM
#3
Re: Generate AutoNumber
No I haven't set it to Autonumber, and I wanted to have a customized autonumbering.
-
July 31st, 2009, 06:15 AM
#4
Re: Generate AutoNumber
Originally Posted by jorjie
No I haven't set it to Autonumber, and I wanted to have a customized autonumbering.
Sorry, but it's the wrong way to approach it
AutoNumber fields in databases are there for a reason
-
August 1st, 2009, 03:47 AM
#5
Re: Generate AutoNumber
Try this one
Code:
using System.Data.SqlClient;
public String AutoNum (String tbl, String fld, String frmat, int midStart)
{
String mSql = String.Concat("Select " , fld , " from " , tbl , " order by " , fld );
SqlDataAdapter tAd = new SqlDataAdapter(mSql, myCon.Connection);
DataSet tDs = new DataSet();
tAd.Fill(tDs, "AUTONUM");
int mCount = tDs.Table["AUTONUM"].Rows.Count;
String tNo;
if (mCount > 0)
{ tNo = tDs.Table["AUTONUM"].Rows[mCount-1][fld].ToString();
tNo = tNo.Substring(midStart);
tNo = tNo.PadLeft(4, '0');
}
else
tNo = '0001';
return tNo;
}
Last edited by kvwarun; August 1st, 2009 at 07:35 AM.
Reason: Code is not formatting properly even though i put the tag
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|