1 Replies - 907 Views - Last Post: 20 January 2014 - 06:57 AM Rate Topic: -----

#1 iamLazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-December 13

Generate Auto ID with a Prefix

Posted 20 January 2014 - 06:39 AM

Hi !

I have this code below

I need any comment/tips/suggestion please.

It's working for me but i'm not sure if my code is proper or right?

I don't want to get any error in the future when the time of our system checking

The possible result will be

If reservatonID is null then EMP001
else EMP002



 Private Sub MYID()

        Try

            Dim sql As String = "SELECT Max(reservationID) FROM Reservation"
            cmd = New MySqlCommand(sql, CNN)
            dr = cmd.ExecuteReader

            If dr.Read() Then
                If dr.IsDBNull(0) Then
                    Dim number As Integer = 1
                    Dim emplang As String = "EMP"
                    Dim append As String = emplang + number.ToString().PadLeft(3, "00")
                    txtReserveID.Text = append
                    

                Else
                    Dim empString As String = dr(0).ToString.Substring(0, 3)
                    Dim empID As Integer = dr(0).ToString.Substring(3) + 1
                    Dim append As String = empString + empID.ToString().PadLeft(3, "00")
                    txtReserveID.Text = append.ToString

                End If
            End If


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
            dr.Close()
        End Try




Is This A Good Question/Topic? 0
  • +

Replies To: Generate Auto ID with a Prefix

#2 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 181
  • View blog
  • Posts: 659
  • Joined: 03-February 10

Re: Generate Auto ID with a Prefix

Posted 20 January 2014 - 06:57 AM

If this application is going to be distributed you will probably run into problems with your reservation ID. Say for example, the application is running on 2 computers and both select the same max(reservationId), then when you go to save one will fail or multiple records with the same id will be created. One way to solve this could be to select for the max reservationid and then immediately save it back as an empty row, there by ensuring unique identity. Another way could be to gather all the information first and then after saving the data, provide the reservation id.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1