我想从数据库的数据集中获取值。我创建了一个登录表单,当我登录时,它会显示用户的ID号和MessageBox中的用户名。
当我尝试将ID值传递给Globals变量时,我得到0.我正在使用for循环和while循环。每当我将该值传递给另一个表单时,我仍然会得到0.我不知道我的代码中存在什么问题。
你能指出我正确的方向吗?int i ;
HabibisGrll.Globals.cashier = i;
private void but_log_in_Click(object sender, EventArgs e)
{
if (tbx_username.Text == "" || Tbx_Password.Text == "")
{
MessageBox.Show("Please provide UserName and Password");
return;
}
try
{
//Create SqlConnection
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand("Select * from Tbl_Cashier where FName=@username and Password=@password", con))
using (SqlDataAdapter adapt = new SqlDataAdapter(cmd))
{
con.Open();
cmd.Parameters.AddWithValue("@username", tbx_username.Text);
cmd.Parameters.AddWithValue("@password", Tbx_Password.Text);
HabibisGrll.Globals.sss = tbx_username.Text;
DataSet ds = new DataSet();
adapt.Fill(ds);
con.Close();
int count = ds.Tables[0].Rows.Count;
//while(i <= ds.Tables[0].Rows.Count - 1)
//{
// MessageBox.Show("ID Number : " + ds.Tables[0].Rows[i].ItemArray[0] + Environment.NewLine + "USER : " + ds.Tables[0].Rows[i].ItemArray[1], "User INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
// i++;
//}
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
MessageBox.Show("ID Number : " + ds.Tables[0].Rows[i].ItemArray[0] + Environment.NewLine + "USER : " + ds.Tables[0].Rows[i].ItemArray[1], "User INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//If count is equal to 1, than show frmMain form
if (count == 1)
{
this.Hide();
HabibisGrll fm = new HabibisGrll();
fm.Show();
}
else
{
MessageBox.Show("Login Failed!");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 0 :(得分:0)
这里有一些错误,例如,当它真正只有1条记录时你使用for循环有点奇怪,但无论如何,我认为你应该做的事情,改变尽可能少的代码:
private void but_log_in_Click(object sender, EventArgs e)
{
if (tbx_username.Text == "" || Tbx_Password.Text == "")
{
MessageBox.Show("Please provide UserName and Password");
return;
}
try
{
//Create SqlConnection
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand("Select * from Tbl_Cashier where FName=@username and Password=@password", con))
using (SqlDataAdapter adapt = new SqlDataAdapter(cmd))
{
con.Open();
cmd.Parameters.AddWithValue("@username", tbx_username.Text);
cmd.Parameters.AddWithValue("@password", Tbx_Password.Text);
HabibisGrll.Globals.sss = tbx_username.Text;
DataSet ds = new DataSet();
adapt.Fill(ds);
con.Close();
int count = ds.Tables[0].Rows.Count;
//If count is equal to 1, than show frmMain form
if (count == 1)
{
HabibisGrll.Globals.cashier = Int32.Parse(ds.Tables[0].Rows[0].ItemArray[0].ToString());
MessageBox.Show("ID Number : " + ds.Tables[0].Rows[0].ItemArray[0] + Environment.NewLine + "USER : " + ds.Tables[0].Rows[0].ItemArray[1], "User INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
HabibisGrll fm = new HabibisGrll();
fm.Show();
}
else
{
MessageBox.Show("Login Failed!");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}