Private Sub AutoGenerateRun()
Dim myConnection As MySqlConnection
myConnection = New MySqlConnection("Database=testconn;Data Source=localhost;User Id=root;Password=root")
myConnection.Open()
Dim sqlTmp As String = ""
Dim comTmp As MySqlCommand = New MySqlCommand
Dim drTmp As MySqlDataReader
Dim tmpID As String = ""
Dim FirstID As Integer = 0
Dim LastID As Integer = 0
sqlTmp = "SELECT * FROM mytable ORDER BY T_id DESC LIMIT 0,1"
Try
With comTmp
.CommandType = CommandType.Text
.CommandText = sqlTmp
.Connection = Conn
drTmp = .ExecuteReader()
drTmp.Read()
tmpID = CStr(drTmp.Item("T_id"))
LastID = CInt(StringFromRight(tmpID, 2))
LastID = LastID + 1
txtid.Text = LastID.ToString("M000")
drTmp.Close()
End With
Catch
txtid.Text = "M001"
End Try
End Sub
Private Sub showcode()
cn.Open()
Dim sql As String = "SELECT * FROM mytable ORDER BY T_id DESC LIMIT 0,1"
dt = New DataTable
da = New MySqlDataAdapter(sql, cn)
da.Fill(dt)
If dt.Rows.Count > 0 Then
Dim i As Integer = dt.Rows(0).Item("T_id") + 1
txtid.Text = i.ToString("000#")
Else
txtid.Text = "0001"
End If
End Sub
Private Sub showcode()
cn.Open()
Dim sql As String = "SELECT * FROM mytable ORDER BY T_id DESC LIMIT 0,1"
dt = New DataTable
da = New MySqlDataAdapter(sql, cn)
da.Fill(dt)
If dt.Rows.Count > 0 Then
Dim i As Integer = dt.Rows(0).Item("T_id") + 1
txtid.Text = "M"+ i.ToString("000#")
Else
txtid.Text = "M"+"0001"
End If
End Sub
Private Sub AutoGenerateRun()
Dim myConnection As MySqlConnection
myConnection = New MySqlConnection("Database=testconn;Data Source=localhost;User Id=root;Password=root")
myConnection.Open()
Dim sqlTmp As String = ""
Dim comTmp As MySqlCommand = New MySqlCommand
Dim drTmp As MySqlDataReader
Dim tmpID As String = ""
Dim LastID As Integer = 0
sqlTmp = "SELECT * FROM mytable ORDER BY T_id DESC LIMIT 0,1"
Try
With comTmp
.CommandType = CommandType.Text
.CommandText = sqlTmp
.Connection = Conn
drTmp = .ExecuteReader()
drTmp.Read()
tmpID = CStr(drTmp.Item("T_id"))
LastID = CInt(StringFromRight(tmpID, 3))
LastID = LastID + 1
txtid.Text = "M"&LastID
drTmp.Close()
End With
Catch
txtid.Text = "M001"
End Try
End Sub
Private Sub AutoGenerateRun()
Dim myConnection As MySqlConnection
myConnection = New MySqlConnection("Database=testconn;Data Source=localhost;User Id=root;Password=root")
myConnection.Open()
Dim sqlTmp As String = ""
Dim comTmp As MySqlCommand = New MySqlCommand
Dim drTmp As MySqlDataReader
Dim tmpID As String = ""
Dim LastID As Integer = 0
sqlTmp = "SELECT * FROM mytable ORDER BY T_id DESC LIMIT 0,1"
Try
With comTmp
.CommandType = CommandType.Text
.CommandText = sqlTmp
.Connection = Conn
drTmp = .ExecuteReader() '<<<ดีบัคแล้วตรง drTmp มันไม่มีค่าเลยอะครับ
drTmp.Read()
tmpID = CStr(drTmp.Item("T_id"))
LastID = CInt(StringFromRight(tmpID, 3))
LastID = LastID + 1
txtid.Text = "M"&LastID
drTmp.Close()
End With
Catch '<<<แล้วก็มารันตรงบรรทัดนี้เลยครับ
txtid.Text = "M001"
End Try
End Sub
Private Sub auto_id()
Dim sqlcmd As MySqlCommand = New MySqlCommand
Dim dr As MySqlDataReader
Dim tempSql As String = "SELECT * FROM mytable ORDER BY T_id DESC LIMIT 0,1"
Dim tempId As String
cn.Open()
Try
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = tempSql
sqlcmd.Connection = cn
dr = sqlcmd.ExecuteReader
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
dr.Read()
If IsDBNull(dr.Item("T_id")) = True Then
txtid.Text = "M" & "0001"
Else
tempId = dr.Item("T_id")
txtid.Text = "M" & (tempId.Substring(3) + 1).ToString("0000")
End If
cn.Close()
End Sub