ถ้าDB ไม่มีข้อมูลอะไรสามารถรัน OR5800001 ออกมาได้ค่ะแต่พอเพิ่ม OR5800001 เข้าไปใน DB ก็จะ Error Input string was not in a correct format. ต้องแก้ไขยังไงดีค่ะ (orderID เก็บค่าเป็น nchar)
Code (VB.NET)
Public Function order_id() As String
Dim SqlCon As New SqlConnection(con)
Dim sb As New StringBuilder
Dim intNum As String = ""
Dim id As String = ""
Dim ids As String = ""
sb.Append("SELECT MAX(orderID) AS orderID FROM OrderTest")
intNum = sb.ToString
Dim da As New SqlDataAdapter(intNum, SqlCon)
Dim ds As New DataSet
da.Fill(ds, "OrderTest")
SqlCon.Open()
If IsDBNull(ds.Tables("OrderTest").Rows(0)("orderID")) Then
id = "OR" & CStr(Now.Year + 543).Substring(2) & "00001"
Else
ids = "OR" & (CInt(ds.Tables("OrderTest").Rows(0)("orderID").Substring(4)))
ids = ids + 1 ' Error ตรงนี้ Input string was not in a correct format.
Dim CountID As Integer = Len(ids)
If CountID = 5 Then
id = "OR" & CStr(Now.Year + 543).Substring(2) & ids
ElseIf CountID = 4 Then
id = "OR" & CStr(Now.Year + 543).Substring(2) & "0" & ids
ElseIf CountID = 3 Then
id = "OR" & CStr(Now.Year + 543).Substring(2) & "00" & ids
ElseIf CountID = 2 Then
id = "OR" & CStr(Now.Year + 543).Substring(2) & "000" & ids
ElseIf CountID = 1 Then
id = "OR" & CStr(Now.Year + 543).Substring(2) & "0000" & ids
End If
End If
SqlCon.Close()
Return id
End Function
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), VB.NET