We help IT Professionals succeed at work.
Get Started

Database with C# help

nuk23
nuk23 asked
on
498 Views
Last Modified: 2013-12-17
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

Comment
Watch Question
Senior Software Engineer
CERTIFIED EXPERT
Top Expert 2009
Commented:
This problem has been solved!
Unlock 2 Answers and 7 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE