Question : 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))
{

}
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
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);
         }
      }
   }
}

Answer : Database with C# help

Try:
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(acctno, a))
{
    //Put your db code here, lines 27-49. (use try/except for this code)
}
}
Random Solutions  
 
programming4us programming4us