I have a stored procedure with 13 parameters in a SQL Server database. And in my C# application, I need to insert data to that stored procedure and I am able to insert 1 value at a time but I need to be able to insert multiple values like 5 or 10 or more. I have 5 arrays that will have many values to insert to that stored procedure but is not inserting if the arrays have more than 1 value, I am thinking my loop is not done correctly.
Please take a look below.
Class that will create a method for the stored procedure
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BarcodeReceivingApp.Persistence;
using BarcodeReceivingApp.Persistence.Repositories;
namespace BarcodeReceivingApp.Functionality
{
public class StoredProcedureInsert
{
private readonly BarcodeReceivingFootPrintDbContext _barcodeReceivingFootPrintDbContext = new BarcodeReceivingFootPrintDbContext();
public void CallManualBlindBarcodeParsingEventRequestFootPrintProcedure(decimal actualPackagedAmount, int actualPackagedPackId, string lotLookupCode,
int warehouseId, int materialId, string vendorLotLookupCode, DateTime vendorLotManufactureDate,
DateTime vendorLotExpirationDate, int shipmentId, decimal netWeight,
decimal grossWeight, string serialLookupCode, string licensePlateLookupCode)
{
_barcodeReceivingFootPrintDbContext.Database
.ExecuteSqlCommand("EXEC noram_reporting.ManualBlindBarcodeParsingEventRequest " +
"@ActualPackagedAmount, @ActualPackagedPackId, @LotLookupCode, @WarehouseId, @MaterialId, @VendorLotLookupCode," +
"@VendorLotManufactureDate, @VendorLotExpirationDate, @ShipmentId, @netWeight, @grossWeight, @serialLookupCode, @licensePlateLookupCode",
new SqlParameter("@ActualPackagedAmount", actualPackagedAmount),
new SqlParameter("@ActualPackagedPackId", actualPackagedPackId),
new SqlParameter("@LotLookupCode", lotLookupCode),
new SqlParameter("@WarehouseId", warehouseId),
new SqlParameter("@MaterialId", materialId),
new SqlParameter("@VendorLotLookupCode", vendorLotLookupCode),
new SqlParameter("@VendorLotManufactureDate", vendorLotManufactureDate),
new SqlParameter("@VendorLotExpirationDate", vendorLotExpirationDate),
new SqlParameter("@ShipmentId", shipmentId),
new SqlParameter("@netWeight", netWeight),
new SqlParameter("@grossWeight", grossWeight),
new SqlParameter("@serialLookupCode", serialLookupCode),
new SqlParameter("@licensePlateLookupCode", licensePlateLookupCode)
);
}
}
}
Here then I am calling that method to insert to each parameter
private void SendStoredProcedureDataToFootPrint()
{
var lotList = _connection.ParseLot();
var netWeightList = _connection.ParseNetWeight();
var grossWeightList = _connection.ParseGrossWeight();
var serialNumberList = _connection.ParseSerialNumber();
var material = _unitOfWork.Shipments.GetLastShipmentMaterialEntry();
var scanCounts = _connection.CountReceivingBarcodeEntries();
var packagingId = _unitOfWork.Materials.GetPackagingId();
var warehouse = _unitOfWork.Warehouses.GetWarehouseIdQuery();
var shipment = _unitOfWork.Shipments.GetLastShipmentIdEntry();
var licensePlate = _unitOfWork.LicensePlates.GetLastCreatedLicensePlate();
try
{
for (var i = 0; i < _connection.GetBarcodeList().Count ; i++)
{
_storedProcedureInsert.CallManualBlindBarcodeParsingEventRequestFootPrintProcedure(scanCounts, packagingId, lotList[i], warehouseId: warehouse, materialId: 5785,
vendorLotLookupCode: lotList[i], vendorLotManufactureDate: DateTime.Now,
vendorLotExpirationDate: DateTime.Now, shipmentId: shipment,
netWeight: Convert.ToDecimal(netWeightList[i]) / 100,
grossWeight: Convert.ToDecimal(grossWeightList[i]) / 100,
serialLookupCode: serialNumberList[i], licensePlateLookupCode: licensePlate);
}
}
catch (Exception exception)
{
MetroMessageBox.Show(null, exception.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
throw;
}
}
So, like I said this works if I insert 1 data for each parameter but If the lotlist, netweightlist, grossweightlist and serialnumberlist arrays have more than 1 data it will not send to the stored procedure.
So the goal is to insert data it doesn't matter have many records I need to insert it could be one or many at one time.
Could not find a good solution on this in other questions like stackoverflow or google.