Код: Выделить всё
using AfterFix.Pages.Clients;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
namespace AfterFix.Pages.Sales
{
public class EditModel : PageModel
{
private readonly string _connectionString = "Data Source=DESKTOP-9CABSPL\\SQLEXPRESS;Initial Catalog=mystore;Integrated Security=True;MultipleActiveResultSets=true";
public SalesInfo salesInfo = new SalesInfo();
public string errorMessage = "";
public string SuccessMessage = "";
public List InvoiceItems { get; set; } = new List();
public async Task OnGet(int InvNo)
{
try
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
await connection.OpenAsync();
string invoiceQuery = "SELECT id, ClientName, InvDate, InvNo, SubTotal FROM SalesInv WHERE InvNo = @InvNo;";
using (SqlCommand invoiceCommand = new SqlCommand(invoiceQuery, connection))
{
invoiceCommand.Parameters.AddWithValue("@InvNo", InvNo);
using (SqlDataReader invoiceReader = await invoiceCommand.ExecuteReaderAsync())
{
if (invoiceReader.Read())
{
salesInfo.id = "" + invoiceReader.GetInt32(0);
salesInfo.ClientName = invoiceReader.GetString(1);
salesInfo.InvDate = invoiceReader.GetDateTime(2).ToString("yyyy-MM-dd HH:mm:ss");
salesInfo.InvNo = "" + invoiceReader.GetInt32(3);
salesInfo.SubTotal = invoiceReader.GetDecimal(4);
}
}
}
string query = "SELECT ItemName, ItemQuantity, Price, InvNo FROM Titem WHERE InvNo = @InvNo";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@InvNo", InvNo);
using (SqlDataReader reader = await command.ExecuteReaderAsync())
{
while (reader.Read())
{
InvoiceItems.Add(new InvoiceItem
{
ItemName = reader["ItemName"].ToString(),
ItemQuantity = Convert.ToInt32(reader["ItemQuantity"]),
Price = Convert.ToDecimal(reader["Price"]),
InvNo = Convert.ToInt32(reader["InvNo"])
});
}
}
}
}
return Page();
}
catch (Exception ex)
{
errorMessage = ex.Message;
return Page();
}
}
public IActionResult OnPost()
{
try
{
using (SqlConnection innerConnection = new SqlConnection(_connectionString))
{
innerConnection.Open();
string itemSql = "INSERT INTO Titem (ItemName, ItemQuantity, Price) VALUES (@ItemName, @ItemQuantity, @Price);";
using (SqlCommand itemCommand = new SqlCommand(itemSql, innerConnection))
{
// Clear parameters once outside the loop
itemCommand.Parameters.Clear();
// Add parameters outside the loop (to be reused in each iteration)
itemCommand.Parameters.Add("@ItemName", SqlDbType.NVarChar);
itemCommand.Parameters.Add("@ItemQuantity", SqlDbType.Int);
itemCommand.Parameters.Add("@Price", SqlDbType.Decimal);
foreach (var item in InvoiceItems)
{
// Validate quantity and price format
if (!IsNumeric(item.ItemQuantity.ToString()) || !IsNumeric(item.Price.ToString()))
{
errorMessage = "Invalid quantity or price format.";
return Page();
}
// Set parameter values in each iteration
itemCommand.Parameters["@ItemName"].Value = item.ItemName;
itemCommand.Parameters["@ItemQuantity"].Value = item.ItemQuantity;
itemCommand.Parameters["@Price"].Value = item.Price;
itemCommand.ExecuteNonQuery();
}
}
}
SuccessMessage = "تم حفظ البيانات بنجاح";
return RedirectToPage("/Sales/Index");
}
catch (Exception ex)
{
errorMessage = $"An error occurred: {ex.Message}";
return Page();
}
}
// Helper method to validate numeric strings
private bool IsNumeric(string value)
{
return decimal.TryParse(value, out _);
}
// Unrelated to error handling
public class InvoiceItem
{
public string ItemName { get; set; }
public int ItemQuantity { get; set; }
public decimal Price { get; set; }
public int InvNo { get; set; }
}
}
}
Код: Выделить всё
@page
@model AfterFix.Pages.Sales.EditModel
@{
ViewData["Title"] = "Edit Invoice";
}
Edit Invoice
@if (!string.IsNullOrEmpty(Model.errorMessage))
{
@Model.errorMessage
}
@if (!string.IsNullOrEmpty(Model.SuccessMessage))
{
@Model.SuccessMessage
}
Invoice Items
Item Name
Quantity
Price
@foreach (var item in Model.InvoiceItems)
{
@item.ItemName
@item.ItemQuantity
@item.Price
}
Save
Подробнее здесь: https://stackoverflow.com/questions/785 ... azor-pages