Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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

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 ) ;
 
Share this answer
 
Comments
cyanceenu 16-Jan-13 2:34am    
this is the thing I'm looking for.. thanks a lot..
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.

C#
//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);
}

//-------------------------------------------------------
C#
//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);
 
Share this answer
 
Comments
cyanceenu 16-Jan-13 2:36am    
It works fine.. though it didn't serves my purpose, but gave me some idea.. Thanks...
VB
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
 
Share this answer
 
Comments
Jayanta Modak 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900