เช่น 0001/2554 แล้วจะให้มัน +1 เรื่อยๆแล้วตามด้วยปีเหมือนเดิมน่ะค่ะ ตอนเพิ่ม record แรกก็ไม่มีปัญหานะคะ
แต่พอเพิ่ม record ที่ 2 มัน ERROR ว่า Conversion from string "1/2554" to type 'Integer' is not valid. ที่ตรง "Tranfer_ID" อ่ะค่ะ
แทนที่มันจะเป็น 0002/2554 อ่ะค่ะ
นี่code นะคะ Code (VB.NET)
Private Sub Tranfer_id()
connection()
Dim sql As String
sb = New StringBuilder
sb.Append("Select MAX(Tranfer_ID) AS Tranfer_ID From STAFF_TR")
Sql = sb.ToString
da = New SqlDataAdapter(Sql, conn)
ds = New DataSet
da.Fill(ds, "STAFF_TR")
If IsDBNull(ds.Tables("STAFF_TR").Rows(0)("Tranfer_ID")) Then
id = CStr("0001/" & Now.Year + 543)
Else
id = CStr(CInt(ds.Tables("STAFF_TR").Rows(0)("Tranfer_ID").ToString.Substring(3)) + 1)
End If
conn.Close()
End Sub
Private Sub Tranfer_id()
connection()
Dim sql As String
Dim ids As String
Dim countID As Integer
sb = New StringBuilder
sb.Append("Select MAX(Tranfer_ID) AS Tranfer_ID From STAFF_TR")
Sql = sb.ToString
da = New SqlDataAdapter(Sql, conn)
ds = New DataSet
da.Fill(ds, "STAFF_TR")
If IsDBNull(ds.Tables("STAFF_TR").Rows(0)("Tranfer_ID")) Then
id = CStr("0001/" & Now.Year + 543)
Else
ids = ds.Tables("STAFF_TR").Rows(0)("Tranfer_ID")
ids = ids +1
countID = Len(ids )
If countID = 4 Then
id = ids & Now.Year + 543
ElseIf countID = 3 Then
id = "0" & ids & Now.Year + 543
ElseIf countID = 2 Then
id = "00" & ids & Now.Year + 543
ElseIf countID = 1 Then
id = "000" & ids & Now.Year + 543
End If
End If
conn.Close()
End Sub
Private Sub Tranfer_id()
connection()
Dim sql As String
sb = New StringBuilder
sb.Append("Select MAX(Tranfer_ID) AS Tranfer_ID From STAFF_TR")
Sql = sb.ToString
da = New SqlDataAdapter(Sql, conn)
ds = New DataSet
da.Fill(ds, "STAFF_TR")
If IsDBNull(ds.Tables("STAFF_TR").Rows(0)("Tranfer_ID")) Then
id = CStr(Now.Year + 543).Substring(2) & "-" & "0000"
Else
ids = ds.Tables("STAFF_TR").Rows(0)("Tranfer_ID")
ids = ids + 1
countID = Len(ids)
If countID = 4 Then
id = (Now.Year + 543).Substring(2) & "-" & ids
ElseIf countID = 3 Then
id = (Now.Year + 543).Substring(2) & "-" & "0" & ids
ElseIf countID = 2 Then
id = (Now.Year + 543).Substring(2) & "-" & "00" & ids
ElseIf countID = 1 Then
id = (Now.Year + 543).Substring(2) & "-" & "000" & ids
End If
End If
conn.Close()
End Sub
Private Sub Tranfer_id()
connection()
Dim sql As String
sb = New StringBuilder
sb.Append("Select MAX(Tranfer_ID) AS Tranfer_ID From STAFF_TR")
sql = sb.ToString
da = New SqlDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "STAFF_TR")
If IsDBNull(ds.Tables("STAFF_TR").Rows(0)("Tranfer_ID")) Then
'id = CStr(Now.Year) + 543 & "-" & "0000"
id = CStr(Now.Year + 543).Substring(2) & "-" & "0001"
'id = CStr(1)
Else
Dim stRef1 As String = (Now.Year+543-2500).ToString("00") & "-"
Dim stNextID As String = stRef1 & "0001"
ids = ds.Tables("STAFF_TR").Rows(0)("Tranfer_ID")
If ids.IndexOf(stRef1)>=0 Then
Dim n As Integer = Convert.ToInt32(ids.SubString(stRef1.Length))+1
stNextID = stRef1 & n.ToString("0000")
End If
MessageBox.Show("NextID: " & stNextID)
End If
conn.Close()
End Sub
Private Sub Tranfer_id()
connection()
Dim sql As String
sb = New StringBuilder
sb.Append("Select MAX(Tranfer_ID) AS Tranfer_ID From STAFF_TR")
sql = sb.ToString
da = New SqlDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "STAFF_TR")
If IsDBNull(ds.Tables("STAFF_TR").Rows(0)("Tranfer_ID")) Then
id = "ID" & "001"
Else
id = CStr(CInt(ds.Tables("STAFF_TR").Rows(0)("Tranfer_ID").ToString.Substring(3)) + 1)
ids = (CInt(ds.Tables("STAFF_TR").Rows(0)("Tranfer_ID").Substring(3)))
ids = ids + 1
id = "ID" & ids.PadLeft(3, '0')
End If
conn.Close()
End Sub