Home > Blockchain >  MySql not updating values in columns C# MVC app
MySql not updating values in columns C# MVC app

Time:11-09

I need to update the value of price of a product to reflect it on the database, but the UPDATE function is not reflected on the database. This is the code for the action Update

[HttpPost]
        public ActionResult UpdatePrice(Models.Product product)
        {

            // First thing to do is to check that the User that is updating the Product is a StoreManager

            // to be completed late


            string query = "UPDATE product SET price=@price = "   product.price   " WHERE productId=@idproduct";
            string constr = "server=localhost;user id=root;password=;database=accounting;persistsecurityinfo=True";

            using(MySqlConnection con = new MySqlConnection(constr))
            {
                using (MySqlCommand cmd = new MySqlCommand(query))
                {
                   // cmd.Parameters.AddWithValue("@idproduct", product.productId);
                    cmd.Parameters.AddWithValue("@price", product.price);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            return new EmptyResult();
        }

In the view I use a javascript function that allows the user to change the price from a text input like that:

 @foreach (var item in Model)
    {
        <tr>
            <td class="Product Name">
                <span>@item.name</span>
                
            </td>
            <td class="Description">
                <span>@item.description</span>
               
            </td>
            <td class="Price">
                <span>@item.price</span>
                <input type="text" value="@item.price" style="display:none; width: 50px;" />
            </td>

            <td>
                <a class="Edit" href="javascript:;">Edit</a>
                <a class="Update" href="javascript:;" style="display:none">Update</a>
                <a class="Cancel" href="javascript:;" style="display:none">Cancel</a>
                
            </td>
        </tr>

    }
</table>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
    <script type="text/javascript">
        //Edit event handler.
        $("body").on("click", "#tblProducts .Edit", function () {
            var row = $(this).closest("tr");
            $("td", row).each(function () {
                if ($(this).find("input").length > 0) {
                    $(this).find("input").show();
                    $(this).find("span").hide();
                }
            });
            row.find(".Update").show();
            row.find(".Cancel").show();
            row.find(".Delete").hide();
            $(this).hide();
        });
        //Update event handler.
        $("body").on("click", "#tblProducts .Update", function () {
            var row = $(this).closest("tr");
            $("td", row).each(function () {
                if ($(this).find("input").length > 0) {
                    var span = $(this).find("span");
                    var input = $(this).find("input");
                    span.html(input.val());
                    span.show();
                    input.hide();
                }
            });
            row.find(".Edit").show();
            row.find(".Delete").show();
            row.find(".Cancel").hide();
            $(this).hide();

            var product = {};
            //product.idproduct= row.find(".productId").find("span").html();
            //product.name = row.find(".Product Name").find("span").html();
            product.price = row.find(".Price").find("span").html();
            $.ajax({
                type: "POST",
                url: "/Product/Index",
                data: '{product:'   JSON.stringify(product)   '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json"
            });
        });
</script>

Any suggestion? I have tried to change my query by adding the variable but it didn't help.

CodePudding user response:

This is not valid SQL:

string query = "UPDATE product SET price=@price = "   product.price   " WHERE productId=@idproduct";

Your UpdatePrice action is likely throwing an unhandled exception (which would tell you where the problem is). You should ensure that you're logging those failures, to be informed of problems in your application.

Change it to:

string query = "UPDATE product SET price=@price WHERE productId=@idproduct";

Then make sure you're adding both @price and @idproduct to your command's parameters collection.

CodePudding user response:

Since he already fixed your sql code i wanna answer your question regarding that the action is never invoked. I could have overread it but i think you are not calling the UpdatePrice ActionResult in your code.

  • Related