Click here to Skip to main content
15,921,989 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
// This is what i've tried first but i want the vice versa of this with current year.

   Public Sub incrPR()
        Dim curValue As Integer
        Dim result As String
        Dim s As String = "PR"
        Dim yr As String = Now.Year.ToString()
        Using con As SqlConnection = New SqlConnection(ConString)
            con.Open()
            Dim cmd = New SqlCommand("Select MAX(SpecialOrderNo) FROM SpecialOrder", con)
            result = cmd.ExecuteScalar().ToString()
            cmd.Parameters.Clear()
            If String.IsNullOrEmpty(result) Then
                result = "PR000"
            End If
            result = result.Substring(3)
            Int32.TryParse(result, curValue)
            curValue = curValue + 1
            result = s & curValue.ToString("D3")
            txtno.Text = result
        End Using
    End Sub

What I have tried:

Public Sub incrPR()
        Dim curValue As Integer
        Dim result As String
        Dim yr as string = "-" & Now.Year.ToString()
        Dim s As String = "-PR"
        Dim yr As String = Now.Year.ToString()
        Using con As SqlConnection = New SqlConnection(ConString)
            con.Open()
            Dim cmd = New SqlCommand("Select MAX(SpecialOrderNo) FROM SpecialOrder", con)
            result = cmd.ExecuteScalar().ToString()
            cmd.Parameters.Clear()
            If String.IsNullOrEmpty(result) Then
                result = "000"
            End If
            Int32.TryParse(result, curValue)
            curValue = curValue + 1
            result = curValue.ToString("D3") & yr & s
            txtno.Text = result
        End Using
    End Sub

//Sample output: 001-2018-PR but not number is not increasing
//Expected sample output: 001-2018-PR, 002-2018-PR, 003-2018-PR
Posted
Updated 7-Nov-18 23:42pm

You already posted this question in the VB forum. Please do not cross post.
 
Share this answer
 
VB
New SqlCommand("Select MAX(SpecialOrderNo) FROM SpecialOrder", con)

Getting 'max' is a bad idea when you want to generate the next unique key. The reason is that database is multiuser and more than 1 can do the same and end with same next key.
Have a look at :
SQL AUTO INCREMENT a Field[^]
Custom Auto-Generated Sequences with SQL Server - SQLTeam.com[^]
 
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