請幫忙。我有一個用戶名(文本框名稱txtUsername),密碼(文本框名稱txtPassword)和3個單選按鈕(rbAdmin,rbSec和rbTreasure)的VB.NET登錄表單。用戶名和密碼值取自SQL Server數據庫表UserDetails。登錄表單允許用戶使用正確的用戶名和密碼以及正確的單選按鈕的正確組合登錄。如何驗證VB.NET登錄窗體的放射按鈕?
EG如果我的用戶名是'john',密碼是'johns',我是祕書。選中的單選按鈕rbSec)
我只設法驗證用戶名和密碼,因爲我已經失敗了單選按鈕後登錄有人能幫助請在下面的代碼是我曾嘗試:。
Imports System.Data.OleDb
Public Class frmLogin
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Try
If txtUsername.Text = "" Then
MsgBox("All fields are required.")
Return
ElseIf txtPassword.Text = "" Then
MsgBox("All fields are required.")
Return
ElseIf rbAdmin.Checked = False And rbSec.Checked = False And rbTreasurer.Checked = False Then
MessageBox.Show("Select the user type!", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Using conn As New OleDbConnection("Data Source=JUNIOR-PC;Initial Catalog=TBSociety;Persist Security Info=True;User ID=sa;Password=adminadmin")
conn.Open()
Dim command As New OleDbCommand("SELECT COUNT(*) FROM UserDetails where UserName = '" & txtUsername.Text & "' and Password = '" & txtPassword.Text & "' ", conn)
Dim adapter As New OleDbDataAdapter
Dim ds As New DataSet
adapter.SelectCommand = command
adapter.Fill(ds, "UserDetails")
If ds.Tables("UserDetails").Rows.Count > 0 And ds.Tables("UserDetails").Rows.Count < 2 Then
If rbAdmin.Checked = True Then
Me.Hide()
frmHome.Show()
ElseIf rbSec.Checked = True Then
MsgBox("You will have limited access to parts of the system.")
Me.Hide()
frmHome.Show()
End If
End If
End Using
End If
Me.Hide()
frmHome.Show()
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR5", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
這是根據您的建議@Pradeep Kumar修改後的代碼。但是在運行時,我收到一條錯誤消息,其內容爲「必須聲明標量變量@用戶名」。請在此再糾正一次。我只是認爲它是一個我不能糾正的愚蠢錯誤。
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Try
If txtUsername.Text = "" Then
MsgBox("All fields are required.")
Return
ElseIf txtPassword.Text = "" Then
MsgBox("All fields are required.")
Return
ElseIf Not rbAdmin.Checked AndAlso Not rbSec.Checked AndAlso Not rbTreasurer.Checked Then
MessageBox.Show("Select the user type!", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Dim recordsFound As Integer
Using conn As New OleDbConnection("Provider=SQLOLEDB.1;Data Source=Junior-PC;Persist Security Info=True;User ID=sa;Password=adminadmin;Initial Catalog=TBSociety")
conn.Open()
Dim command As New OleDbCommand("SELECT COUNT(*) FROM Logins where Username = @Username and Password = @Password and Designation = @Designation", conn)
If rbAdmin.Checked Then
command.Parameters.AddWithValue("@Designation", "Admin")
ElseIf rbSec.Checked Then
command.Parameters.AddWithValue("@Designation", "Secretary")
ElseIf rbTreasurer.Checked Then
command.Parameters.AddWithValue("@Designation", "Treasurer")
End If
command.Parameters.AddWithValue("Username", txtUsername.Text)
command.Parameters.AddWithValue("@Password", txtPassword.Text)
recordsFound = command.ExecuteScalar
End Using
If recordsFound > 0 Then
If rbAdmin.Checked Then
txtUsername.Text = ""
txtPassword.Text = ""
rbAdmin.Checked = False
rbSec.Checked = False
rbTreasurer.Checked = False
Me.Hide()
frmHome.Show()
ElseIf rbSec.Checked Then
txtUsername.Text = ""
txtPassword.Text = ""
rbAdmin.Checked = False
rbSec.Checked = False
rbTreasurer.Checked = False
Me.Hide()
frmHome.AttendanceRegisterToolStripMenuItem.Enabled = False
frmHome.AdminTasksToolStripMenuItem.Enabled = False
frmHome.Show()
ElseIf rbTreasurer.Checked Then
frmHome.BackgroundImage = Image.FromFile("C:\Users\Junior\Documents\Visual Studio 2010\Projects\TBSSystem\TBSSystem\Resources\OFFISUPP.gif")
frmHome.BackgroundImageLayout = ImageLayout.Tile
frmHome.MemberToolStripMenuItem.Enabled = False
frmHome.MemberRecordsToolStripMenuItem.Enabled = False
frmHome.AttendanceRegisterToolStripMenuItem1.Enabled = False
frmHome.SecretarialServicesToolStripMenuItem.Enabled = False
frmHome.LogFileToolStripMenuItem.Enabled = False
frmHome.AdminTasksToolStripMenuItem.Enabled = False
frmHome.Show()
Else
MessageBox.Show("Invalid User Name, Password or Designation. Try again...")
Me.Hide()
frmHome.Show()
End If
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR5", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
你的問題與什麼有關*嵌入MS Word到VB.NET 2010 *? – 2014-09-02 10:51:41
哦,對不起。感謝提醒我。我現在糾正了它。 – 2014-09-02 11:05:25
對於這個小任務,您不需要'DataSet'或'DataAdapter'。你可以很容易地用'Command.ExecuteScalar'來完成,它只返回一個值。 – 2014-09-02 11:09:09