Ramco Ramco

Ramco Ramco

  • 469
  • 2.8k
  • 394.3k

Saving Data in Master & Lines simultaneouly

Sep 9 2022 7:59 AM

Hi

   In below code i am saving Data in Master table first & then Lines table. I want that Data should get saved simulataneouly in both tables.

If error is encountered at any stage then Data should RollBack in both tables

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string Id = (sender as Button).ID;
            try
            {
                BALTrainingHeader bALTrainingHeader = new BALTrainingHeader();

                DateTime? dtFrom = null;
                DateTime? dtTo = null;
                if (hdfID.Value == "0")
                {
                    if (txtdtFrom.Text.Trim() != "")
                    {
                        DateTime PMDate2 = DateTime.ParseExact(txtdtFrom.Text, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
                        dtFrom = Convert.ToDateTime(PMDate2.ToString("yyyy-MM-dd"));
                    }

                    if (txtdtTo.Text.Trim() != "")
                    {
                        DateTime PMDate2 = DateTime.ParseExact(txtdtTo.Text, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
                        dtTo = Convert.ToDateTime(PMDate2.ToString("yyyy-MM-dd"));
                    }

                    if (dtTo < dtFrom)
                    {
                        ShowMessage("Error", "Date To should be greather than or equal to Date From, Please Try Again", "error");
                        return;
                    }

                    DateTime StartTime = DateTime.Now;
                    String frTime = ddlTimeFrom.SelectedItem.Value;
                    var frts = TimeSpan.Parse(frTime);
                    String toTime = ddlTimeTo.SelectedItem.Value;
                    var tots = TimeSpan.Parse(toTime);

                    if (dtTo == dtFrom)
                    {
                        if (tots <= frts)
                        {
                            ShowMessage("Error", "To Time should be greather than From Time", "error");
                            return;
                        }
                    }

                    TrainingHeader Result = bALTrainingHeader.GetRecordDetails(Convert.ToInt32(hdfTrainingId.Value));
                    Result.TrainingTypeID = Convert.ToInt32(ddlTrainingType.SelectedItem.Value);
                    Result.TrainingModuleID = Convert.ToInt32(ddlTrainingModule.SelectedItem.Value);
                    Result.FromDate = dtFrom;
                    Result.FromTime = frts;
                    Result.ToDate = dtTo;
                    Result.ToTime = tots;
                    
                    Result.UserRemarks = txtRemarks.Text;
                    if (Id == "btnSubmit")
                    {
                        Result.TrainingStatus = "Open";
                    }
                    else
                    {
                        Result.TrainingStatus = "Drafted";
                    }
                    Result.Deleted = false;
                    Result.ModifiedOn = Utility.CurrentDateTime();

                    int Res = bALTrainingHeader.UpdateRecord(Result);
                    if (Res != 0)
                    {
                        bALTrainingHeader.DeleteRecordParticipants(Convert.ToInt32(hdfTrainingId.Value));
                        if (lstUsers.Items.Count > 0)
                        {
                            for (int i = 0; i < lstUsers.Items.Count; i++)
                            {
                                if (lstUsers.Items[i].Selected)
                                {
                                    TrainingParticipant objLine = new TrainingParticipant();
                                    objLine.TrainingID = Convert.ToInt32(hdfTrainingId.Value);
                                    objLine.ParticipantID = Convert.ToInt32(lstUsers.Items[i].Value);
                                    objLine.TrainingParticipantType = ddlWhom.SelectedItem.Value;
                                    objLine.Deleted = false;
                                    objLine.EntryDate = Utility.CurrentDateTime();
                                    int objResult = bALTrainingHeader.AddRecordParticipant(objLine);
                                }
                            }
                            ShowMessageWithRedirect("", "Record Saved Successfully", "success", "DraftedTrainingList");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utility.SaveErrorLog(ex.Message, System.IO.Path.GetFileName(Request.Path), System.Reflection.MethodBase.GetCurrentMethod().Name, hdfEmpNumber.Value);
                ShowMessage("Oops...", ex.Message, "error");
            }
        }

 

Thanks


Answers (1)