Click here to Skip to main content
12,504,695 members (65,001 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# VB .NET SQL-Server VB.NET

I need to generate and store Invoice Numbers like in the format 'E000001'
How to do it in VB.Net. Please help me with a code logic or suggest a better approach.

Thanks in anticipation
Posted 15-Jan-13 15:53pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Here's some C#, but the VB isn't much different.

string s = System.String.Format ( "E{0:000000}" , 42 ) ;
System.Console.WriteLine ( "E{0:000000}" , 42 ) ;
cyanceenu 16-Jan-13 2:34am
this is the thing I'm looking for.. thanks a lot..
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I wrote this in notepad so i have no idea if it will even compile but it should at least point you in right direction.

//No Database Approach
int seedNum = 1;
char pad = '0';
private void Main()
	for(int i=0; i < 10; i ++)
		Console.WriteLine("E" + seedNum.ToString().padLeft(6,pad);
		seedNum += 1;
private string GenerateInvNo()
	return seedNum.ToString().PadLeft(6, pad);
//Database Approach

string ConnectionString = String.Format(@"Data Source = {0};User Id={1}; password={2}; Initial Catalog = {3};",
string query = "SELECT SeedNumber FROM YourTableName; UPDATE YourTableName SET SeedNumber = SeedNumber + 1";
SqlConnection connection = new SqlConnection(ConnectionString);
SqlCommand queryCMD = new SqlCommand(query, connection);
int seedNum = (int)queryCMD.ExecuteScalar();
char pad = '0';
Console.WriteLine("E" + seedNum.ToString().padLeft(6,pad);
cyanceenu 16-Jan-13 2:36am
It works fine.. though it didn't serves my purpose, but gave me some idea.. Thanks...
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Sub generate_inv()
        Dim id_tmp As String
        Query = "select top 1 InoviceNo from Invoice order by InoviceNo desc"
        cmd = New OleDbCommand(query, cn)
        dr = cmd.ExecuteReader
        If dr.HasRows = False Then
            id_tmp = "E000001"
            id_tmp = Format(Mid(dr("InoviceNo"), 2, 6) + 1,"E00000#")
        End If
        txtInvoice.Text = id_tmp
End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.160919.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100