Hello, I'm trying to program a basic program at the moment and have run into some difficulties. I have a method as follows:

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

the method is called on another form on load. The methods its supposed to call are as follows:

public static void loadTblStaff()
        {
            // prepare, open and load the staff table into dataset ds2
            con.ConnectionString = dbProvider + dbSource;
            MessageBox.Show("loadTblStaff() called");
            con.Open();
            MessageBox.Show("load table staff connection opened");
                sql = "SELECT * FROM tblStaff";
                ds2 = new DataSet();

                da = new OleDbDataAdapter(sql, con);
                da.Fill(ds2, "tblStaff");

            con.Close();

        }




public static void loadTblCars()
        {
            // prepare, open and load the cars table into dataset ds1
            con.ConnectionString = dbProvider + dbSource;
            MessageBox.Show("Loadtablecars called");
            con.Open();
            MessageBox.Show("load table cars connection opened");
                sql = "SELECT * FROM tblCars";
                ds1 = new DataSet();

                da = new OleDbDataAdapter(sql, con);
                da.Fill(ds1, "tblCars");

            con.Close();
        }

I'm not sure why the program isnt working, but I tried to use messageboxes to find out where the program failed and it seems after the loadDB method there are no calls. I'm new to c# so I am not sure why this is. Please advise.

Recommended Answers

All 12 Replies

Why don't you set breakpoints so that the program falls into the debugger?
Because now you can see all the values of your variables and better find uot how your program works.

I dont know how to use break points im afraid!

I'm now getting the following message when I try to run the program

"The type initializer for 'WindowsFormsApplication1.Data' threw an exception."
the code it returns this on is as follows

load = Data.Validate(userName,userPin);

the method is as follows:

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

            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;
            }
        }

Debugging is a craft you definitely have to master. In Visual Studio this is dead easy. see here.
With MessageBoxes alone, you never going to know if your DB connection succeeded or if your DataTable is filled etc.
In the beginning it is sufficient that you can set a breakpoint and get to the values of your variables at that time in your program.

i set a break point and i tried stepping over it and I've noticed several things from my experiments, the connections aren't opened and the datasets remain empty when I try to test if the userdetails are valid. The earliest problem is the following:

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

loadTblStaff(); seems to be called but it skips loadTblCars(); entirely. Then it skips everything and goes straight back to my login form where it returns false. I have no idea what im doing..

Actually seems it gets to the Con.Open() line but nothing in between con.open and con.close is run. but i do not get any error messages. How can I find out why the connection is failing?

Do you have the right connectionstring?
Is the DB correctly installed?
I'm glad you noticed that a MessageBox can be handy, but that with debugging you can do alot more.

Yes the connection string worked when I was writing this before, but I decided to restart and write cleaner code.
It is, its definitely a problem with my code, just cant work out what.
I just wish I knew more about debugging but I need this done by tomorrow so I don't have time to read through too much, I need to hurry and fix this asap.

I was wondering why you are using 2 DataSets?
A DataSet can contain many tables look here.It would reduce the two opens en closes to one. Donno if that would matter much, but I should give it a consideration.

I thought about it but I wanted to keep it in seperate datasets so that its easier to manage and read.

Thank you for your help! i got a reply on another post and have it working now, I will take your advice about reading up on debugging, the break points and stepping into it really makes a difference to the way I go about problems now. Much appreciated.

Glad I could help and guide you a little. Success!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.