C# 从Dataset获取一个值并将其传递给Globals C

C# 从Dataset获取一个值并将其传递给Globals C,c#,dataset,globals,C#,Dataset,Globals,我想从数据库的数据集中获取一个值。我创建了一个登录表单,当我登录时,它会在MessageBox中显示用户的ID号和用户名 当我尝试将ID值传递给Globals变量时,得到0。我使用for循环和while循环。每次我将该值传递给另一个表单时,仍然得到0。我不知道我的代码中有什么问题 你能给我指一下正确的方向吗 int i ; HabibisGrll.Globals.cashier = i; private void but_log_in_Click(object sender, EventArg

我想从数据库的数据集中获取一个值。我创建了一个登录表单,当我登录时,它会在MessageBox中显示用户的ID号和用户名

当我尝试将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);
            }
        } 

这里有两个错误,例如,使用for循环时,实际上只有1条记录,这有点奇怪,但无论如何,我认为您应该这样做,更改尽可能少的代码:

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);
    }
} 

你救了我^^^我当时没有考虑到这一点,阿里更新了你的代码以避免错误,正确的代码是habisgrll.Globals.cashier=Convert.ToInt32 ds.Tables[0]。行[0]。项数组[0];非常感谢^^Convert.ToInt32在内部调用Int32.Parse,只要值不为null,那么它应该几乎相同。现在我再次看到它,我错过了一个ToString,因为itemarray是一个对象数组,而不是字符串,所以您所做的是完美的是。此外,如果要添加验证,可以使用TryParse验证值,即确保数据集对象可以转换为整数。您的原始代码不起作用,因为您正在保存该行的索引,该行的索引将始终为0,因为它只有1条记录。我修复了这个问题,并在静态类中保存了正确的值。