Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Conn = New SqlConnection
With Conn
If .State = ConnectionState.Open Then Conn.Close()
Conn.ConnectionString = StrCon
Conn.Open()
End With
Dim intNumRows As Integer
sql = "SELECT COUNT(*) FROM nk_Grand WHERE GID = '" & Me.txtUsername.Text & "' AND [GPass] = '" & Me.txtPassword.Text & "' "
Command = New SqlCommand(sql, Conn)
intNumRows = Command.ExecuteScalar()
If intNumRows > 0 Then
MessageBox.Show("ยินดีต้อนรับเข้าสู่ระบบ", "ระบบซ่อมคอม ร้าน NK", MessageBoxButtons.OK, MessageBoxIcon.Information)
nk_Main.Show()
Me.Hide()
ElseIf txtUsername.Text = "" Or txtPassword.Text = "" Then
MessageBox.Show("คุณกรอกข้อมูลไม่ครบ", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("ชื่อผู้ใช้ หรือ รหัสผ่านไม่ถูกต้อง", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
tmpG_User = txtUsername.Text
Conn.Close()
Conn = Nothing
txtUsername.Clear()
txtPassword.Clear()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dbCon As New SqlConnection("Data Source=IBM;Initial Catalog=db_test;User ID=sa;Password=ton")
If dbCon.State = ConnectionState.Open Then dbCon.Close()
dbCon.Open()
'
Dim sSQL As String
sSQL = "SELECT * FROM nk_Grand WHERE GID='" & Trim(txtUser.Text) & "' AND GPass='" & Trim(txtPassword.Text) & "'"
Dim DA As New SqlDataAdapter(sSQL, dbCon)
Dim dt As New DataTable
DA.Fill(dt)
'
If dt.Rows.Count = 0 Then
MsgBox("ชื่อผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง", MsgBoxStyle.Exclamation)
Exit Sub
Else
MsgBox("สวัสดี : " & dt.Rows(0)("GID") & " " & dt.Rows(0)("ngName") & vbNewLine & "ระดับ : " & dt.Rows(0)("ngGrand"), MsgBoxStyle.Information)
End If
End Sub
Code ที่พี่แก้ไขมามัน Login ได้แต่ผู้ดูแลระบบอ่ะครับ / ID ที่เป็นพนักงาน Login ไม่ได้
Quote:
Conn = New SqlConnection
With Conn
If .State = ConnectionState.Open Then Conn.Close()
Conn.ConnectionString = StrCon
Conn.Open()
End With
Dim sql As String
sql = "SELECT * FROM nk_Grand WHERE GID='" & Trim(txtUsername.Text) & "' AND GPass='" & Trim(txtPassword.Text) & "'"
Dim da As New SqlDataAdapter(sql, Conn)
Dim dt As New DataTable
da.Fill(dt)
If dt.Rows.Count > 0 Then
MsgBox("สวัสดีคุณ : " & dt.Rows(0)("nGName") & vbNewLine & "ระดับ : " & dt.Rows(0)("nGrand"), MessageBoxIcon.Information)
nk_MainAdm.Show()
Me.Hide()
ElseIf txtUsername.Text = "" Or txtPassword.Text = "" Then
MessageBox.Show("คุณกรอกข้อมูลไม่ครบ", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("ชื่อผู้ใช้ หรือ รหัสผ่านไม่ถูกต้อง", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
ตอนนี้แก้ไข Login ได้แล้วครับ
แต่ผมอยากให้ ID ที่เป็น "ผู้ดูแลระบบ" Login เข้าไปในฟอร์ม nk_MainAdm
ส่วน ID ที่เป็น "พนักงงาน" ให้ Login เข้าไปในฟอร์ม nk_MainEmp
ไม่รู้ว่าควรแก้เงื่อนไขให้เป็นแบบไหน
Quote:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Conn = New SqlConnection
With Conn
If .State = ConnectionState.Open Then Conn.Close()
Conn.ConnectionString = StrCon
Conn.Open()
End With
Dim sql As String
sql = "SELECT * FROM nk_Grand WHERE GID='" & Trim(txtUsername.Text) & "' AND GPass='" & Trim(txtPassword.Text) & "'"
Dim da As New SqlDataAdapter(sql, Conn)
Dim dt As New DataTable
da.Fill(dt)
If dt.Rows.Count > 0 Then
MsgBox("สวัสดีคุณ : " & dt.Rows(0)("nGName") & vbNewLine & "ระดับ : " & dt.Rows(0)("nGrand"), MessageBoxIcon.Information)
nk_MainAdm.Show()
Me.Hide()
ElseIf txtUsername.Text = "" Or txtPassword.Text = "" Then
MessageBox.Show("คุณกรอกข้อมูลไม่ครบ", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("ชื่อผู้ใช้ หรือ รหัสผ่านไม่ถูกต้อง", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
Conn.Close()
Conn = Nothing
txtUsername.Clear()
txtPassword.Clear()
End Sub
1. หากว่าต้องการเปิดฟอร์มแยกตามระดับของพนักงาน สังเกตุบรรทัดคำสั่ง nk_MainAdm.Show()
ให้นำ If มาเช็คก่อนว่า dt.Rows(0)("nGrand") นั้นมีค่าเป็นอะไรเช่น
Code (VB.NET)
If dt.Rows(0)("nGrand") = "ผู้ดูแลระบบ" Then
nk_MainAdm.Show()
Me.Hide()
elseIf dt.Rows(0)("nGrand") = "พนักงาน" Then
nk_MainEmp.Show()
Me.Hide()
End IF
เป็นต้น เห็นว่าเพียงการใช้ If เช็คค่าเพียงนิดเดียวก็ได้แล้ว ขึ้นว่าเราจะผลิกแผลงยังไง
Private Sub btn_Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Login.Click
Conn = New SqlConnection
With Conn
If .State = ConnectionState.Open Then Conn.Close()
Conn.ConnectionString = StrCon
Conn.Open()
End With
Dim sql As String
sql = "SELECT * FROM nk_Grand WHERE GID='" & Trim(txtUsername.Text) & "' AND GPass='" & Trim(txtPassword.Text) & "'"
Dim da As New SqlDataAdapter(sql, Conn)
Dim dt As New DataTable
da.Fill(dt)
If dt.Rows.Count > 0 Then
MsgBox("สวัสดีคุณ : " & dt.Rows(0)("nGName") & vbNewLine & "ระดับ : " & dt.Rows(0)("nGrand"), MessageBoxIcon.Information)
If dt.Rows(0)("nGrand") = "ผู้ดูแลระบบ" Then
nk_MainAdm.Show()
Me.Hide()
ElseIf dt.Rows(0)("nGrand") = "พนักงาน" Then
nk_MainEmp.Show()
Me.Hide()
End If
ElseIf txtUsername.Text = "" Or txtPassword.Text = "" Then
MessageBox.Show("คุณกรอกข้อมูลไม่ครบ", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtUsername.Focus()
Else
MessageBox.Show("ชื่อผู้ใช้ หรือ รหัสผ่านไม่ถูกต้อง", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtUsername.Focus()
End If
tmpG_ID = dt.Rows(0)("GID")
tmpG_GID = dt.Rows(0)("nGID")
tmpG_GName = dt.Rows(0)("nGName")
Conn.Close()
Conn = Nothing
txtUsername.Clear()
txtPassword.Clear()
End Sub
ส่วนข้อ2 วิธีที่ผมนำไปใช้คือ ประกาศค่าใน Module
Public tmpG_ID As String = ""
Public tmpG_GID As String = ""
Public tmpG_GName As String = ""