Generate AutoNumber
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Generate AutoNumber

  1. #1
    Join Date
    Feb 2008
    Posts
    4

    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

  2. #2
    Join Date
    Oct 2004
    Posts
    97

    Re: Generate AutoNumber

    doesn't the table create the auto number for you?

  3. #3
    Join Date
    Feb 2008
    Posts
    4

    Re: Generate AutoNumber

    No I haven't set it to Autonumber, and I wanted to have a customized autonumbering.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    Re: Generate AutoNumber

    Quote Originally Posted by jorjie View Post
    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

  5. #5
    Join Date
    Sep 2006
    Posts
    96

    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 08: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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center