Click here to Skip to main content
13,087,566 members (81,407 online)
Rate this:
 
Please Sign up or sign in to vote.
Hi

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
Sri
Posted 15-Jan-13 15:53pm
Rate this: bad
 
good
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 ) ;
or
System.Console.WriteLine ( "E{0:000000}" , 42 ) ;
  Permalink  
Comments
cyanceenu 16-Jan-13 2:34am
   
this is the thing I'm looking for.. thanks a lot..
Rate this: bad
 
good
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};",
                            "DB_Server",
                            "DB_User",
                            "DB_Pass",
                            "DB_Name");
 
string query = "SELECT SeedNumber FROM YourTableName; UPDATE YourTableName SET SeedNumber = SeedNumber + 1";
 
SqlConnection connection = new SqlConnection(ConnectionString);
connection.Open();
SqlCommand queryCMD = new SqlCommand(query, connection);
 
int seedNum = (int)queryCMD.ExecuteScalar();
char pad = '0';
 
Console.WriteLine("E" + seedNum.ToString().padLeft(6,pad);
  Permalink  
Comments
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
 
good
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
            dr.Close()
            id_tmp = "E000001"
        Else
            dr.Read()
            id_tmp = Format(Mid(dr("InoviceNo"), 2, 6) + 1,"E00000#")
        End If
        dr.Close()
        txtInvoice.Text = id_tmp
End Sub
  Permalink  
Comments
Member 13024453 19-Jun-17 1:18am
   
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
dr.Close()
id_tmp = "E000001"
Else
dr.Read()
id_tmp = Format(Mid(dr("InoviceNo"), 2, 6) + 1,"E00000#")
End If
dr.Close()
txtInvoice.Text = id_tmp
End Sub


I use this code but some problem here, I con't understand what is the problem
Problem is : It is fine work blank database(means starting E000001) to E000009, after that showing the number E000011 after that no incensed the number. Please Help me.

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 |
Web01 | 2.8.170813.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2017
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