0
我想從數據庫的數據集中獲取值。我創建了一個登錄表單,當我登錄時,它會向我顯示用戶的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);
}
}
你救了我^^我沒有想到在 –
它的作用,但它如何? hehehe –
我更新了你的代碼,以避免錯誤,正確的代碼'HabibisGrll.Globals.cashier = Convert.ToInt32(ds.Tables [0] .Rows [0] .ItemArray [0]);'非常感謝^^ –