Share via

Cannot insert the value NULL into column 'invID',

Analyst_SQL 3,576 Reputation points
2023-07-18T14:40:13.1466667+00:00

I am inserting value into database from view to controller ,then error is coming that null value can not be insert into column of InvID, while i have a value in InvID in Label.

as per below solution https://learn.microsoft.com/en-us/answers/questions/1332274/how-to-execute-store-procedure-in-net-6-core

View

@model SalesInvoiceMasterDetail.Models.InvoiceModelView

@{

	ViewData["Title"] = "Create Invoice";
}

<div class="row">
	<div   class="col-md-4">

		<form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="InvId" class="control-label"></label>
                <label asp-for="InvId" class="control-label">@Model.InvId</label>
                <span asp-validation-for="InvId" class="text-danger"></span>
            </div>

            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
            </form>
	</div>



</div>



@section Scripts {
    @{
        await Html.RenderPartialAsync("_ValidationScriptsPartial");
    }
}

Controllor

    public IActionResult Create()
        {
            InvoiceModelView invoiceMV = new InvoiceModelView();


            int invID = _context.Database.SqlQuery<int>($"EXEC [dbo].SP_GETInvoiceID").ToList<int>()[0];
            invoiceMV.InvId = invID;

            return View(invoiceMV);


        }

        [HttpPost]
        [ValidateAntiForgeryToken]

        public IActionResult Create (InvoiceModelView invoicemv)
        {
            if (ModelState.IsValid)
            {
                var invoicemodelV = new SaleInvoiceMaster()
                {
                    InvId = invoicemv.InvId


                };

                _context.SaleInvoiceMasters.Add(invoicemodelV);
                _context.SaveChanges();
            }


            else
            {

                return View(invoicemv);
            }


            return View(invoicemv);
        }

Developer technologies | ASP.NET Core | Other

Answer accepted by question author

AgaveJoe 31,361 Reputation points
2023-07-18T14:58:44.7933333+00:00

InvId is null because there are no inputs except the submit button. Try a hidden field if InvId is not user updatable.

<input type="hidden" asp-for="InvId" />

That should get you past the null InvId but you are also missing the other InvoiceModelView inputs.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,086 Reputation points
    2023-07-18T15:14:31.9733333+00:00

    It looks like InvId is a key field. If stored in a hidden field it should be encrypted, or you site is easily hackable.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.