Click here to Skip to main content
12,622,652 members (28,673 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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)
        {   
        }
 
        
        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 4-Nov-12 16:13pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hello,

Instead of this,

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


Try this,

//get next result
               if (myResultSet.ReadFirst())
               {
                   pin = myResultSet.getString("pin");
                   firstName = myResultSet.getString("firstName");
                   balance = myResultSet.getDouble("balanceAmount");
               }
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161128.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100