Link to home
Start Free TrialLog in
Avatar of jlscot
jlscot

asked on

Variables not returning correct information

I'm not sure why my variables pin, firstName, and balance aren't giving correct values. When I run the app and pick an account number, I am able to enter a pin, and the messageBox displays welcome, but not the member's name. Then, when I hit the balance button, it prints 0 everytime. Im not sure why this is happening.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SQLDll;

namespace WindowsFormsApplication14
{

    public partial class Form1 : Form
    {
        private Connection myConnection;
        private Statement myStatement;
        private ResultSet myResultSet;
        String databaseURL = "http://www.boehnecamp.com/phpMyAdmin/razorsql_mysql_bridge.php";
        private string pin ,firstName, userAccountNumber;
        private double balance;
        private double withdraw = 0;
        private double deposit = 0;
        bool buttonClicked, buttonClicked2;

        public Form1()
        {
            InitializeComponent();
            
            try
            {
                //connect to database
                SQL sql = new SQL();
                myConnection = sql.getConnection(databaseURL);
                //create Statement for executing SQL
                myStatement = myConnection.createStatement(databaseURL);
                loadAccountNumbers();
                updateBalance();
            }
            catch (Exception)
            {
                Console.WriteLine("Cannot connect to database server");
            }
            //close statement and database connection 
           myStatement.close();
           myConnection.close();
        }
        private void Form1_Load(object sender, EventArgs e)
        {   
        }
        
        public void setText(string text)
        {
        }
        //load account numbers to ComboBox
        private void loadAccountNumbers()
        {
            //get all account numbers from database
            try
            {
                myResultSet = myStatement.executeQuery("SELECT accountNumber FROM accountInformation");
                // add account numbers to ComboBox
                while (myResultSet.next())
                {
                    accountNumberComboBox.Items.Add(myResultSet.getString("accountNumber"));
                }
                myResultSet.close(); // close myResultSet
            }//end try
            catch (Exception)
            {
                Console.WriteLine("Error in loadAccountNumbers");
            }
        }//end method to loadAccountNumbers

        private void retrieveAccountInformation()
        {
            //get account info
            try
            {
                myResultSet = myStatement.executeQuery("SELECT pin, " +
               "firstName, balanceAmount FROM accountInformation " +
               "WHERE accountNumber = '" + userAccountNumber + "'");

                //get next result
                if (myResultSet.next())
                {
                    pin = myResultSet.getString("pin");
                    firstName = myResultSet.getString("firstName");
                    balance = myResultSet.getDouble("balanceAmount");
                }
                myResultSet.close(); //close myResultSet
            }//end try
            catch (Exception)
            {
                Console.WriteLine("Error in retrieveAccountInformation");
            }
        }// end method retrieveAccountInformation

        //update database after withdrawing
        private void updateBalance()
        {
            //update balance in database
            try
            {
                myStatement.executeUpdate("UPDATE accountInformation" +
                               " SET balanceAmount = " + balance + " WHERE " +
                               "accountNumber = '" + userAccountNumber + "'");

            }
            catch (Exception)
            {
                Console.WriteLine("Error in updateBalace");
            }
        }//end method updateBalance

        private void accountNumberComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            accountNumberComboBox.Enabled = false;
            numberTextField.Text = " ";
            messageTextArea.Text = "Enter your PIN #.";
            button0.Enabled = true;
            button1.Enabled = true;
            button2.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = true;
            button5.Enabled = true;
            button6.Enabled = true;
            button7.Enabled = true;
            button8.Enabled = true;
            button9.Enabled = true;
            buttonDone.Enabled = true;

        }
        private void buttonEnter_Click(object sender, EventArgs e)
        {
            retrieveAccountInformation();
                numberTextField.Text = " ";
                buttonEnter.Enabled = false;
                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;
                button6.Enabled = false;
                button7.Enabled = false;
                button8.Enabled = false;
                button9.Enabled = false;
                button0.Enabled = false;
                buttonBalance.Enabled = true;
                buttonWithdraw.Enabled = true;
                buttonDeposit.Enabled = true;
                messageTextArea.Text = "Welcome" + firstName;
                updateBalance();
               if (buttonClicked == true)
                {
                    withdraw = Double.Parse(numberTextField.Text);
                    balance = balance - withdraw;
                    updateBalance();
                }
            
        }

        private void buttonBalance_Click(object sender, EventArgs e)
        {
            retrieveAccountInformation();
            updateBalance();
            messageTextArea.Text=(balance.ToString());
            //display balance to messageTextArea
        }

        private void buttonWithdraw_Click(object sender, EventArgs e)
        {
            buttonBalance.Enabled = false;
            buttonWithdraw.Enabled = false;
            button0.Enabled = true;
            button1.Enabled = true;
            button2.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = true;
            button5.Enabled = true;
            button6.Enabled = true;
            button7.Enabled = true;
            button8.Enabled = true;
            button9.Enabled = true;
            messageTextArea.Text = "Enter withdrawal amount:";
            buttonClicked = true;

        }

        private void buttonDone_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            button7.Enabled = false;
            button8.Enabled = false;
            button9.Enabled = false;
            button0.Enabled = false;
            buttonEnter.Enabled = false;
            buttonBalance.Enabled = false;
            buttonWithdraw.Enabled = false;
            buttonDone.Enabled = false;
            buttonDeposit.Enabled = false;
            accountNumberComboBox.Enabled = true;
            messageTextArea.Text = "Please select your account number.";
      

        }

        private void buttonDeposit_Click(object sender, EventArgs e)
        {
            buttonBalance.Enabled = false;
            buttonWithdraw.Enabled = false;
            button0.Enabled = true;
            button1.Enabled = true;
            button2.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = true;
            button5.Enabled = true;
            button6.Enabled = true;
            button7.Enabled = true;
            button8.Enabled = true;
            button9.Enabled = true;
            messageTextArea.Text = "Enter deposit amount:";
            buttonClicked2 = true;
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            setText("1");
            numberTextField.Text += "1";
            buttonEnter.Enabled = true;

        }


        private void button2_Click(object sender, EventArgs e)
        {
            setText("2");
            numberTextField.Text += "2";
            buttonEnter.Enabled = true;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            setText("3");
            numberTextField.Text += "3";
            buttonEnter.Enabled = true;

        }

        private void button4_Click(object sender, EventArgs e)
        {
            setText("4");
            numberTextField.Text += "4";
            buttonEnter.Enabled = true;

        }

        private void button5_Click(object sender, EventArgs e)
        {
            setText("5");
            numberTextField.Text += "5";
            buttonEnter.Enabled = true;

        }

        private void button6_Click(object sender, EventArgs e)
        {
            setText("6");
            numberTextField.Text += "6";
            buttonEnter.Enabled = true;

        }

        private void button7_Click(object sender, EventArgs e)
        {
            setText("7");
            numberTextField.Text += "7";
            buttonEnter.Enabled = true;

        }

        private void button8_Click(object sender, EventArgs e)
        {
            setText("8");
            numberTextField.Text += "8";
            buttonEnter.Enabled = true;

        }

        private void button9_Click(object sender, EventArgs e)
        {
            setText("9");
            numberTextField.Text += "9";
            buttonEnter.Enabled = true;

        }

        private void button0_Click(object sender, EventArgs e)
        {
            setText("0");
            numberTextField.Text += "0";
            buttonEnter.Enabled = true;

        }
    }
}

Open in new window

Avatar of jlscot
jlscot

ASKER

I think it is because userAccountNumber is never set to a value, so it is null. How to I set the variable userAccountNumber eqaul to the item selected from the comboBox?
Avatar of jlscot

ASKER

userAccountNumber = accountNumberComboBox.SelectedItem.ToString();
well I added this but that didnt fix my problem
ASKER CERTIFIED SOLUTION
Avatar of Member_2_861731
Member_2_861731
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.