Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C#
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)
        {   
        }

        
        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 #.";
            buttonDone.Enabled = true;

        }
        private void buttonEnter_Click(object sender, EventArgs e)
        {
            retrieveAccountInformation();
                numberTextField.Text = " ";
                buttonEnter.Enabled = false;
                buttonBalance.Enabled = true;
                buttonWithdraw.Enabled = true;
                buttonDeposit.Enabled = true;
                messageTextArea.Text = "Welcome" + firstName;
                updateBalance();

            
        }

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


   
    }
}
Posted

1 solution

Hello,

Instead of this,

C#
//get next result
               if (myResultSet.next())
               {
                   pin = myResultSet.getString("pin");
                   firstName = myResultSet.getString("firstName");
                   balance = myResultSet.getDouble("balanceAmount");
               }



Try this,

C#
//get next result
               if (myResultSet.ReadFirst())
               {
                   pin = myResultSet.getString("pin");
                   firstName = myResultSet.getString("firstName");
                   balance = myResultSet.getDouble("balanceAmount");
               }
 
Share this answer
 
Comments
Member 9417196 5-Nov-12 9:56am    
Error 1 'SQLDll.ResultSet' does not contain a definition for 'ReadFirst' and no extension method 'ReadFirst' accepting a first argument of type 'SQLDll.ResultSet' could be found (are you missing a using directive or an assembly reference?)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900