Private Sub frmchances_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ts_save.Enabled = False
ts_cancel.Enabled = False
Dim fd As New fader
fd.FadeIn(Me, 0.25)
sw.Reset()
sw.Start()
E_readsonly()
lbl_idchance.BackColor = Color.FromArgb(0, 0, 0, 0)
lbl_namechance.BackColor = Color.FromArgb(0, 0, 0, 0)
strConn = DBConnString.strConn
Conn = New OleDbConnection()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
sb.Append("SELECT MAX(id_chance) As maxid FROM tbchance")
Dim sqlmx_id As String
sqlmx_id = sb.ToString()
Com = New OleDbCommand()
Dim dr As OleDbDataReader
With Com
.CommandText = sqlmx_id
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader()
While dr.Read
txt_idchance.Text = CStr((dr.Item("maxid")))
End While
End With
showdata()
formatdgvchance()
sw.Stop()
processtime = sw.ElapsedMilliseconds / 1000
_toolstriplabel.Text = "เวลาที่ใช้ : " & processtime.ToString("0.0000") & " วินาที"
End Sub
Private Sub frmchances_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ts_save.Enabled = False
ts_cancel.Enabled = False
Dim fd As New fader
fd.FadeIn(Me, 0.25)
sw.Reset()
sw.Start()
E_readsonly()
lbl_idchance.BackColor = Color.FromArgb(0, 0, 0, 0)
lbl_namechance.BackColor = Color.FromArgb(0, 0, 0, 0)
strConn = DBConnString.strConn
Conn = New OleDbConnection()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
sb.Append("SELECT MAX(id_chance)+1 As maxid FROM tbchance")
Dim sqlmx_id As String
sqlmx_id = sb.ToString()
Com = New OleDbCommand()
Dim dr As OleDbDataReader
With Com
.CommandText = sqlmx_id
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader()
While dr.Read
txt_idchance.Text = CStr((dr.Item("maxid")))
End While
End With
showdata()
formatdgvchance()
sw.Stop()
processtime = sw.ElapsedMilliseconds / 1000
_toolstriplabel.Text = "เวลาที่ใช้ : " & processtime.ToString("0.0000") & " วินาที"
End Sub
'Dim dr As OleDbDataReader
With Com
.CommandText = sqlmx_id
.CommandType = CommandType.Text
.Connection = Conn
'dr = .ExecuteReader()
'While dr.Read
'txt_idchance.Text = CStr((dr.Item("maxid")))
'End While
End With
Dim Result As Integer = CInt(Com.ExecuteScalar())
Private Sub ts_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_add.Click
ActionFlag = "ADD"
'ClearAllData()
Conn = New OleDbConnection()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Dim sb1 As New StringBuilder
'sb1.Append("SELECT MAX(id_chance)+1 As maxid FROM tbchance")
sb1.Append("SELECT IF((COUNT([id_chance])=0),1,MAX([id_chance])+1) As maxid FROM [tbchance]")
Dim sqlmx_id As String
sqlmx_id = sb1.ToString()
Com = New OleDbCommand()
Dim dr1 As OleDbDataReader
With Com
.CommandText = sqlmx_id
.CommandType = CommandType.Text
.Connection = Conn
dr1 = .ExecuteReader()
While dr1.Read
txt_idchance.Text = CStr((dr1.Item("maxid")))
End While
End With
txt_idchance.ReadOnly = True
txt_namechance.ReadOnly = False
ts_add.Enabled = False
ts_edit.Enabled = False
ts_save.Enabled = True
ts_cancel.Enabled = True
ts_del.Enabled = False
txt_namechance.Focus()
End Sub
ถ้าใช้โค้ดนี้ มันจะ error บรรทัดที่ 13
Code (VB.NET)
'Dim dr As OleDbDataReader
With Com
.CommandText = sqlmx_id
.CommandType = CommandType.Text
.Connection = Conn
'dr = .ExecuteReader()
'While dr.Read
'txt_idchance.Text = CStr((dr.Item("maxid")))
'End While
End With
Dim Result As Integer = CInt(Com.ExecuteScalar())
Private Sub ts_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_add.Click
ActionFlag = "ADD"
'ClearAllData()
Conn = New OleDbConnection()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Dim sb1 As New StringBuilder
'sb1.Append("SELECT MAX(id_chance)+1 As maxid FROM tbchance")
sb1.Append("SELECT IF((COUNT([id_chance])=0),1,MAX([id_chance])+1) As maxid FROM [tbchance]")
Dim sqlmx_id As String
sqlmx_id = sb1.ToString()
Com = New OleDbCommand()
Dim dr1 As OleDbDataReader
With Com
.CommandText = sqlmx_id
.CommandType = CommandType.Text
.Connection = Conn
dr1 = .ExecuteReader()
While dr1.Read
txt_idchance.Text = CStr((dr1.Item("maxid")))
End While
End With
txt_idchance.ReadOnly = True
txt_namechance.ReadOnly = False
ts_add.Enabled = False
ts_edit.Enabled = False <<< access control ข้าม thread ไม่ได้ครับ คุณต้องใช้ event-delegate
ts_save.Enabled = True
ts_cancel.Enabled = True
ts_del.Enabled = False
txt_namechance.Focus()
End Sub
Code (VB.NET)
'Dim dr As OleDbDataReader
With Com
.CommandText = sqlmx_id
.CommandType = CommandType.Text
.Connection = Conn
'dr = .ExecuteReader()
'While dr.Read
'txt_idchance.Text = CStr((dr.Item("maxid")))
'End While
End With
Dim Result As Integer = CInt(Com.ExecuteScalar())
<<< เป็นไปได้ สองแบบ คือ
เขียนใหม่แบบ ถ้ามันฟ้อง error น่ะ
Dim Result As Integer
Result = CInt(Com.ExecuteScalar())
กับ
command เป็น select command คุณต้องเอา data adapter มารับครับ ExecuteScalar ไม่ถูกต้องนัก
Private Sub frmchances_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ts_save.Enabled = False
ts_cancel.Enabled = False
Dim fd As New fader
fd.FadeIn(Me, 0.25)
sw.Reset()
sw.Start()
E_readsonly()
lbl_idchance.BackColor = Color.FromArgb(0, 0, 0, 0)
lbl_namechance.BackColor = Color.FromArgb(0, 0, 0, 0)
strConn = DBConnString.strConn
Conn = New OleDbConnection(strConn)
If Conn.State = ConnectionState.Open Then
Conn.Close()
End If
strCom As String = "SELECT MAX([id_chance]) As [maxid] FROM [tbchance]"
Com = New OleDbCommand(strCom, Conn)
Try
Conn.Open()
Dim Result As Integer = CInt(Com.ExecuteScalar())
Conn.Close()
txt_idchance.Text = Convert.ToString(Result + 1)
Catch ex As Exception
txt_idchance.Text = ex.Meaasge
End Try
showdata()
formatdgvchance()
sw.Stop()
processtime = sw.ElapsedMilliseconds / 1000
_toolstriplabel.Text = "เวลาที่ใช้ : " & processtime.ToString("0.0000") & " วินาที"
End Sub