0

So I'm trying to return a value from an input with asp.net. The however the issue is, I keep receiving a null value. But if I input the string as if it were inserted via the query it works fine.

Why am I getting a null value?

This Works:
 [HttpPost]
        [Route("A3Bans/searchBan")]
        public string oSearchBan(tBan ban)
        {
            {
                tBan bans = new tBan();
                string dbConnection = "datasource=127.0.0.1;port=3306;username=admin;password=00000";
                MySqlConnection conDataBase = new MySqlConnection(dbConnection);
                MySqlDataReader dbReader;

                MySqlCommand selectCommand = new MySqlCommand("SELECT `BanID`, `GUID`, `BanTime`, `Reason`, `BanType`, `Proof` FROM `a3bans`.`bans` WHERE  `GUID`= 'e7af78997ef220a557c97a1a4c11e0c2'", conDataBase);  //@prmGuid, conDataBase); // Returning a null value?!

               // selectCommand.Parameters.AddWithValue("@prmGuid", new tID(ban.GuidOrIP));

                conDataBase.Open();
                dbReader = selectCommand.ExecuteReader();
                try
                {

                    while (dbReader.Read())
                    {
                        tBan searchBan = new tBan();
                        searchBan.BanID = dbReader.GetString("BanID");
                        searchBan.GuidOrIP = dbReader.GetString("GUID");
                        searchBan.BanTime = dbReader.GetString("BanTime");
                        searchBan.BanType = dbReader.GetString("BanType");
                        searchBan.BanReason = dbReader.GetString("Reason");
                        searchBan.BanType = dbReader.GetString("BanType");
                        searchBan.Proof = dbReader.GetString("Proof");
                        bans = searchBan;
                    }

                }
                catch (Exception ex)
                {

                }
                finally
                {
                    dbReader.Close();
                    conDataBase.Close();
                }
                return bans.Proof;
            }
        }

However, as soon as I try to make it an input from another source it's null.

So my issue is here:

MySqlCommand selectCommand = new MySqlCommand("SELECT `BanID`, `GUID`, `BanTime`, `Reason`, `BanType`, `Proof` FROM `a3bans`.`bans` WHERE  `GUID`= '@prmGuid'", conDataBase); 

selectCommand.Parameters.AddWithValue("@prmGuid", new tID(ban.GuidOrIP));

The Class with Constructor: Which is probably completley wrong. tID

namespace A3Bans.Schemas
{
    public class tID
    {
        public tID(string guidOrIP)
        {
            GuidOrIP = guidOrIP;
        }

        public string GuidOrIP { get; set; }
        public string BanID { get; set; }

    }
}

Which if I had to guess it's my constructor... Any thoughts?

EDIT

Got it to partially work, it's just returning the wrong values.

MySqlConnection con = new MySqlConnection(dbConnection);
            MySqlCommand cmd;
            con.Open();
            MySqlDataReader dbReader;
            string cmdText = "SELECT `BanID`, `GUID`, `BanTime`, `Reason`, `BanType`, `Proof` FROM `a3bans`.`bans` WHERE  `GUID` LIKE @pGUID";


            cmd = new MySqlCommand(cmdText, con);

            cmd.Parameters.AddWithValue("@pGUID", "%" + bans.GuidOrIP + "%");

1 Answer 1

1

Do not put a parameter placeholder betweew single quotes. In this way it becomes a literal string and your query search a value for the column GUID that is equal to the literal string '@prmGuid' and, of course, nothing is returned with this condition.

Just change your sql command text to

..... WHERE `GUID`= @prmGuid", conDataBase); 
Sign up to request clarification or add additional context in comments.

9 Comments

I have, that was the last try at getting it to work. It still returns as null
Have you removed the comment from the AddWithValue line? And it is not clear what is the result of this code: new tID(ban.GuidOrIP)); it doesn't seem to return values like 'e7af78997ef220a557c97a1a4c11e0c2'
Well it's supposed to return the get string values based on the string inserted that is located in the database. But it just comes back as null.I'm new to c# and I've been on the line of code for 3 days now. My rubber ducky is across the house
What if AddWithValue(@prmGuid, ban.GuidOrIP);
it returns null, I don't have anything null in my DB either. ` MySqlCommand selectCommand = new MySqlCommand("SELECT BanID, GUID, BanTime, Reason, BanType, Proof FROM a3bans.bans WHERE GUID= @prmGuid", conDataBase);` selectCommand.Parameters.AddWithValue("@prmGuid", ban.GuidOrIP);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.