Ho to create Auto Generated number in vb.net 2003

db , vb.net United States
  • 7 years ago
    Dear All,
    i have problem in producing the auto generated number. my requirements are to produce series of numbers like that
    01
    02
    03
    upto 100
    If there is any person who help me in producing that module / algorithm or piece of code in
    VB.NET 2003 , i will be thankful for that.

    NOTE:- the number should me auto generated i.e., after one transaction the new number would be generated.
    I Hope that there is someone which will help me.

    Best regards.






  • 7 years ago
    I'm not sure what your after is really much of an algorithm.  It's just basic addition.  Is that really an algorithm? :)  Is this for a database?  Because in most databases I believe you can create an auto increment field.  If you need to do this yourself then all it consists of is creating a global integer and setting it to 0 (or wherever you want to start).  Then when a transaction takes place add 1 to it.  You could also make a simple class for this.
    Public Class Form1
    
        Dim myAutoNumber As New AutoNumber
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MsgBox(myAutoNumber.GetNumber)
        End Sub
    End Class
    
    ' A class to generate a series of numbers
    Public Class AutoNumber
        Private _Value As Integer
    
        Public Sub New()
            Me.New(0)
        End Sub
    
        Public Sub New(ByVal startValue As Integer)
            _Value = startValue
        End Sub
    
        Public Function GetNumber() As Integer
            Dim result As Integer = _Value
            _Value += 1
            Return result
        End Function
    End Class
  • 7 years ago

    dear thomas (two faced),

    can i ask here a question, small doubts.

    for generating auto number why should we use class(sub new, set value, return value) . is there any good idea behind that. Please let me know. thanks

  • 7 years ago
    trafeeque wrote:

    dear thomas (two faced),

    can i ask here a question, small doubts.

    for generating auto number why should we use class(sub new, set value, return value) . is there any good idea behind that. Please let me know. thanks

    I wasn't making any claims that a class is necessary or should be used.  It was just an example that demonstrated what the OP wanted.  However, creating a class has the benefits of being reusable.  It also packages all the logic in one nice place. 
  • 7 years ago

    Thanks for giving piece of code.

    My req is

    Example is below

    1.When i click the button first time  it will create a new text file  name as 1sample.txt

    2.when i click the button second time it will create a new text file with name as 2sample.txt.

    .

    .

    .

    upto n times                                                                                               name as nsample.txt 

    Again next day if i do like this the files will not create as  above name ,it should be generated as  different number name

     

     

     

    Can u give me an advise or piece of code without using database ,

    Thanks,

    Yr

  • 7 years ago
    In that case you might write some code that will find the next free file number.  Look at this code for example.
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' Location our files will be stored
            Const location As String = "c:\"
            ' Name of the file (* will the replaced by the fileNumber)
            ' In this case each filename would look like Log_1.txt, Log_2.txt etc.
            Const fileName As String = "Log_*.txt"
    
            ' Start at 0
            Dim fileNumber As Integer = 0
            ' The file we want to create
            Dim file As String
    
            ' Loop until we find a file number that hasn't been used
            Do
                ' The full filename to test
                file = IO.Path.Combine(location, fileName.Replace("*", fileNumber.ToString))
    
                If Not IO.File.Exists(file) Then
                    ' File doesn't exist...we can use it
    
                    ' Exit this loop
                    Exit Do
                End If
    
                ' Increase the fileNumber and try again
                fileNumber += 1
            Loop
    
            MsgBox(file)
        End Sub
    The one thing to note about this code is it will use the first free file it finds. This means if you created a few logs like Log_0.txt Log_1.txt and Log_2.txt and then for some reason Log_0.txt was deleted that name would be used again for the next file created. This may or may not be acceptable. If the file number should increase no matter what you will probably need to store the last number used somewhere. The registry or a textfile might be appropriate. In that case you could just read that value add 1 to it and you've got your file number.
  • 7 years ago

    thks for your explaination. still can you explain me the following.

    using of class is fine. you can use the same code whenever you required. then why do you use 'public sub new()' with parameter and function getnumber(). instead we can written in one function itself. i know that this is right procedure, everybody using this type only. i do also wanted to know the procedure. please clarify. thanks

  • 7 years ago

    The 'new' procedure is a constructor.  They get called when a class is created.  You can overload that procedure as I've done to accept different values.  The reason you do this is either to make setting up the class easier or because the class requires the information.  Take the 'StreamReader' class for instance.  You can't create a new instance without at least supplying a file.  The 'String' class on the other hand doesn't require values to be passed when you create an instance, but it does have an overloaded constructor that accepts paramaters.  In this case it's a matter of convenience and flexibilty.

    In my classes example it's not really worth analyzing.  I spent a whole 30 seconds throwing an example together and didn't put any real thought into it.  The reason I accepted a parameter in the constructor is so the user could set up where the count should begin.  Once the class has been created this can't be changed so I needed to allow the person to set it initially.  If you wanted you could create a seperate property or function that sets the starting number which would allow a user to change it anytime.  No real right or wrong here it's just the reason I did what I did.  The getNumber function is actually very different from what the constructor did.  I'm not real sure how you think they could be combined into one function.  getNumber simply returns a number and then increments that number by 1.  The constructor actually set the starting value for this number.

  • 7 years ago
    Can you give me the piece of code for the above req.it is very urgent to me .
  • 7 years ago
    yr wrote:
    Can you give me the piece of code for the above req.it is very urgent to me .
    Just below your request I've posted my own reply along with some sample code.  Either you didn't see it, don't understand it, or it doesn't work for you.  I'm not sure what else to write besides take another look at it.  If you need some help understanding it please ask.  If it doesn't meet your needs please tell me how it fails to meet them so I might be able to help further.
  • 7 years ago

    Dim myDataReader As SqlDataReader

    Dim myDataReader1 As SqlDataReader

    Dim mySqlCommand As SqlCommand

    Dim mySqlCommand1 As SqlCommand

    Dim mySqlConnection As New SqlClient.SqlConnection

    Dim mySqlConnection1 As New SqlClient.SqlConnection

    mySqlConnection.ConnectionString = ConfigurationSettings.AppSettings("MM_CONNECTION_STRING")

    mySqlConnection1.ConnectionString = ConfigurationSettings.AppSettings("MM_CONNECTION_STRING")

    mySqlCommand1 =

    New SqlCommand("SELECT * FROM Table1", mySqlConnection1)

    mySqlConnection1.Open()

    myDataReader1 = mySqlCommand1.ExecuteReader(CommandBehavior.CloseConnection)

    Do While (myDataReader1.Read())

    Pa = myDataReader1.GetInt32(0).ToString()

    'Else

    ' ' Console.Write(myDataReader.GetInt32(4).ToString() + Chr(10))

    'End If

    Loop

    myDataReader1.Close()

    mySqlConnection1.Close()

     

    mySqlCommand =

    New SqlCommand("SELECT * FROM Table1 b,Table2 a where a.Status =1 and a.ship_flag=1", mySqlConnection)

    mySqlConnection.Open()

    myDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection)

    Dim rows As String

    Dim rcount, _Value1 As Integer

    sTransFile = Transfer & Pa & "_Rs_Transfer_" & _Value1 & ".txt"

     

    If Not System.IO.Directory.Exists(Application.StartupPath & "\Transfer\") Then

    System.IO.Directory.CreateDirectory(Application.StartupPath & "\Transfer\")

    End If

    'check the file

    Dim fs As FileStream = New FileStream(Application.StartupPath & "\Transfer\" & sTransFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)

    Dim sw As StreamWriter = New StreamWriter(fs, System.Text.Encoding.Unicode)

     

    in the place of    _Value1  I need to get number with increment order like (1,2,3..)  Please help me.

     

  • 7 years ago

    All you need to do is turn my code into a usefull function.  This can be accomplished with just a few tweaks.  This is how it might look.

        Private Function GetFreeFile(ByVal folder As String, ByVal name As String, ByVal replacement As String) As String
            ' Start at 0
            Dim fileNumber As Integer = 0
            ' The file we want to create
            Dim file As String
    
            ' Loop until we find a file number that hasn't been used
            Do
                ' The full filename to test
                file = IO.Path.Combine(folder, name.Replace(replacement, fileNumber.ToString))
    
                If Not IO.File.Exists(file) Then
                    ' File doesn't exist...we can use it
    
                    ' Exit this loop
                    Exit Do
                End If
    
                ' Increase the fileNumber and try again
                fileNumber += 1
            Loop
            Return file
        End Function

    The function takes 3 parameters:
    The location - Where the file will go
    The name - Should look something like "Log_*.txt" where the * will be replaced by a number
    The replacment string - Tells it which part of the name should be replaced by a number, I've used an * so that's what you should pass here. 


    Then if I understand your code correctly you could do something like this.

            ' The file pattern to use.  I've inserted an '*' where I want the number to be
            Dim filePattern As String = Transfer & Pa & "_Rs_Transfer_*.txt"
    
            ' The location to store the file
            Dim location As String = IO.Path.Combine(Application.StartupPath, "Transfer")
    
            ' This will be the fully qualified filename
            Dim file As String = GetFreeFile(location, filePattern, "*")
    The variable file should contain the complete pathname of the next free file for you to use in your stream.
  • 7 years ago
    A note about the previouse solution.  As I've already stated this code will find the first free file.  If you have a few files created for instance "Log_0.txt" "Log_1.txt" "Log_2.txt" and Log_1.txt is removed then the next free file returned will be Log_1.txt.  I don't know your exact requirments for how this should work.  You may need to figure out an alternative method depending on your exact requirments.  As I believe I have previously mentioned you may need to store the last filenumber used.  You might use the registry, a textfile, or even your database to store that information.  In this case all you need to do is read that value and add 1 to it then you would update the value.
  • 7 years ago
    Thank you for your code.It is working fine.
  • 7 years ago

    i think it not so good but you should try this

    do

    Dim rcount, _Value1 As Integer

    _Value1=1

    sTransFile = Transfer & Pa & "_Rs_Transfer_" & _Value1 & ".txt"

     

    If Not System.IO.Directory.Exists(Application.StartupPath & "\Transfer\") Then

    System.IO.Directory.CreateDirectory(Application.StartupPath & "\Transfer\")

    End If

    'check the file

    Dim fs As FileStream = New FileStream(Application.StartupPath & "\Transfer\" & sTransFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)

    Dim sw As StreamWriter = New StreamWriter(fs, System.Text.Encoding.Unicode)

    _Value1=_Value1+1

    if _Value1=rcount then exit do

    loop

     

    sorry if sth wrong ..

  • 7 years ago

    hello,

    the code was helpfull me to.

    but i want to ask u that can i genarate a automatic number from the last no.

    eg: i have records upto 34 no. i want to create 35.........nth no of reconrd beyond that.

     

    plz help me.

     

    thanks in advance

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Walking on water and developing software from a specification are easy if both are frozen.” - Edward V Berard