public async Task <IHttpActionResult> AddIdtIctIn([FromBody] IdtIctInModel value)
 {
     using (TransactionScope scope = new TransactionScope())
         using (var connection = new SqlConnection(sqlConnectionString))
         {
             try
             {
                 var p = new firstForm
                 {
                     IdtIctType      = value.firstForm.IdtIctType,
                     ReferenceNumber = value.firstForm.ReferenceNumber,
                     DateOfEntry     = value.firstForm.DateOfEntry,
                     Status          = value.status,
                 };
                 p.Id = connection.Query <int>(@"insert IdtIcTMaster(IdtIctType,ReferenceNumber,DateOfEntry,Status) values (@IdtIctType,@ReferenceNumber,@DateOfEntry,@Status) select cast(scope_identity() as int)", p).First();
                 foreach (var item in value.depotProdcutValueList)
                 {
                     var data = new depotProductValueModel
                     {
                         productId         = item.productId,
                         depotId           = item.depotId,
                         quantity          = item.quantity,
                         date              = item.date,
                         IdtIctMasterId    = p.Id,
                         AvailableQuantity = item.quantity,
                         AddedOn           = DateTime.Now
                     };
                     var id = connection.Query <int>(@"insert IdtIctDetails(IdtIctMasterId,productId,depotId,quantity,date,AvailableQuantity,AddedOn) values (@IdtIctMasterId,@productId,@depotId,@quantity,@date,@AvailableQuantity,@AddedOn) select cast(scope_identity() as int)", data).First();
                 }
                 scope.Complete();
                 return(Json(new { Message = "Record Inserted Successfully" }));
             }
             catch (Exception)
             {
                 // Not needed any rollback, if you don't call Complete
                 // a rollback is automatic exiting from the using block
                 connection.BeginTransaction().Rollback();
                 return(Json(new { Message = "Error" }));
             }
         }
 }
        public async Task <IHttpActionResult> EditIdtIctOut([FromBody] EditIdtIctOutModel value)
        {
            var Id = 0;

            if (value.depotProdcutValueList.Count > 0)
            {
                Id = Convert.ToInt32(value.depotProdcutValueList[0].IdtIctMasterId);
            }
            else
            {
                Id = Convert.ToInt32(value.unitProductValueList[0].IdtIctMasterId);
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            //   using (TransactionScope scope = new TransactionScope())
            using (var connection = new SqlConnection(sqlConnectionString))
            {
                try
                {
                    connection.Open();

                    var IdtIctExist = connection.Query <int>("Select * from IdtIcTOutMaster where ID = @Id", new { Id = Id }).FirstOrDefault();
                    if (IdtIctExist == null)
                    {
                        throw new ProcessException("Selected Idt/Ict not exists");
                    }
                    else
                    {
                        //var p = new firstForm
                        //{
                        var IdtIctType      = value.firstForm.IdtIctType;
                        var ReferenceNumber = value.firstForm.ReferenceNumber;
                        var DateOfEntry     = value.firstForm.DateOfEntry;
                        var Status          = value.status;


                        //};
                        // string updateQuery = @"UPDATE IdtIcTMaster SET IdtIctType=@IdtIctType,ReferenceNumber=@ReferenceNumber,DateOfEntry=@DateOfEntry,Status=@Status where  Id = @Id";
                        string updateQuery = @"update IdtIctOutMaster SET IdtIctType = @IdtIctType,ReferenceNumber=@ReferenceNumber,DateOfEntry=@DateOfEntry,Status=@Status WHERE Id = @Id";

                        var result = connection.Execute(updateQuery, new
                        {
                            IdtIctType,
                            ReferenceNumber,
                            DateOfEntry,
                            Status,
                            Id
                        });
                        var IdtIctMasterId = Id;
                        // var IdtIctDetails = connection.Query<depotProductValueModel>("Select * from IdtIctDetails where IdtIctMasterId=@IdtIctMasterId", new { IdtIctMasterId = IdtIctMasterId }).ToList();
                        if (value.firstForm.IdtIctType == "LUT")
                        {
                            for (int i = 0; i < value.unitProductValueList.Count; i++)
                            {
                                if (value.unitProductValueList[i].Id != 0)
                                {
                                    var currentRecord = connection.Query <int>("Select * from IdtIctOutDetails where ID = @Id", new { Id = value.unitProductValueList[i].Id }).FirstOrDefault();
                                    //  var isRecordExist
                                    var productId = value.unitProductValueList[i].productId;
                                    var unitId    = value.unitProductValueList[i].unitId;
                                    var quantity  = value.unitProductValueList[i].quantity;
                                    var date      = value.unitProductValueList[i].date;
                                    var DetailsId = value.unitProductValueList[i].Id;
                                    //var IdtIctMasterId = IdtIctMasterId;


                                    //   data.Id = connection.Query<int>(@"UPDATE IdtIctDetails SET IdtIctMasterId=@IdtIctMasterId,productId=@productId,depotId=@depotId,quantity=@quantity,date=@dte) select cast(scope_identity() as int)", p).First();
                                    string updateQueryforDetails = @"UPDATE IdtIctOutDetails SET productId = @productId,unitId=@unitId,quantity=@quantity,date=@date WHERE Id = @DetailsId";
                                    var    result2 = connection.Execute(updateQueryforDetails, new
                                    {
                                        productId,
                                        unitId,
                                        quantity,
                                        date,
                                        DetailsId
                                    });
                                }
                                else
                                {
                                    var data = new unitProductValueModel
                                    {
                                        productId      = value.unitProductValueList[i].productId,
                                        unitId         = value.unitProductValueList[i].unitId,
                                        quantity       = value.unitProductValueList[i].quantity,
                                        date           = value.unitProductValueList[i].date,
                                        IdtIctMasterId = value.unitProductValueList[i].IdtIctMasterId,
                                        depotId        = 0
                                    };
                                    var id = connection.Query <int>(@"insert IdtIctOutDetails(IdtIctMasterId,productId,unitId,quantity,date,depotId) values (@IdtIctMasterId,@productId,@unitId,@quantity,@date,@depotId) select cast(scope_identity() as int)", data).First();
                                }
                            }
                        }
                        else
                        {
                            for (int i = 0; i < value.depotProdcutValueList.Count; i++)
                            {
                                if (value.depotProdcutValueList[i].Id != 0)
                                {
                                    var currentRecord = connection.Query <int>("Select * from IdtIctOutDetails where ID = @Id", new { Id = value.depotProdcutValueList[i].Id }).FirstOrDefault();
                                    //  var isRecordExist
                                    var productId         = value.depotProdcutValueList[i].productId;
                                    var depotId           = value.depotProdcutValueList[i].depotId;
                                    var quantity          = value.depotProdcutValueList[i].quantity;
                                    var date              = value.depotProdcutValueList[i].date;
                                    var DetailsId         = value.depotProdcutValueList[i].Id;
                                    var AvailableQuantity = value.depotProdcutValueList[i].quantity;
                                    var ModifiedOn        = DateTime.Now;
                                    //var IdtIctMasterId = IdtIctMasterId;


                                    //   data.Id = connection.Query<int>(@"UPDATE IdtIctDetails SET IdtIctMasterId=@IdtIctMasterId,productId=@productId,depotId=@depotId,quantity=@quantity,date=@dte) select cast(scope_identity() as int)", p).First();
                                    string updateQueryforDetails = @"UPDATE IdtIctOutDetails SET productId = @productId,depotId=@depotId,quantity=@quantity,date=@date,AvailableQuantity=@AvailableQuantity,ModifiedOn=@ModifiedOn WHERE Id = @DetailsId";
                                    var    result2 = connection.Execute(updateQueryforDetails, new
                                    {
                                        productId,
                                        depotId,
                                        quantity,
                                        date,
                                        DetailsId,
                                        AvailableQuantity,
                                        ModifiedOn
                                    });
                                }
                                else
                                {
                                    var data = new depotProductValueModel
                                    {
                                        productId      = value.depotProdcutValueList[i].productId,
                                        depotId        = value.depotProdcutValueList[i].depotId,
                                        quantity       = value.depotProdcutValueList[i].quantity,
                                        date           = value.depotProdcutValueList[i].date,
                                        IdtIctMasterId = value.depotProdcutValueList[i].IdtIctMasterId,
                                        AddedOn        = DateTime.Now
                                    };
                                    var id = connection.Query <int>(@"insert IdtIctOutDetails(IdtIctMasterId,productId,depotId,quantity,date,AddedOn) values (@IdtIctMasterId,@productId,@depotId,@quantity,@date,@AddedOn) select cast(scope_identity() as int)", data).First();
                                }
                            }
                        }
                        // scope.Complete();
                        return(Json(new { Message = "Record Updated successfully!" }));
                    }
                }
                catch (Exception)
                {
                    connection.BeginTransaction().Rollback();
                    return(Json(new { Message = "Error" }));
                }
            }
        }