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
Me.OrderTableAdapter.Fill(Me.ORDDataSet.Order)
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")
        Else
            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)
            Me.OrderTableAdapter.Fill(Me.ORDDataSet.Order)
            OrderNoTextBox.Text = count
            OrderQtyTextBox.Text = ""
            StatusTextBox.Text = ""
        End If
End Sub
Posted
Updated 6-Apr-19 3:58am
v2

1 solution

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

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