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.