TTT
TTT

Reputation: 13

Mysql update query not running ASP.NET

My mysql query in ASP.NET is not running. I have printed out string session so i know i have the value for it and connection string is working in all other methods so i know the issue is not there. Running the command manually in PHPMyadmin works fine. Another method which looks the same, only with select cmd works.

public IActionResult DeleteProfile()
    {
        string session = HttpContext.Session.GetString("session"); /* gets value (customerid) for user session */

        MySqlConnection conn = new MySqlConnection(connectionString);
        try
        {

            conn.Open();
            string cmdtxt = "UPDATE customer SET active = '0' WHERE customerid = @session";


            MySqlCommand cmd = new MySqlCommand(cmdtxt, conn);

            /**************** SQL PARAMETER ********///

            MySqlParameter parameter = new MySqlParameter();
            cmd.Parameters.AddWithValue("@session", session);

            /**************** SQL PARAMETER ********///


        }
        catch (Exception ex)
        {
            ViewBag.error = "Connection Error!\n" + ex.Message;


        }
        finally
        {

            conn.Close();
            ViewBag.error = "Account deleted";
            HttpContext.Session.Remove("session");
        }                

        return View("../Account/Index");
    }

Upvotes: 0

Views: 164

Answers (1)

CodeCaster
CodeCaster

Reputation: 151594

You're correctly instantiating the MySqlCommand and assigning the appropriate parameter, but then you're not doing anything with that command.

You need to execute the query, so it gets sent to the database server:

cmd.ExecuteNonQuery();

Upvotes: 4

Related Questions