Search code examples
c#mysqldatareader

C# - MysqlDataReader not reading


This is the code atm:

if (e.KeyCode == Keys.Enter && chatTextRTB.Text.Length > 0) <br>
        {
            con.Open();
            MySqlDataReader mdr;
            MySqlCommand cmd;
            string sq6 = ("UPDATE `new`.`chat_mod` SET `cCstatus`= cCstatus + 1 WHERE  `ID`= 1;");
            cmd = new MySqlCommand(sq6, con);
            mdr = cmd.ExecuteReader();

            if (mdr.Read())
            {
                ***chatClean = mdr.GetInt32("cCstatus");***
                if(chatClean > 20)
                {
                    sq6 = ("UPDATE `new`.`chat_mod` SET `cCfirst`= cCfirst + 1, `cClast`= cClast+1 WHERE  `ID`= 1;");
                }
            }
            con.Close(); </pre>

chatClean = mdr.GetInt32("cCstatus");

isnt recieving any data from the table.


Solution

  • Updates do not return data. They return the number of rows affected. You should call ExecuteNonQuery instead of ExecuteReader.

    This answer suggests to make a procedure that selects what you've updated, although the method used isn't entirely safe since you can end up with rows that were added/removed between the two statements.