0

My application starts with the login menu, a user provides his login credentials and the process of comparing passwords (hashed) goes on, logged in if everything is fine and error handlers kick in if there is an error.

My question is, is there a way to check if the targeted table is empty like not having any sort of data in it (just a skeleton table in a database). Because I am not willing 150+ employees to the table and they may leave their job, get promoted and get fired... so I wanna leave it for the admins running the HR of the company...

I used the Form_Activated event but nothing changed, tried the Form_Initialize event no luck. What am I doing wrong here?

Should I change my query? I am totally lost here cause I read through dozens of forms and NON got even close!

Using the code provided with the form initialize event did not work. for it will dispose the form and you just can not get around the problem or at least I couldn't!



try
{
    using (MySqlConnection connection = Connect())
    {
        DataTable table = new DataTable("employee");
        string checkuserexistance = "select count(uname) from employee";
        MySqlCommand command = new MySqlCommand(checkuserexistance, connection);
        using (MySqlDataReader reader = command.ExecuteReader())
        {
            if (reader.Read() && reader.FieldCount > 0 && reader.HasRows)
            {
                Form1_Load(sender, e);
                reader.Close();
            }
            else
            {
                DialogResult dialog = MessageBox.Show("Can not sign in as the given user, would you like to add a user now?", "Empty Database", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dialog == DialogResult.Yes)
                {
                    new Thread(() => new User_Managment().ShowDialog()).Start();
                    this.Close();
                }
                else
                {
                    Application.Exit();
                }
            }
        }
    }
}
catch (MySqlException ex)
{
    MessageBox.Show(ex.Message, "Error Connecting to Database!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
AKX
  • 152,115
  • 15
  • 115
  • 172
UnkownReality
  • 130
  • 13
  • If I'm understanding you correctly, maybe consider refactoring your application so this whole login/are-there-any-users procedure is in its own form, and you only show the authenticated main form after all that is done? – AKX Oct 26 '21 at 11:28
  • @AKX, I found a better solution, turned out I just need to edit my connection string as such `string checkuserexistance = "select LEAST(1,uname) from employee";` thanks for the idea tho. – UnkownReality Oct 26 '21 at 21:04

1 Answers1

2

Your logic is currently checking whether there are any rows returned:

MySqlCommand command = new MySqlCommand("select count(uname) from employee", connection);
using (MySqlDataReader reader = command.ExecuteReader())
{
    if (reader.Read() && reader.FieldCount > 0 && reader.HasRows)
    {
        // OK
    }
}

However, a SELECT COUNT(...) always returns (at least) one row so you'll need to also check that the count read from that single line is more than zero by reading the zeroth result column's value.

MySqlCommand command = new MySqlCommand("select count(uname) from employee", connection);
using (MySqlDataReader reader = command.ExecuteReader())
{
    if (reader.Read() && reader.FieldCount > 0 && reader.HasRows && reader.GetInt32(0) > 0)
    {
        // OK
    }
}
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Your answer works, yet as mentioned in the previous comment on the main question, `string checkuserexistance = "select LEAST(1,uname) from employee"; MySqlCommand command = new MySqlCommand(checkuserexistance, connection); using (MySqlDataReader reader = command.ExecuteReader()) { if (reader.Read() && reader.FieldCount > 0 && reader.HasRows) {` this approach worked too, using the `LEAST(1,uname)` did the trick thanks, buddy both approaches work ✌ – UnkownReality Oct 26 '21 at 21:09
  • I was Wondering what's the difference between `count` and `Least` – UnkownReality Oct 26 '21 at 21:10
  • 1
    They do entirely different things. `select LEAST(1,uname) from employee` will end up MySQL retrieving the usernames from all rows (and figuring out whether they're greater or smaller than `1`), whereas `COUNT(uname)` (or even better, `COUNT(*)`) will only count the rows and return the count. – AKX Oct 27 '21 at 04:22
  • thank you for the explanation mate, and with that said, using the `reader.Read() && reader.FieldCount > 0 ` should be enough in knowing if a table is data-voided or not! – UnkownReality Oct 27 '21 at 09:27
  • 1
    No, it's not, because you're making a query that always returns a row. You could do `SELECT id FROM employee LIMIT 1` if you really only want to look at `HasRows`, since that's bound to return no rows if there are no employees and 1 row when there's at least 1 employee. – AKX Oct 27 '21 at 09:54
  • @AKV I've been trying queries left and right but this LIMIT 1 is not working, I am always getting an `incorrect syntax near 1` sos with the syntax PLEASE – UnkownReality Mar 29 '22 at 21:40
  • Show the actual query you're running that results in the error. – AKX Mar 30 '22 at 05:12
  • `string checkuserexistance = "SELECT OperID FROM dbo.Operators LIMIT 1;";` @AKX – UnkownReality Mar 30 '22 at 09:39
  • @UnkownReality By any chance, did you switch databases in-between? `dbo` is an SQL Server thing as far as I know. – AKX Mar 30 '22 at 11:02
  • no mate I didn't even when removing the `dbo` the error persists, and i am using Mysql – UnkownReality Mar 30 '22 at 19:03
  • I don't know what to tell you given these clues - `SELECT OperID FROM Operators LIMIT 1` is a perfectly valid MySQL statement. – AKX Mar 30 '22 at 19:30
  • it throws an `error syntax near 1` i to be honest totally lost here! @AKX – UnkownReality Mar 30 '22 at 20:18
  • @UnkownReality "incorrect syntax near ..." is a Microsoft SQL Server error. Are you _absolutely_ sure you're using MySQL? It really doesn't sound like you are. – AKX Mar 31 '22 at 05:11