I have been struggling with this since yesterday and I really need to get it finished. I have no clue why its not working. THe porblem im facing at the moment is as follows:

I have a method loadDB(); That is looks like this:

 public static void loadDB()
        {
            // load data into datasets  - this will be called on form2 the login screen            

             loadTblStaff();
             loadTblCars();
             con.ConnectionString = dbProvider + dbSource;

        }

I have stepped into the program and placed breakpoints and the method never does anything other than call the first method. Then it jumps off to the next section of the program. I call this loadDb method in a login form on load, the program then runs the first line which is to call loadTblStaff, but then it does nothing and awaits user interaction. When I type in details and click login it should run a validate method which uses the data loaded into the dataset. The form for login and the Data.cs class look like this:

login form:

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 System.Data.Common;
using System.Data.OleDb;  

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        // Variables------------------------------------------------------------------------------------------------------------------------   

        private static string userName;
        private static string userPin;

        private static int load;

        // Program Logic Below -------------------------------------------------------------------------------------------------------------

        private void Form2_Load(object sender, EventArgs e)
        {
            // calls the method to load the database
            Data.loadDB();

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            userName = txtBoxAccount.Text;
            userPin = txtBoxPass.Text;

            // determine if credentials are valid 
           load = Data.Validate(userName,userPin);

            // switch statement to determine which form to load if any 
           switch (load)
           {
               case 0: 
                   MessageBox.Show("Invalid Credentials '\n' Please try again");
                   txtBoxAccount.Clear();
                   txtBoxPass.Clear();                   
                   break;

               case 1:
                   MessageBox.Show("Valid Credentials! '\n' You will now be given Staff privelages");
                   loadForms.loadStaff();
                   this.Hide();
                   break;

               case 2:
                   MessageBox.Show("Valid Credentials! '\n' You will now be given Admin privelages");
                   loadForms.loadAdmin();
                   this.Hide();
                   break;

               default:
                   MessageBox.Show("Invalid credentials! '\n' Please try again.");
                   txtBoxAccount.Clear();
                   txtBoxPass.Clear();
                   break;
           }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {            
            this.Hide();
            loadForms.loadWelcome();
        }              

    }
}

and the Data class looks like this

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 System.IO;
using System.Data.Common;
using System.Data.OleDb;  


namespace WindowsFormsApplication1
{
    // This class will contain methods and fields needed to handle the data. 
    class Data
    {
        // Fields -----------------------------------------------------------------------------------------------------------------

        private static System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection();
        private static System.Data.OleDb.OleDbDataAdapter da;
        private static DataSet ds1; // tblCars
        private static DataSet ds2; //tblStaff

        private static string sql;
        private static string dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;";
        private static string dbSource = "Data Source = E:\\cars.accdb'";

        private static string userName;
        private static string userPin; 

        private static bool valid;

        private static int numOfRowsCars;
        private static int numOfRowsStaff;

        // Methods -----------------------------------------------------------------------------------------------------------------

        public static void loadDB()
        {
            // load data into datasets  - this will be called on form2 the login screen            

             loadTblStaff();
             loadTblCars();
             con.ConnectionString = dbProvider + dbSource;

        }

        public static void loadTblStaff()
        {
            // prepare, open and load the staff table into dataset ds2

            con.Open();

                sql = "SELECT * FROM tblStaff";
                ds2 = new DataSet();

                da = new OleDbDataAdapter(sql, con);
                numOfRowsStaff = da.Fill(ds2, "tblStaff");
                MessageBox.Show("Number of staff rows: " + numOfRowsStaff.ToString());
            con.Close();

        }

        public static void loadTblCars()
        {
            // prepare, open and load the cars table into dataset ds1

            con.Open();

                sql = "SELECT * FROM tblCars";
                ds1 = new DataSet();

                da = new OleDbDataAdapter(sql, con);
                numOfRowsCars = da.Fill(ds1, "tblCars");
                MessageBox.Show("Number of car rows: " + numOfRowsCars.ToString());
            con.Close();
        }       

        public static int Validate(string Name, string Pin)
        {
            // this method will be used to validate login credentials - it will return 0 for invalid, 1 for staff privalages and 2 for admin

            // check to see if log in details are valid
            for (int count = 1; count < numOfRowsStaff; count++)
            {
                userName = ds2.Tables["tblStaff"].Rows[count][0].ToString();
                userPin = ds2.Tables["tblStaff"].Rows[count][1].ToString();

                if (Name == userName && Pin == userPin)
                {
                    valid = true;
                }
                else
                {
                    valid = false;
                }
            }          



            if (valid && Pin == "9999")
            {
                // returning 2 will load the admin screen
               return 2;
            }
            else if (valid)
            {
                // returning 1 will load the staff screen
               return 1;
            }
            else
            {
                // returning 0 will clear the input fields and return an error message
                return 0;
            }
        }

    }
}

The program just skips everything and when I click login it returns 0 clearing the input fields. i dont understand whats wrong with this program. I realize I'm not good at this at all... but I need to get this done. Any help is greatly appreciated.

In the interest of getting your code working soon, this seems to be a problem:

loadTblStaff();
loadTblCars();
con.ConnectionString = dbProvider + dbSource;

You're creating the connection string after calling methods that need to use it. Try moving that last line above the load* method calls.

I think you should be seeing an unhandled exception at line 54 up there, where you try to open a connection that has an empty connection string. Is that what you mean by "never does anything other than call the first method"? Because if there's no exception, loadTblCars should always be called after loadTblStaff.

I tried moving the connection string and now the loadTblStaff() isnt being called, its still only running the first line of the method and skipping back to the form to wait for me to click the login button. I think moving the connection string fixed a problem im not getting to yet?

its still only running the first line of the method and skipping back to the form

This doesn't make sense... under normal execution, code isn't just skipped like that. If it's doing what you say, there must be an exception being thrown. Are you really not seeing an unhandled exception? What IDE are you using?

I'm using microsoft visual c# 2010 express edition. I'm not getting any error messages, I'm not sure if I'm just explaining my problem poorly or not, but I'm stuck and pretty clueless right now! if its not returning an error nad is just continuing on with the program but without loading the datasets then it would make sense that its like this but when I dont understand why it would be doing that. Its supposed to run that method to load the dataset when the form is loaded and when I use breakpoints its and messageboxes and step through the program, its just not doing that. I am just a beginner so maybe I'm overlooking something but I don't know what I should do.

It seems to me you're using the class without instantiating an instance of it. Perhaps something like this,Data NewData;, will help. Then use NewData.loadDB(); to call the methods.

Also just noticed, your datasource string seems to have an extra ' in it.

Edited 3 Years Ago by tinstaafl

Hm, okay. Weird. There are some circumstances under which I've seen .NET applications silently fail and/or die, but as far as I can tell, you're not doing anything nearly arcane enough for that to be a problem.

Can you zip up your entire solution and post it? I'd like to see if I can reproduce the problem over here.

There's no database in that archive.

But right off the bat it threw an exception until I got rid of that extra ' in dbSource.

Edited 3 Years Ago by tinstaafl

didn't notice that and havenb't been getting any exceptions myself, strange. I'l l try fix it and see if it makes a difference.

it made a difference but its still not working properly, thanks for pointing it out! maybe now I can find the other problems and finally get on with this.

Actually that fixed the connection problem there entirely, its now populating the datasets! Thank you very much, everyone has been very helpful. no idea why that wasn't giving me any exceptions, does this sort of thing happen a lot?

This question has already been answered. Start a new discussion instead.