1

I am trying to check my database to see if a user's member_hwid column is null or not. If it's null, then run the function to update the hwid. However, if the column is not null; then run the code to login. The problem is, when the code is ran; I'm always getting the response "Not Null"; even when the user's hwid field is set as null. As seen below, here is my code:

        public void CHECKANDWRITEHWID()
        {
            string server = "72.167.59.139";
            string database = "DATABASENAME";
            string uid = "YOURUID";
            string password = "YOURPASSWORD";
            string ssl = "None";
            string connectionString = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password};SSL Mode={ssl};";
            using var connection = new MySqlConnection(connectionString);
            connection.Open();
            //using var command = new MySqlCommand("SELECT name = @name FROM core_members WHERE members_pass_hash IS NULL OR members_pass_hash = '';", connection);
            using var command = new MySqlCommand("SELECT member_hwid FROM core_members WHERE name = @name AND member_hwid IS NULL OR member_hwid = '';", connection);
            //using var command = new MySqlCommand("", connection);
            command.Parameters.AddWithValue("@name", maskedTextBox1.Text);
            //var hwid = (string)command.ExecuteScalar();
            var hwid = command.ExecuteNonQuery();
            if (hwid == null)
            {
                MessageBox.Show("null");
            }
            else
            {
                MessageBox.Show("Not null");
            }
                
        }

I have tried setting the user's member_hwid to null, to numbers and letters, or just an empty string. No matter the case; it is always returning the response as Not null. If the column "member_hwid" for let's say John Doe is empty, then I need it to return saying null and if it's not; to return saying it's not. I have tried adjusting my commands for the query as seen above but I am now lost; what am I doing wrong?

Charlieface
  • 52,284
  • 6
  • 19
  • 43
  • 2
    you might want to add parentheses in your WHERE statement (`name = @name AND (member_hwid IS NULL or member_hwid = "")`) – Gugu72 Jul 13 '23 at 07:46
  • Simply ```UPDATE table SET column = COALESCE(column, @new_value) WHERE ..```. The value will be set if it is NULL and not changed if it is not NULL/ None checking needed. – Akina Jul 13 '23 at 07:46
  • @Gugu72 it still displays as **Not Null**; even though the value is Null. – The Guild Of ASMR Jul 13 '23 at 09:27
  • uhm, the question was very probably wrongfully closed as duplicate, damn... – Gugu72 Jul 13 '23 at 09:30
  • @Akina I need it to check to see the name as well since there are a lot of empty `member_hwid`s, the idea is to check the name based off of what is in `maskedtextbox1.text`, and then check to see if that user's `member_hwid` is empty or null or if there is a value in that column. – The Guild Of ASMR Jul 13 '23 at 09:30
  • @Charlieface mind reopening the question? definitely not answered – Gugu72 Jul 13 '23 at 09:31
  • 1
    Other bugs: `ExecuteNonQuery` returns a Rows Affected number, not a value, you want `ExecuteScalar`. And if you expect a row which has a null value (as opposed to no row) then you need to check for `DBNull.Value` – Charlieface Jul 13 '23 at 09:35
  • @Charlieface, I have never used `DBNull.Value`, can you please point me in the right direction to learn it's usage for my issue? – The Guild Of ASMR Jul 13 '23 at 12:59
  • Basically you would need `var hwid = command.ExecuteScalar();` then if there is no row at all you check `hwid == null`. But if you want to check that a row was returned with a `NULL` value, do `hwid == DBNull.Value`. It's unclear what your intention is regarding `UPDATE` as you haven't shown that. – Charlieface Jul 13 '23 at 13:27
  • @Charlieface, correct, the idea of the function was first to see if the users column for member_hwid was empty or null and if no5 then run the main function... however, if it was null or empty; then to run another function that then adds the users hwid to their respective columns. – The Guild Of ASMR Jul 13 '23 at 14:53

0 Answers0