Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a reference table that when a user is created it automatically, by Trigger, puts a record with the userId. When a Customer adds Company info I would like to update that record with CustomerId. Below where (private readonly int CustomerId) is this has a message that reads:
Field 'CustomerController.CustomerId' is never assigned to, and will always have its default value 0

So perhaps it is updating the record with 0. I cannot tell because on record creation with the trigger I put a zero in the columns as I have them set to not null.
Thank you for your help.

What I have tried:

Controller:
C#
private readonly int CustomerId;
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateProfile([Bind(Include = "CustomerId,CustomerName")] CustomerNames customerNames)
        {
            if (ModelState.IsValid)
            {
                db.CustomerNames.Add(customerNames);
                var user = User.Identity.GetUserId();
                var companyId = (from p in db.UserJoin where p.UserId == user select p).First();
                companyId.CustomerId = CustomerId;
                db.SaveChanges();
                return RedirectToAction("AddBillingAddress", "BillingAddresses");
            }

            return View(customerNames);
        }



Model:

C#
public class UsersToAddresses
{
    [Key]
    public string UserId { get; set; }
    public int BillToId { get; set; }
    public int ShipToId { get; set; }
    public int CustomerId { get; set; }
}
Posted
Updated 30-Oct-18 7:02am

1 solution

The compiler is correct that your code never explicitly assigns a value to CustomerId. Assuming you have correctly configured your binding so that it is assigned a value (I don't think you have), you can make the warning go away with:

private readonly int CustomerId = 0;
 
Share this answer
 
Comments
Member 13812021 30-Oct-18 14:08pm    
Yvan, The CustomerId is the Key to the Customer table and gets auto incremented on adding. So maybe I should do something like this?
private readonly int CustomerId = CustomerId. I will play with it and see what I come up with. If you know a better way that would be great.
Member 13812021 30-Oct-18 15:38pm    
That did not work. putting the code you have still has the message like before. I have found that you cannot get the CustomerId until it is saved. However I am still coming up with a 0 for CustomerId even when with breakpoints the CustomerId is what it is on the line of code to add. I just need to be able to update a record with the same userId with the CustomerId.
Yvan Rodrigues 30-Oct-18 15:59pm    
But what/where are you expecting CustomerId to get assigned a value. What are you trying to do here:
[Bind(Include = "CustomerId,CustomerName")]
Member 13812021 30-Oct-18 21:54pm    
I have decided to just run a stored procedure that inserts the CustomerName and gets the Id and then updates the CustomerId in the other table. Seems to be cleaner and keeps code out of the controller.

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