【发布时间】:2019-03-17 15:36:21
【问题描述】:
我正在尝试创建一个函数 GetUserID(),它返回用户 ID,我已将其插入到标签中,以便我可以在其他形式中使用它。
但是当我尝试将标签转换为 int32 时,标签似乎总是为空。我认为这是因为函数在我的代码中的位置。
见:
private void Loginbtn_Click(object sender, EventArgs e)
{
var LoginFunction = new LoginFunction();
var DataTable = new DataTable();
DataTable = LoginFunction.Login(Usernametxt.Text.Trim(), Passwordtxt.Text.Trim());
int UserID = Convert.ToInt32(DataTable.Rows[0]["USER_ID"]);
if (DataTable.Rows.Count == 1)
{
CalculatorMain calculatorMain = new CalculatorMain();
MainMenu mainMenu = new MainMenu();
UserIDlbl.Text = Convert.ToString(UserID);
MessageBox.Show("ID = " + UserID);
this.Hide();
mainMenu.Show();
}
else
{
MessageBox.Show("You entered the wrong username or password");
}
}
public int GetUserID()
{
int UserID;
if (Int32.TryParse(UserIDlbl.Text, out UserID))
{
UserID = Convert.ToInt32(UserIDlbl.Text);
}
else
{
MessageBox.Show("Error, Label for UserID could not be parsed");
}
return UserID;
}
我不知道我还能把这个函数放在哪里让它工作。
这是调用函数的代码,它以单独的形式使用。
private void WorkoutHistoryForm_Load(object sender, EventArgs e)
{
Login login = new Login();
int UserId = login.GetUserID();
this.sETSTableAdapter.Fill(this.gymDataSet.SETS, UserId);
}
我一直认为必须有更好的方法来执行此操作,而不是将 UserID 存储在标签中,但我不确定如何。
【问题讨论】:
标签: c# sql function label userid