这是我的grideview代码:
我需要为此代码使用if语句:private const string select_query
需要向管理员显示第一个代码,向stockcontroller显示第二个代码。我试图在最后"UserID"
只隐藏一列。
我不知道如何调用它并使用if语句签署给不同的用户。
刚试了一下!!!
这是我的库存网格视图代码:
SqlCommand selectCommand = new SqlCommand(" Select * from New_User where User_Name=@USER_ID", conn);
selectCommand.Parameters.Add(new SqlParameter("USER_ID", txtusername.Text.ToString());
string UserType = null;
SqlDataReader reader = selectCommand.ExecuteReader();
bool rowfound = reader.HasRows;
if (rowfound)
{
while (reader.Read())
{
UserType = reader["User_Type"].ToString().Trim();
if (UserType == "Administrator")
{
GlobalVariablesClass.VariableOne = txtusername.Text;
private const string select_query = "SELECT TOP 3 id,stock_type,stock_no,no_of_pieces,Gem_Type,Weight,Image,Cost,Create_Date,Update_Date,UserID FROM Stock_Gems";
}
else if (UserType == "StockController")
{
GlobalVariablesClass.VariableOne = txtusername.Text;
private const string select_query = "SELECT TOP 3 id,stock_type,stock_no,no_of_pieces,Gem_Type,Weight,Image,Cost,Create_Date,Update_Date FROM Stock_Gems";
}
}
}
这是我的登录代码:
private void tbnlogin_Click(object sender, EventArgs e)
{
try
{
SqlCommand selectCommand = new SqlCommand(" Select * from New_User where User_Name=@USER_ID and Password=@PASS", conn);
selectCommand.Parameters.Add(new SqlParameter("USER_ID", txtusername.Text.ToString()));
String password = "";
using (SHA1 sha1 = SHA1.Create())
{
byte[] data = sha1.ComputeHash(Encoding.UTF8.GetBytes(txtpassword.Text));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; ++i)
{
sb.Append(data[i].ToString("x2"));
}
password = sb.ToString();
}
selectCommand.Parameters.Add(new SqlParameter("PASS", password));
string UserType = null;
SqlDataReader reader = selectCommand.ExecuteReader();
bool rowfound = reader.HasRows;
if (rowfound)
{
while (reader.Read())
{
UserType = reader["User_Type"].ToString().Trim();
if (UserType == "Administrator")
{
GlobalVariablesClass.VariableOne = txtusername.Text;
MessageBox.Show("Welcome ", "Admin Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
Admin_Menu frm = new Admin_Menu();
frm.bunifuFlatButton3.Visible = true;
frm.Show();
this.Hide();
}
else if (UserType == "StockController")
{
GlobalVariablesClass.VariableOne = txtusername.Text;
MessageBox.Show("Welcome ", "User Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
Admin_Menu frm = new Admin_Menu();
frm.bunifuFlatButton3.Visible = false;
frm.Show();
this.Hide();
}
}
}
else
{
MessageBox.Show(" Invalid User Or Password ", "Login ", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
reader.Close();
}
}
我有一个带有项目列的网格视图,如下所示:
ID|Stock_Type|Stock_No|No_of_pieces|Gem_Type|Weight|Image|Cost|Create_Date|Update_date|UserID|
我发现了这个,但我不知道如何从数据库中检索。
这被编入库存gridview加载器
DisplayData();
hello.Text = GlobalVariablesClass.VariableOne;
if (Roles.IsUserInRole("Administrator"))
{
this.dataGridView1.Columns[10].Visible = true;
}
else
{
this.dataGridView1.Columns[10].Visible = false;
}
**我使用了全局变体,这意味着登录的用户名将出现在所有页面上。