Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Team

I have a controller to edit a value from the form, but i keep getting this error on my controller that my property is nullable to int. How do i fix this issue? I try to put int? property name to fix this

What I have tried:

&
ASP.NET
lt;pre>// POST: OrderHeader/Edit
        [HttpPost]
        public ActionResult Edit(OrderLine orderLine)
        {
            if (ModelState.IsValid)
            {
                // Update the order line in the database
                UpdateOrderLine(orderLine);

                return RedirectToAction("Index");
            }

            return View(orderLine);
        }

        private void UpdateOrderLine(OrderLine orderLine)
        {
            string connectionString = "Data Source=DESKTOP-C5QD55P\\SQLEXPRESS;Initial Catalog=SalesOrder;Integrated Security=True";

            using(SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string query = "UPDATE OrderLine SET ProductCode = @ProductCode, ProductType = @ProductType, CostPrice = @CostPrice, SalesPrice = @SalesPrice, Quantity = @Quantity WHERE LineNumber = @LineNumber";
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@LineNumber", orderLine.LineNumber);
                command.Parameters.AddWithValue("@ProductCode", orderLine.ProductCode);
                command.Parameters.AddWithValue("@ProductType", orderLine.ProductType);
                command.Parameters.AddWithValue("@CostPrice", orderLine.CostPrice);
                command.Parameters.AddWithValue("@SalesPrice", orderLine.SalesPrice);
                command.Parameters.AddWithValue("@Quantity", orderLine.Quantity);

                command.ExecuteNonQuery();
            }
        }

        // Helper method to retrieve an order line by line number
        private OrderLine GetOrderLineByLineNumber(int lineNumber)
        {
            string connectionString = "Data Source=DESKTOP-C5QD55P\\SQLEXPRESS;Initial Catalog=SalesOrder;Integrated Security=True";

            OrderLine orderLine = null;
            using(SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT * FROM OrderLine WHERE LineNumber = @LineNumber";
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@LineNumber", lineNumber);

                SqlDataReader reader = command.ExecuteReader();
                if(reader.Read())
                {
                    orderLine = new OrderLine
                    {
                        LineNumber = (int)reader["LineNumber"],
                        ProductCode = reader["ProductCode"].ToString(),
                        ProductType = reader["ProductType"].ToString(),
                        CostPrice = (decimal)reader["CostPrice"],
                        SalesPrice = (decimal) reader["SalesPrice"],
                        Quantity = (int) reader["Quantity"]
                    };
                }
                reader.Close();
            }

            return orderLine; // Return the retrieved order line or null if not found
        }


// model
public class OrderLine
    {
        
        public int OrderId { get; set; }
        public int LineNumber { get; set; }
        public string ProductCode { get; set; }
        public string ProductType { get; set; }
        public decimal CostPrice { get; set; }
        public decimal SalesPrice { get; set; }
        public int Quantity { get; set; }
        public List<OrderLine> OrderLines { get; set; }
    }


// view for edit
@model OrderApplicationXML.Models.OrderLine

@using (Html.BeginForm("Edit", "OrderHeader", FormMethod.Post))
{
    @Html.HiddenFor(model => model.LineNumber)

    <div class="form-group">
        @Html.LabelFor(model => model.ProductCode)
        @Html.TextBoxFor(model => model.ProductCode, new { @class = "form-control" })
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ProductType)
        @Html.TextBoxFor(model => model.ProductType, new { @class = "form-control" })
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CostPrice)
        @Html.TextBoxFor(model => model.CostPrice, new { @class = "form-control" })
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.SalesPrice)
        @Html.TextBoxFor(model => model.SalesPrice, new { @class = "form-control" })
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Quantity)
        @Html.TextBoxFor(model => model.Quantity, new { @class = "form-control" })
    </div>

    <button type="submit" class="btn btn-primary">Update</button>
}


// routing config
routes.MapRoute(
                       name: "OrderHeaderEdit",
                       url: "OrderHeader/Edit/{LineNumber}",
                       defaults: new { controller = "OrderHeader", action = "Edit" }
                   );
Posted
Updated 7-Jun-23 0:17am
v2
Comments
Graeme_Grant 7-Jun-23 5:41am    
int? allows null - ie: a nullable type. You only have 2 int fields and both, IMHO, shoulkd not be null. Have you set a breakpoint and checked out why?
Richard Deeming 7-Jun-23 5:45am    
Your view / form is most likely missing a hidden input for the required LineNumber property. But since we can't see that view, that's just a guess.
Gcobani Mkontwana 7-Jun-23 6:06am    
@Richard Deeming, i have posted view and routeconfig.

1 solution

Your OrderId is not being posted back. Need to add a hidden field for it. You can check by setting a breakpoint in the public ActionResult Edit(OrderLine orderLine) method.
 
Share this answer
 
Comments
Gcobani Mkontwana 7-Jun-23 6:30am    
@Graeme_Grant i did that but i am still getting an error. I placed a breakpoint it never went to the method at all during runtime
Graeme_Grant 7-Jun-23 13:40pm    
If the breakpoint was set correctly, it should be hit.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900