status = "add"
Dim sql As String = ""
sql = "Select Top 1 ProductID from Products Order by ProductID DESC"
With cn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strconn
.Open()
End With
Dim da As New SqlDataAdapter(sql, cn)
Dim ds As New DataSet
da.Fill(ds, "topid")
If ds.Tables("topid").Rows.Count = 0 Then
LblID.Text = "P" & "00001"
Else
Dim oldid As String
oldid = ds.Tables("topid").Rows(0).Item(0).ToString
LblID.Text = "P" & (CInt(oldid) + 1).ToString("00000")
End If
Dim MaxID As Object
Dim NewID As String
Dim SQLstring As String = "SELECT MAX(ID) FROM ProductsT"
Dim Cm As New SqlCommand(SQLstring, Conn) ' Conn คือ SQLConnection
MaxID = Cm.ExecuteScalar
If MaxID Is DBNull.Value Then
'เริ่มต้นเลขที่ใหม่
NewID = "P0001"
Else
Dim n as integer = tryCast(MaxID, String).Remove(0,1)
n += 1
NewID = "P" & n.Tostring("0000")
End If