0

我的代码中有一个提款屏幕,当我提款时,我希望它更新访问数据库,但我的代码仅从程序中从数据库中删除金额。一旦程序关闭,数据库将重置为其先前的状态。

我的这个屏幕的代码

int moneyselected = 0;
Decimal balance = frmaccount.Balance;

public FrmTransaction()
{
    InitializeComponent();
}

private void btnWithdraw_Click(object sender, EventArgs e)
{
    if (moneyselected == 0)
    {
        MessageBox.Show("Select Amount");
    }
    else
    {
        if (moneyselected > balance)
        {
            MessageBox.Show("Not enough funds");
        }
        else
        {
            balance = balance - moneyselected;
            OleDbCommandBuilder cb = new OleDbCommandBuilder(FrmLogin.da);

            FrmLogin.da.Update(FrmLogin.ds1, "tblCustomers");
            MessageBox.Show("Amount Withdrawn = " + moneyselected.ToString() + "\r\nBalance = " + balance.ToString());
        }
    }
}

private void btnWithdrawreceipt_Click(object sender, EventArgs e)
{

}

private void btnExit_Click(object sender, EventArgs e)
{
    this.Close();
    Form FrmLogin = new FrmLogin();
    this.Hide();
    FrmLogin.Show();
}

private void FrmTransaction_Load(object sender, EventArgs e)
{

}

private void rb10_CheckedChanged(object sender, EventArgs e)
{
    moneyselected = 10;
}

private void rb20_CheckedChanged(object sender, EventArgs e)
{
     moneyselected = 20;
}

private void rb30_CheckedChanged(object sender, EventArgs e)
{
     moneyselected = 30;
}

private void rb40_CheckedChanged(object sender, EventArgs e)
{
     moneyselected = 40;
}

private void rb50_CheckedChanged(object sender, EventArgs e)
{
     moneyselected = 50;
}

private void rb60_CheckedChanged(object sender, EventArgs e)
{
     moneyselected = 60;
}

private void rb70_CheckedChanged(object sender, EventArgs e)
{
     moneyselected = 70;
}

private void rb80_CheckedChanged(object sender, EventArgs e)
{
     moneyselected = 80;
}

private void rb90_CheckedChanged(object sender, EventArgs e)
{
     moneyselected = 90;
}

private void Cashamountbox_Enter(object sender, EventArgs e)
{

}

private void rb100_CheckedChanged(object sender, EventArgs e)
{
     moneyselected = 100;
}

private void rb150_CheckedChanged(object sender, EventArgs e)
{
     moneyselected = 150;
}

private void rb250_CheckedChanged(object sender, EventArgs e)
{
     moneyselected = 250;
}
        }>>LOGIN PAGE<<
    }

namespace BANK_ATM_PROJECT
{
     public partial class FrmLogin : Form
     {
         public FrmLogin()
         {
             InitializeComponent();
         }

    int failedAttempts = 0;

    public static OleDbConnection con = new OleDbConnection();

    string dbProvider;
    string dbSource;

    public static OleDbDataAdapter da;

    public static DataSet ds1 = new DataSet();

    string sql;
    string pin;
    int rownum = 0;
    bool valid = false;

    public static decimal balance = 0;
    private void FrmLogin_Load(object sender, EventArgs e)
    {
        {
            dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;";
            dbSource = "Data Source = 'D:\\bank11.accdb'";
            con.ConnectionString = dbProvider + dbSource;
            ds1 = new DataSet();
            con.Open();
            sql = " SELECT tblCustomers.* FROM tblCustomers";
            da = new OleDbDataAdapter(sql, con);
            rownum = da.Fill(ds1, "tblCustomers");

            con.Close();

            {

            }
        }
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        System.Environment.Exit(0);
        this.Close();

    }
    private bool validate()
    {
        ds1 = new DataSet();
        con.Open();

        sql = "SELECT tblCustomers.* FROM tblCustomers WHERE ((tblCustomers.AccountNo) = '" + txtAccount.Text + "')";
        da = new OleDbDataAdapter(sql, con);
        rownum = da.Fill(ds1, "tblCustomers");
        con.Close();


        if (rownum != 1)
        {

            return false;

        }

        else
        {
            pin = ds1.Tables["tblCustomers"].Rows[0][4].ToString();
            if (pin == txtPin.Text)
            {
                return true;
            }
            else
            {
                MessageBox.Show("INVALID PIN");
                return false;
            }

        }
    }

    private void btnLogin_Click(object sender, EventArgs e)
    {

        valid = validate();
        if (!valid)
        {
            //Increment the number of failed attempts
            failedAttempts += 1;
            if (failedAttempts == 1)

                MessageBox.Show("You Have 2 Attempts Left");

            if (failedAttempts == 2)
                MessageBox.Show("You Have 1 Attempt Left");

            //If equal to 3
            if (failedAttempts == 3)

                MessageBox.Show("Card Retained");

            {

            }
        }
        else
        {
            valid = validate();
            if (valid == true)
            {
                if (txtAccount.Text == "11111111" && txtPin.Text == "9999")
                {
                    Frmmanager Manager = new Frmmanager();
                    this.Close();
                    Manager.Show();
                }
                else
                {
                    frmaccount account = new frmaccount();
                    this.Close();
                    account.Show();

                    {
                        txtAccount.Clear();
                        txtPin.Clear();

                        valid = validate();
                        if (valid == true)
                        {
                            if (txtAccount.Text == "11111111" && txtPin.Text == "9999")
                            {
                                Frmmanager Manager = new Frmmanager();
                                this.Close();
                                Manager.Show();
                            }
                            else
                            {
                                frmaccount Account = new frmaccount();
                                this.Close();
                                account.Show();

                                {
                                    txtAccount.Clear();
                                    txtPin.Clear();
                                }
                            }

                        }
                    }
                }
            }
        }
    }
}

}

我虽然 ds.update会更新它,但这似乎只在程序中工作

任何帮助或建议将不胜感激

4

2 回答 2

0

我认为问题出在以下代码中,在这里您创建了一个 FrmLogin 的新实例,而不是引用当前实例:

private void btnExit_Click(object sender, EventArgs e)
{
    this.Close();
    Form FrmLogin = new FrmLogin();       
    this.Hide();
    FrmLogin.Show();
}

尝试以下操作:

private void btnExit_Click(object sender, EventArgs e)
{
    this.Close();
    FrmLogin obj = (FrmLogin)Application.OpenForms["FrmLogin"];
    this.Hide();
    obj.Show();
}

通常,当您引用时,FrmLogin最好使用 obj 作为 ref 以确保您引用当前实例。因此,将其添加到代码的开头:

obj = (FrmLogin)Application.OpenForms["FrmLogin"];

并且每个裁判FrmLogin都通过obj

于 2013-03-24T10:05:29.643 回答
-1

您是在调试文件夹还是在项目解决方案中检查数据库?尝试检查调试文件夹。

于 2013-03-23T19:02:55.790 回答