Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more: , +
paid and remain Amount always saved null in database although i get value is not null by javascript

What I have tried:

my storedProcedure
SQL
USE [OnlineMarket]
GO
/****** Object:  StoredProcedure [dbo].[InsertintoSalesInvoice]    Script Date: 10/07/2023 09:02:32 م ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[InsertintoSalesInvoice]

@InvoiceNo int,
@ItemId decimal(10,2),@SalePrice decimal(10,2),
@Qty decimal(10,2),
@Discount decimal(10,2),
--@amountAfterDiscount decimal(10,2),
@Paid decimal(10,2),
@Remain decimal(10,2),
@Total decimal(10,2),
@Paiddate date,
@CustomerId int

AS
BEGIN

declare @Btotal dec
declare @count dec
declare @countSale dec
declare @Bdate date
declare @pd dec
declare @AmountAfterdiscount dec
select @Bdate=getdate()
select @countSale=count(*) from TbInvoiceSales where 
InvoiceDate=@Bdate and
InvoiceNo=@InvoiceNo and
ItemId=@ItemId

select @pd =(Isnull(@Discount,1)*((@Qty*@SalePrice))-(@Qty*@SalePrice))-@Paid
select @AmountAfterdiscount=(@Qty*@SalePrice)-(Isnull(@Discount/100,1))*((@Qty*@SalePrice))
 
if @countSale=0
	INSERT INTO [dbo].[TbInvoiceSales]
		([InvoiceDate]
		,[InvoiceNo]
		,[ItemId]
		,[SalePrice]
		,[Qty]
		,[Discount]
		,[total]
		,[Paid]
		,[Remain]
		,[Paiddate])

	values
		(@Bdate,@InvoiceNo,@ItemId,@SalePrice,@Qty,Isnull(@Discount,1),@AmountAfterdiscount,
		@Paid,@Remain,
		@Paiddate)

 else
	update TbInvoiceSales set Qty=Qty+@Qty , total=total + (@Qty*@SalePrice)
	where
		InvoiceDate=@Bdate and
		InvoiceNo=@InvoiceNo and
		ItemId=@ItemId

 select @Btotal=ISNULL(sum(total),0) from TbInvoiceSales where InvoiceDate=@Bdate and InvoiceNo=@InvoiceNo;
 select @count=count(*) from TbInvoiceSalesDetails where TinvoiceSaleDetailsDT=@Bdate and InvoiceNo=@InvoiceNo;

if @count=0
	INSERT INTO [dbo].[TbInvoiceSalesDetails]
		([TinvoiceSaleDetailsDT]
		,[InvoiceNo]
		,[TotalInvoice]
		,[CustomerId])
	values
	(
		@Bdate,@InvoiceNo,@Btotal,@CustomerId
	)
else
	update TbInvoiceSalesDetails set TotalInvoice=@Btotal where TinvoiceSaleDetailsDT=@Bdate and
		InvoiceNo=@InvoiceNo

update TbItem set Qty=Qty-@Qty  where Id=@ItemId

END

my view
ASP.NET
@page
@model Food.Pages.Sales.IndexModel
@{
}

<h1>
    عمل فاتورة
</h1>

<div class="mx-2 mb-3 row">
	<div class="col-1">
		<div>
			
				code
			
		</div>
	</div>
	<div class="col-3">
		
	</div>

	<div class="col-1">
		<div>
			discount
		</div>
	</div>
	<div class="col-1">
	</div>
	<div class="col-1">
		<div>
			quantity
		</div>
	</div>
	<div class="col-1">
	</div>
</div>
<div class="d-flex justify-content-around border p-2">
	<div>
		Date
		@Model.InvoiceDate.ToString("dd/MM/yyyy")
	</div>
	<div>
		Number
	</div>
	<div>
		total
	</div>
	<div>
		Paid
	</div>
	<div>
		Remain
	</div>
</div>

<div class="border mb-3 row">
    <div class="col-3">
    </div>
    <div class="col-2">
    </div>
</div>

<div class="m-2 border" style="height: 450px">
	@foreach (var s in Model.lstInvoiceSale)
	{
                                
	}

	<table class="table table-bordered text-center">
		<thead class="bg-primary text-light">
			<tr>
				<th>Name</th>
				<th>quantity</th>
				<th>price</th>
				<th>discount</th>
				<th>total</th>
				<th>.....</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>@s.Item.ItemName</td>
				<td>@s.Qty</td>
				<td>@s.Item.SalePrice</td>
				<td>@s.Discount</td>
				<td></td>
				<td>
					<a class="btn btn btn-outline-danger">
						Delete
					</a>
				</td>
			</tr>
		</tbody>
	</table>
</div>

<div>
    <a class="btn btn-primary">New Item   AddNew</a>
</div>

@section Scripts {
	document.getElementById("ItemId").focus();

	var element = document.getElementById("InsertForm");
	element.addEventListener("keypress", function (event) {
		if (event.key === "Enter") {
			document.getElementById("iQuantity").focus();
			event.preventDefault();
		}
	});

	var Iquantity = document.getElementById("iQuantity");
	Iquantity.addEventListener("keypress", function (event) {
		if (event.key === "Enter") {
			document.getElementById("InsertForm").submit();
			event.preventDefault();
		}
	});

	var paid = document.getElementById("Paid");
	paid.addEventListener("keypress", function (event) {
		if (event.key === "Enter") {
			var total = parseInt(document.getElementById("amount").value);
			var val2 = parseInt(document.getElementById("Paid").value);
	   
			if (!total) { total = 0; }
			if (!val2) { val2 = 0; }

			var ansD = document.getElementById("remain");
			ansD.value = val2 - total;
		}
	});
}

my code
C#
using Food.Models;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System.Data;

namespace Food.Pages.Sales
{
    public class IndexModel : PageModel
    {
        OnlineMarketContext db = new OnlineMarketContext();
        
        [BindProperty]
        public List<tbinvoicesale> lstInvoiceSale { get; set; }

        [BindProperty]
        public TbInvoiceSale InvoiceSale { get; set; }

        [BindProperty]
        public TbItem itemO { get; set; }
     
        [BindProperty]
        public int ItemId { get; set; }

        [BindProperty]
        public DateTime InvoiceDate { get; set; }
        [BindProperty]
        public int InvoiceNum { get; set; } = 0;

        [BindProperty]
        public decimal discount { get; set; } 
        [BindProperty]
        public decimal Paid { get; set; }
        [BindProperty]
        public decimal remain { get; set; } 

        [BindProperty]
        public decimal quantity { get; set; }

        [BindProperty]
        public decimal AmountAfterDiscount { get; set; }

        [BindProperty]
        public decimal total { get; set; } = 1;

        [BindProperty]
        public DateTime PaidDate { get; set; } = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));
        public void OnGet()
        {  
            lstInvoiceSale = db.TbInvoiceSales.Include("Item").Where(i => i.InvoiceNo == 0).ToList();
            InvoiceSale = db.TbInvoiceSales.Where(i => i.ItemId == ItemId).FirstOrDefault();
        }

        public void OnPost()
        {
            var Oitem = db.TbItems.Where(i => i.Id == ItemId).FirstOrDefault();
          
            InvoiceDate = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));

            if (Oitem != null)
            {
                if (InvoiceNum == 0)
                {
                    try
                    {
                        InvoiceNum = db.TbInvoiceSales.Where(i => i.InvoiceDate == InvoiceDate).Max(i => i.InvoiceNo) + 1;
                    }
                    catch
                    {
                        InvoiceNum = 1;
                    }
                }
                
				string ConnectionString = "Server=.;Database=OnlineMarket;Trusted_Connection=True;TrustServerCertificate=true;";
                using (SqlConnection con = new SqlConnection(ConnectionString))
                {
                    using (SqlCommand cmd = new SqlCommand("InsertintoSalesInvoice", con))
                    {
                        con.Open();
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@InvoiceNo", InvoiceNum);
                        cmd.Parameters.AddWithValue("@ItemId", ItemId);
                        cmd.Parameters.AddWithValue("@SalePrice", Oitem.SalePrice);
                        cmd.Parameters.AddWithValue("@Qty", quantity);
                        cmd.Parameters.AddWithValue("@Discount", discount);
                   
                        cmd.Parameters.AddWithValue("@Paid",Paid);
                        cmd.Parameters.AddWithValue("@Remain",remain);
                        cmd.Parameters.AddWithValue("@Total", InvoiceSale.Total);
                        cmd.Parameters.AddWithValue("@Paiddate", PaidDate);
                        cmd.Parameters.AddWithValue("@CustomerId", 1);
                        cmd.ExecuteNonQuery();
                    }
                    con.Close();
                }

                /// /
                lstInvoiceSale = db.TbInvoiceSales.Include("Item").Where(i => i.InvoiceNo == InvoiceNum).ToList();
                total = (decimal)db.TbInvoiceSales.Where(i => i.InvoiceNo == InvoiceNum && i.InvoiceDate == InvoiceDate)
               .Sum(i => i.Total);
                remain = Paid - total;
            }
        }
    }
}
Posted
Updated 10-Jul-23 10:33am
v2

1 solution

This is your 5th question posted. At least put some effort in and learn to format your code so we can at least read it. I have done that for you.

If you want help, and for us to read your code, take pride in what you post, it will show. Click on the "Improve question" link and see how it is done. Here is your original posting: original posting[^].

Quote:
paid and remain Amount always saved null in database although i get value is not null by javascript

Have you used the debugger? Have you set break points in your code to see what the values are on the client? When the data hit the server? Do you know what values are being passed to the SqlCommand before passing to the database?

That Stored Procedure is too big to debug if there was a problem. You need to break it into more than one Stored Procedure, each with a single responsibility. Less complexity = less issues.

Dumping your client, server, and SQL code here with little to no explicit explanation, nor any effort shown in debugging your own code, is only showing us that you want us to debug your code. We are not here to do your debugging for you. We do not need to see all of this code. Why do you expect us to want to help you if you can't help yourself?

Here are the steps that I would need to do if I was to debug your code...

Work backwards on your issue

I would set a debug breakpoint on this line in the OnPost() method:
C#
cmd.ExecuteNonQuery();

When the execution stops on this line of code, inspect the values and check if they are correct. If not, then keep working backwards, setting breakpoints, stepping through the code, and see where the values are either changed or not passed correctly. Then you will know where yourt issue is. Then you can start fixing your problem.

Yes, you can debug JavaScript in the client web browser. Run your project, navigate to the page but do not enter any data. Open the web browser dev tools, go to the Sources tab, find the page file, locate your JavaScript, then set a breakpoint. When the code is hit, you can then debug and check values.

Time for you to roll up your sleeves and work out where the issue is.
 
Share this answer
 
Comments
Andre Oosthuizen 11-Jul-23 4:55am    
Great! +5

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