Returning one selected value from an Access DataBase in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jenniferc2007
    New Member
    • Apr 2010
    • 1

    #1

    Returning one selected value from an Access DataBase in C#

    Hello all!! I am coding a simulated ATM for my software eng class. I would consider myself to be an intermediate programmer, but I have run into a huge problem. I have opened and linked my database to my C# project;
    however I can not get the SQL statement "Select FreeChecking from BankAccount where PIN = ..."; to return the value that is saved in the database.
    HELP PLEASE! I am attaching snippets of my code

    Code:
    //--------------------------------------------------------
                //This method returns one selected record from database.
                //--------------------------------------------------------
    
                public DataTable RecordSelect(String Sql)
                {
                   
                    cmd = new OleDbCommand(Sql, conn);
                    adapter = new OleDbDataAdapter(Sql, conn);
                    dt = new DataTable();
                    adapter.Fill(dt);
                    return dt;
                }
    Code:
    String sqlText = String.Format("SELECT FreeChecking FROM BankAccount2 where PIN = {0};", PIN);
                  dt =   db.RecordSelect(sqlText);
    
    
    
                    for (int i = 0; i < atmData.Rows.Count; i++)
                    {
                        try
                        {
                            string msg = String.Format("{0} {1} {2:c} {3:c}",
                                atmData.Rows[i]["User"],
                                atmData.Rows[i]["PIN"],
                                atmData.Rows[i]["FreeChecking"],
                                atmData.Rows[i]["PersonalSavings"]);
    
                            MessageBox.Show(msg);
                        }
                        catch (Exception)
                        {
                        }
    
                    }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Until someone with more experience can offer more targeted advice I can point you toward these:

    Database tutorial Part 1
    Database tutorial Part 2

    Comment

    Working...