从数据集中获取值并将其传递给全局C#

从数据集中获取值并将其传递给全局C#

问题描述:

我想从数据库的数据集中获取值。我创建了一个登录表单,当我登录时,它会向我显示用户的ID号和MessageBox中的用户名。从数据集中获取值并将其传递给全局C#

当我尝试将ID值传递给Globals变量时,我得到0.我正在使用for-loop和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 [email protected] and [email protected]", 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 [email protected] and [email protected]", 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); 
    } 
} 
+0

你救了我^^我没有想到在 –

+0

它的作用,但它如何? hehehe –

+0

我更新了你的代码,以避免错误,正确的代码'HabibisGrll.Globals.cashier = Convert.ToInt32(ds.Tables [0] .Rows [0] .ItemArray [0]);'非常感谢^^ –