Basit Nisar

Basit Nisar

  • 1.3k
  • 84
  • 11k

how to pass the params to stored procedure

Apr 29 2023 4:05 PM

in my c# code the request payload is:

{"lstContractInvoicePerdays":[{"ContractInvoicePerDayID":"1061","LastModifiedDate":"2023-04-24T04:37:44"},{"ContractInvoicePerDayID":"1135","LastModifiedDate":"2023-04-24T04:37:45"}]}

Let me help to pass the params in stored procedure as existing logic is not helping

try
{
    int? BaseStateProvinceID = null;
    List<StaffCIPDApprovedPutModel> lstContractInvoicePerdays = new List<StaffCIPDApprovedPutModel>();
    lstContractInvoicePerdays = staffCIPDApprovedViewModel.lstContractInvoicePerdays.ToList();
    if (lstContractInvoicePerdays.Count == 1)
    {
    }
    CIPDPushReqMod timeCardPushRequest = new CIPDPushReqMod();
    List <int> ContractInvoicePerDayIDs = staffCIPDApprovedViewModel.lstContractInvoicePerdays.Select(x=> x.ContractInvoicePerDayIDs.Split(',').Select(int.Parse).ToList();
    using (SqlConnection connection = new SqlConnection(db.Database.Connection.ConnectionString))
    {
        try
        {
            using (SqlCommand command = new SqlCommand("spGetCIPDForPushToLawson", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ContractInvoicePerDayIDs", SqlDbType.NVarChar).Value = ContractInvoicePerDayIDs as object ?? DBNull.Value;
                if (connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
                connection.Open();
                using (var reader = command.ExecuteReader())
                {
                    timeCardPushRequest.TimeCardLst = ((IObjectContextAdapter)db).ObjectContext.Translate<CIPDPushReqObj>(reader).ToList();
                    reader.NextResult();
                    timeCardPushRequest.AdditionalComponents = ((IObjectContextAdapter)db).ObjectContext.Translate<CIPDPushReqObj_AdditionalComponent>(reader).ToList();
                }
            }
        }
        catch (Exception)
        {
        }

 


Answers (2)