我正在用C#开发一个windows窗体应用程序,问题如下。
该要求是根据登录用户的角色更改我的主菜单项中的属性(例如:使某些菜单条项目不可见等)(例如:学生的主菜单不应包含"Lecturer“菜单条项目)
我的登录表单的代码如下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using DBL;
namespace Interfaces
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
public string UserID;
public string Password;
public string Role;
private void btnLogin_Click(object sender, EventArgs e)
{
String SQL = "SELECT * FROM LoginInfo";
string connectionString;
SqlConnection con;
connectionString = @"Data Source=DESKTOP-U9V5QGE\CL_HNDCSE_86_16;Initial Catalog=UNIMAP_WAD;Integrated Security=True";
con = new SqlConnection(connectionString);
con.Open();
SqlCommand comm = new SqlCommand(SQL, con);
SqlDataReader sqlDReader = comm.ExecuteReader();
UserID = txtUserID.Text;
Password = txtPassword.Text;
while (sqlDReader.Read())
{
Role = sqlDReader["Role"].ToString();
}
if (UserID == sqlDReader["UserID"].ToString() && Password == sqlDReader["Password"].ToString())
{
con.Close();
if (Role == "SuperAdmin")
{
CurrentCredentials.LoggedInUID = UserID;
CurrentCredentials.LoggedInRole = Role;
frmMainMenu objmainmenu = new frmMainMenu();
objmainmenu.Show();
}
else if (Role == "Admin")
{
CurrentCredentials.LoggedInUID = UserID;
CurrentCredentials.LoggedInRole = Role;
frmMainMenu objmainmenu = new frmMainMenu();
objmainmenu.Show();
objmainmenu.credentialsToolStripMenuItem.Visible = false;
}
else if (Role == "StudentCoordinator")
{
CurrentCredentials.LoggedInUID = UserID;
CurrentCredentials.LoggedInRole = Role;
frmMainMenu objmainmenu = new frmMainMenu();
objmainmenu.Show();
objmainmenu.credentialsToolStripMenuItem.Visible = false;
objmainmenu.lecturerToolStripMenuItem.Visible = false;
}
else if (Role == "Lecturer")
{
CurrentCredentials.LoggedInUID = UserID;
CurrentCredentials.LoggedInRole = Role;
frmMainMenu objmainmenu = new frmMainMenu();
objmainmenu.Show();
objmainmenu.credentialsToolStripMenuItem.Visible = false;
objmainmenu.studentToolStripMenuItem.Visible = false;
}
else if (Role == "Student")
{
CurrentCredentials.LoggedInUID = UserID;
CurrentCredentials.LoggedInRole = Role;
frmMainMenu objmainmenu = new frmMainMenu();
objmainmenu.Show();
objmainmenu.credentialsToolStripMenuItem.Visible = false;
objmainmenu.lecturerToolStripMenuItem.Visible = false;
}
else
{
MessageBox.Show("Invalid credentials", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Invalid credentials", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}另外,使用静态类"CurrentCredentials“来存储当前用户的用户ID和角色。驻留在类库"DBL“中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using DBL;
namespace DBL
{
public static class CurrentCredentials
{
private static string _LoggedInUID;
private static string _LoggedInRole;
public static String LoggedInUID
{
get
{
return _LoggedInUID;
}
set
{
_LoggedInUID = value;
}
}
public static String LoggedInRole
{
get
{
return _LoggedInRole;
}
set
{
_LoggedInRole = value;
}
}
}
}您可能会看到,我已经更改了登录代码本身的属性。我已经对主菜单>菜单条项目(private > public)进行了更改,因此我可以从程序中的任何位置访问这些菜单项。
此外,刚才在登录屏幕的以下部分收到此错误。
//error
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
//received from the line
if (UserID == sqlDReader["UserID"].ToString() && Password == sqlDReader["Password"].ToString())非常感谢您在这件事上提供的专业知识。
提前谢谢。
发布于 2020-05-10 15:41:00
你可能是指那个角色
if (UserID == sqlDReader["UserID"].ToString() && Password == sqlDReader["Password"].ToString())...在while循环中吗?如果它在while循环之后,则不能访问sqldreader,因为没有更多的数据可供读取。
https://stackoverflow.com/questions/61708584
复制相似问题