2
Answers

how to speed up data insertion in multiple table in sql

Sajid Hussain

Sajid Hussain

9y
434
1
i have 4 tables in sql table ,from different fields in a form like textboxes,Checkboxes,dynamic created
textbox, data is inserted properly,but i want to speedup data insertion
because data for that forms are in bulk,following is code for data insertion,
 
protected void btnSubmit_Click(object sender, EventArgs e)    
   {        
   try        
   {             
    Ticket tk = new Ticket();  
    tk.Sector_Id = SafeConvert.ToInt32(ddlSelectSector.SelectedValue);
    tk.Address = txtaddress.Text;
    tk.Book_ComputerNo = SafeConvert.ToInt64(txtcomputerno.Text);
    tk.Document_Id = SafeConvert.ToByte(ddldocument.SelectedValue); 
    tk.DueDate = SafeConvert.ToDateTime(txtDuedate.Text);
    tk.Employee_Id = ddlRollNo.SelectedValue;
    tk.OffenceDate = SafeConvert.ToDateTime(txtoffencedate.Text); 
    tk.RegistrationNo = txtRegistration.Text;
    tk.T_Number = SafeConvert.ToInt64(txttno.Text); 
    tk.TicketNo = SafeConvert.ToByte(txtTicketNo.Text);
    tk.Vehicle_Impound_Id = SafeConvert.ToByte(ddlImpoundVehicle.SelectedValue); 
   tk.Status = false
    tk.IsJMC = false;
    tk.IsPaid = false
    tk.Year = SafeConvert.ToByte(DateTime.Now.ToString("yy"));
    tk.TotalFine = 2000
    tk.OffenderName = txtoffenderno.Text;  
   _service.InsertTicket(tk); 
   var lastrecord = _service.GetAllTicket().LastOrDefault();
    Int64 tkid = lastrecord.ID; 
    foreach (ListItem listItem in Chkboxlst.Items)
      if (listItem.Selected)  
       {
        TicketCrime tkcrime = new TicketCrime();
        tkcrime.Ticket_Id = tkid; 
       var chkcrimeid = listItem.Value;
       int chkcrime = SafeConvert.ToInt16(chkcrimeid);
       tkcrime.OtherPunishment_Id = chkcrime;
        tkcrime.Status = true
         _service.InsertTicketCrime(tkcrime); 
              if (ViewState["CurrentTable"] != null
                    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                   DataRow drCurrentRow = null;   
                if (dtCurrentTable.Rows.Count > 0
                      drCurrentRow = dtCurrentTable.NewRow(); 
                      drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;  
                       //add new row to DataTable
                       dtCurrentTable.Rows.Add(drCurrentRow); 
                      //Store the current data to ViewState for future reference 
                       ViewState["CurrentTable"] = dtCurrentTable; 
                        for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++) 
                      {  
                           //extract the TextBox values
                            var box1 = ((TextBox)Gridview1.Rows[i].Cells[1].FindControl("txtCode")).Text; 
                          var box2 = ((TextBox)Gridview1.Rows[i].Cells[2].FindControl("txtFine")).Text; 
                          int box1value = SafeConvert.ToInt16(box1);  
                         int box2value = SafeConvert.ToInt16(box2); 
                            TicketDetails tkdetail = new TicketDetails(); 
                         // var chkradio = rblQualification.SelectedValue; 
                          //byte chkRadio = SafeConvert.ToByte(chkradio); 
                          var vehiclecategory = _service.GetAllVehicleCategory().Where(x => x.ID == SafeConvert.ToByte(rblQualification.SelectedValue)).FirstOrDefault();
                           var vehicletype = vehiclecategory.VehicleType_Id;
                           var vehiclepnalty = _service.GetAllVehiclePenaltiesFee().Where(x => x.VehicleTypeID == vehicletype).FirstOrDefault();                                 var fee = _service.GetAllVehiclePenaltiesFee().Where(x => x.VehiclePenaltyTypeID == box1value && x.VehicleTypeID == vehicletype).FirstOrDefault();
                           tkdetail.Ticket_Id = tkid;
                           tkdetail.VehicleType_Id = vehicletype;
                           tkdetail.VehiclePenaltyType_Id = vehiclepnalty.VehiclePenaltyTypeID; 
                          tkdetail.VehiclePenaltiesFee_Id = fee.ID;
                           tkdetail.Status = true;
                           tkdetail.VehicleCategory_Id = vehiclecategory.ID;
                           _service.InsertTicketDetails(tkdetail);
                           _queryStatus = tkdetail.ID > 0;
                       }   
                }          
     }      
     }
Answers (2)