Click here to Skip to main content
14,973,807 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project, one of my form need to record the orders while automatically generate an unique ID for them. The id should be in numbers.
In previous attempt, I have tried to obtain the amount of records in database + 1 as the Id. But the form has a delete function that removes record. When a record in the middle is removed,(eg: 3 out of 5), the next id generated will be 5, which will cause error.
Any suggestion to create an unique Id? At best, it will not bother the database. Thanks in advance

What I have tried:

Dim count As Integer
Private Sub Order_Load(sender As Object, e As EventArgs) Handles MyBase.Load
count = OrderBindingSource.Count + 1
End Sub

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
If OrderQtyTextBox.Text = "" Then
            MessageBox.Show("Please specify order quantity")
            count = count + 1
            Dim Pqty As Integer
            Integer.TryParse(PalletQtyTextBox.Text, Pqty)
            OrderTableAdapter.Insert(OrderNoTextBox.Text, VendorIDTextBox.Text, ItemNoTextBox.Text, OrderQtyTextBox.Text, OrderDateDateTimePicker.Value, OrderDateDateTimePicker.Value, StatusTextBox.Text, MultipackQtyTextBox.Text, Pqty)
            OrderNoTextBox.Text = count
            OrderQtyTextBox.Text = ""
            StatusTextBox.Text = ""
        End If
End Sub
Updated 6-Apr-19 3:58am

1 solution

Get the database to generate the ID for you by using the autonumber field type for your id.
Maciej Los 9-Apr-19 14:38pm

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