Link to home
Start Free TrialLog in
Avatar of nuk23
nuk23

asked on

Database with C# help

this is the question:

1. Create a small BankAccount database with MS Access. Create one Account table in the database. The Account table should have fields for account number, customer last and first names, and current balance, Populate the table with 5-6 records. Design a user interface that allows the user to enter an account number. Your program would retrieve and display the current balance for the account.

and my solution is attached:


my  problem is that i need a loop so the console can keep asking me to put in the account numbers  and i dont want the problem to crash if i enter anything else than a numeric value and i know both the codes look something like this (look below) but i just dont know where to put them in my original coding can anyone please help me:

string s;
do
{

Console.WriteLine("Please enter Q to quit");
s = Console.Readline();
} while (s != "Q");

for the loop^^^

and so the problem wont crash when entering values:

string acctno = Console.ReadLine();
double a;
if (Double.TryParse(acctno, a))
{

}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace bankAccount
{
   class Program
   {
      static void Main(string[] args)
      {
         DataBaseDemo1();
      }

      static void DataBaseDemo1()
      {
         try
         {

            //Query user 
            Console.WriteLine("Enter Bank account Number ");
            double a = Convert.ToDouble(Console.ReadLine());

            //Check database for matching records
            string sConnection;
            sConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Dayspace\bank.mdb";
            OleDbConnection dbConn;
            dbConn = new OleDbConnection(sConnection);
            dbConn.Open();
            string sql;
            sql = "SELECT LastName, FirstName, CurrentBalance FROM bankAccountTable WHERE AccountNumber = " + Convert.ToString(a);
            OleDbCommand dbCmd = new OleDbCommand();
            dbCmd.CommandText = sql;    // set command  SQL string
            dbCmd.Connection = dbConn; // dbConn is connection object 
            OleDbDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();
            
            if (dbReader.Read())
            {
               Console.WriteLine("First Name: " + dbReader["FirstName"] + "\t Last Name: " + dbReader["LastName"] + "\t Balance: " + dbReader["CurrentBalance"]);
            }
            else
            {
               Console.WriteLine("No matching record");
            }
            dbReader.Close(); // Close the Reader object
            dbConn.Close(); // Close the Connection object
         }

         catch (Exception e)
         {
            Console.WriteLine(e);
         }
      }
   }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia 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
Avatar of nuk23
nuk23

ASKER

Error      1      The name 'acctno' does not exist in the current context      

thats the error im getting
string s = String.Empty;
while (s != "Q")
{
Console.WriteLine("Enter account number or enter Q to quit");
s = Console.Readline();
double a;
if (Double.TryParse(s, a))
{
    //Put your db code here, lines 27-49. (use try/except for this code)
}
}
Note: 1) Forgot to change acctno for s in Double.TryParse(s, a))
2) Once you have it working you can further improve the performance by creating the OleDbConnection dbConn before the loop.


SOLUTION
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
Avatar of nuk23

ASKER

getting alot of syntax errors...
Sorry, I fixed my typos (quick fingers), this code builds on VS2008
static void DataBaseDemo1()
        {
            string sConnection;
            sConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Dayspace\bank.mdb";
            OleDbConnection dbConn;
            dbConn = new OleDbConnection(sConnection);

            string s = String.Empty;
            while (s != "Q")
            {
                Console.WriteLine("Enter account number or enter Q to quit");
                s = Console.ReadLine();
                double a;
                if (Double.TryParse(s, out a))
                {
                    dbConn.Open();
                    try
                    {
                        //Check database for matching records
                        string sql;
                        sql = "SELECT LastName, FirstName, CurrentBalance FROM bankAccountTable WHERE AccountNumber = " + Convert.ToString(a);
                        OleDbCommand dbCmd = new OleDbCommand();
                        dbCmd.CommandText = sql;    // set command  SQL string
                        dbCmd.Connection = dbConn; // dbConn is connection object 
                        OleDbDataReader dbReader;
                        dbReader = dbCmd.ExecuteReader();

                        if (dbReader.Read())
                        {
                            Console.WriteLine("First Name: " + dbReader["FirstName"] + "\t Last Name: " + dbReader["LastName"] + "\t Balance: " + dbReader["CurrentBalance"]);
                        }
                        else
                        {
                            Console.WriteLine("No matching record");
                        }
                        dbReader.Close(); // Close the Reader object
                        dbConn.Close(); // Close the Connection object
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
        }

Open in new window

Avatar of nuk23

ASKER

Thanks for the help!