Click here to Skip to main content
15,888,104 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void btnUpload_Click(object sender, EventArgs e)
       {
           ResetStatus();

           for (int i = 1; i <= 10; i++)
           {
               string file_control = "FileUpload" + i.ToString();
               string link_button_control = "LinkButton" + i.ToString();
               FileUpload fileUpload = (FileUpload)this.Master.FindControl("MainContent").FindControl(file_control);
               LinkButton linkButton = (LinkButton)this.Master.FindControl("MainContent").FindControl(link_button_control);
               string label_file_name_control = "LabelFileName" + i.ToString();
               Label label_file_name = (Label)this.Master.FindControl("MainContent").FindControl(label_file_name_control);

               if (fileUpload.PostedFile.FileName != "")
               {
                   string f_type = Functions.CheckFileType(fileUpload.PostedFile.FileName);
                   string file_name = fileUpload.PostedFile.FileName;
                   if (f_type == "xls" && f_type != "xlsx")
                   {
                       error += "\nFile number " + i.ToString() + " " + file_name + " should be in Excel format!";
                       linkButton.Visible = true;
                       linkButton.Text = "Error found, click to show error";
                   }
                   else
                   {
                       DateTime rightnow = DateTime.Now;
                       string user_name = SharedFunctionV3.UserInfo.GetUserInfofromAD(Session["UserID"].ToString()).DisplayName;
                       string new_file_name = Session["UserID"].ToString() + rightnow.ToString("yyyyMMddhhmmss") + "ForecastDataUpload" + i.ToString() + ".xls";
                       string f_name = Server.MapPath(".") + "\\upload\\" + new_file_name;
                       fileUpload.PostedFile.SaveAs(f_name);
                       label_file_name.Text = f_name;
                       if (CheckFile(f_name, i) == 0)//edited
                       {
                           linkButton.Visible = true;
                           linkButton.Text = "Error found, click to show error";
                       }
                       else
                       {
                           UploadRecord(f_name, i);
                       }
                   }
               }
           }

           tbError.Text = error;
       }


What I have tried:

Suppose if the loop takes 10 rounds how can i make one call for UploadRecord function for all the 10 times so that it will reduce the time it takes to upload bulk load
Posted
Updated 4-Dec-18 22:32pm
Comments
Richard Deeming 7-Dec-18 11:59am    
if (f_type == "xls" && f_type != "xlsx")

Can you think of a string value which would be equal to both "xls" and "xlsx" at the same time?

Me neither. Either your second check on that line is not required, or the first == should be != instead.

string user_name = SharedFunctionV3.UserInfo.GetUserInfofromAD(Session["UserID"].ToString()).DisplayName;

Accessing AD is particularly slow, and that line doesn't rely on the loop variable at all. Try moving it outside of the for loop.

1 solution

Try using the parallel version of "for". If you gain performance from it will depends on a number of things, but it's probably the easiest thing to try. Google "c# parallel for" for details and examples.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900