Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a program that generate serial numbers and then adds them to a database.

The serial numbers are generated within a for next loop which is based on the number of serials a user needs.


I have no problem checking if the code alread exists in the database but I'm having issues in how to create a new code and to check that one also. So if the code exists it will create another new code and check it again against the records in the database etc. until it has reached the number of all new codes needed.

VB
'For Loop 
 Dim i As Integer = Convert.ToInt32(OrderQuantity.Trim)
                Dim FinalSN As String = ""
                For sn As Integer = 1 To i
                   
                    FinalSN = GenSerial()

                    Dim SW1 As StreamWriter
                    Dim FS1 As FileStream
                    FS1 = New FileStream("c:\Temp\" & PO & ".txt", FileMode.Append)
                    SW1 = New StreamWriter(FS1)
                    SW1.WriteLine(Part.Trim & ":" & Description.Trim & ":" & "SN:" & FinalSN.Trim)
                    SW1.Close()
                    SerialArray.Add(FinalSN)
                    con2.Close()
                    Dim strCommand3 As String = "INSERT INTO SerialNo (SerialNumbers) values ('" & FinalSN & "')"
                    Dim cm3 As New SqlClient.SqlCommand(strCommand3, con2)
                    con2.Open()
                    Try

                        cm3.ExecuteNonQuery()
                    Catch ex As Exception
                        MessageBox.Show(ex.Message, "Error")

                    End Try
                    FinalSN = ""


                Next



VB
'Function for creating codes
Public Function GenSerial() As String
        Dim xCharArray() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray
        Dim xNoArray() As Char = "0123456789".ToCharArray
        Dim xGenerator As System.Random = New System.Random()
        Dim xStr As String = String.Empty

        While xStr.Length < 15

            If xGenerator.Next(0, 2) = 0 Then
                xStr &= xCharArray(xGenerator.Next(0, xCharArray.Length))
            Else
                xStr &= xNoArray(xGenerator.Next(0, xNoArray.Length))
            End If

        End While
        Return xStr

    End Function
Posted
Comments
jaket-cp 31-Oct-14 5:38am    
Have you consider using newid() on the sql side to generate the Serial numbers?
Try out select newid() NewID; in MSSQL Server.
johnjsm 31-Oct-14 8:14am    
I like this a lot but due to it going on a label I don't have room for it to fit. Code is being used for track and trace label
jaket-cp 31-Oct-14 8:18am    
to shrink it down you could do:
select substring(replace(newid(),'-',''),1,15) NewID_15;

1 solution

HI try this, I am bit dull in VB.net so i gave solution in C#, hope you can understand this.

string Verifyserialnumber( string serialno)
{
if(serialno exist)
{
string sno=Genserial()
serialno=Verifyserialnumber(sno);

}

return serialno;


}
 
Share this answer
 

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