Hi
I have below code & i want to upload multiple Excel Files
protected void btnUploadSave_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.HasFile)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string RandomName = Utility.CurrentDateTime().ToFileTime().ToString();
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string FolderPath = "~/Upload/ExcelUpload/";
string FilePath = Server.MapPath(FolderPath + RandomName + FileName);
FileUpload1.SaveAs(FilePath);
if (Extension.ToLower() == ".csv")
{
UploadSave(Utility.ReadCSVFileNew(FilePath));
}
else
{
UploadSave(Utility.ReadExcelFile(FilePath));
}
divUpload.Style.Add("display", "none");
btnUploadSave.Style.Add("display", "none");
if (myDataTable0.Rows.Count == 0)
{
ShowMessageWithRedirect(recno.ToString(), " Leads Uploaded Successfully", "success", "ViewOpenLeads");
}
else
{
ShowMessage(recno.ToString() + " Leads Uploaded Successfully", myDataTable0.Rows.Count + " Duplicate Leads Found", "info");
}
}
}
catch (Exception ex)
{
Utility.SaveErrorLog(ex.Message, System.IO.Path.GetFileName(Request.Path), System.Reflection.MethodBase.GetCurrentMethod().Name, Convert.ToInt32(hdfBDOID.Value));
ShowMessage("Oops...", ex.Message, "error");
}
}
Thanks
Ramco RamcoPosted May 22, 2023, 5:07 AM
Hi Tuhin
Getting below error - I am getting below error - An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked @ Page Async="true" >. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.
Thanks
Tuhin PaulPosted May 21, 2023, 2:17 PM
Few suggestions::
1. Use `async` and `await` for asynchronous file upload and processing operations to improve performance and responsiveness.
2. Consider using a more descriptive and meaningful variable name instead of `FileUpload1`. For example, `fileUploadControl` or something that reflects its purpose.
3. Move the file processing logic into a separate method to improve code readability and maintainability.
Mohamed Azarudeen ZPosted May 21, 2023, 12:17 PM
Got it
csharp
Hop[ew this updated code helps
Ramco RamcoPosted May 21, 2023, 12:12 PM
Hi
Currently i am uploading single file. I want to upload multiple excel sheets at same time.
Thanks
Mohamed Azarudeen ZPosted May 21, 2023, 12:00 PM
Hi ramco may i knw whats the difficulty you are facing with the above code?