2017-10-10 2 views
0

데이터베이스의 데이터 집합에서 값을 가져 오려고합니다. 나는 로그인 폼을 만들었고 로그인 할 때 사용자의 ID 번호와 MessageBox의 사용자 이름을 보여줍니다.데이터 집합에서 값 가져 오기 및 전역 C로 전달 #

ID 값을 전역 변수에 전달하려고하면 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 [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); 
      } 
     } 

답변

0

몇 가지 당신은 루프는 정말 단 한 기록이 있지만, 어쨌든, 내 생각을 heres 당신이해야 할 때, 변화하는 최소한의를 사용하고 조금 이상하다, 예를 들어, 잘못된 여기에있다 코드 :

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]);'thanks alot ^^ –

관련 문제