Private Sub GenID() 'ใช้รันรหัสDivisionID
Dim IDGen As Integer = 0
Dim Conn As New SqlConnection(webConnect)
Dim sql_text As String = " SELECT TOP 1 DivisionID FROM tblDivision ORDER BY DivisionID DESC"
'Dim sql_text As String = " Select Max(Right(DivisionID,5)) as DivisionID from tblDivision "
Dim Cmd As New SqlCommand(sql_text)
Cmd.CommandType = CommandType.Text
Cmd.Connection = Conn
Dim sda As New SqlDataAdapter
Dim dt As New DataTable
Try
Conn.Open()
sda.SelectCommand = Cmd
sda.Fill(dt)
If dt.Rows.Count > 0 Then
IDGen = CInt(dt.Rows(0)("DivisionID")) '<------ ERROR พอเข้าลูปนี้
IDGen = IDGen + 1
Gen = "DV" & IDGen.ToString("0000")
Else
Gen = "DV" & "0001"
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
Conn.Close()
sda.Dispose()
Conn.Dispose()
End Try
End Sub
Private Sub GenID() 'ใช้รันรหัสDivisionID
Dim IDGen As String = 0 '<-----ลองทั้งแบบ sting และ int ก็ยังไม่ได้ค่ะ
Dim Conn As New SqlConnection(webConnect)
Dim sql_text As String = " SELECT TOP 1 DivisionID FROM tblDivision ORDER BY DivisionID DESC"
Dim Cmd As New SqlCommand(sql_text)
Cmd.CommandType = CommandType.Text
Cmd.Connection = Conn
Dim sda As New SqlDataAdapter
Dim dt As New DataTable
Try
Conn.Open()
sda.SelectCommand = Cmd
sda.Fill(dt)
If dt.Rows.Count > 0 Then
IDGen = CStr(dt.Rows(0)("DivisionID")) '<----- ลองทั้งแบบ sting และ int ก็ยังไม่ได้ค่ะ
IDGen = IDGen + 1
Gen = "DV" & IDGen.ToString("0000") ' ตัว Gen ประกาศค่า Dim Gen As String = ""
Else
Gen = "DV" & "0001"
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
Conn.Close()
sda.Dispose()
Conn.Dispose()
End Try
End Sub