Click here to Skip to main content
15,898,957 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I made an upload function from excel to sql that receives a timeout on the last stored procedure it executes.

C++
new SqlCommand(query, con).ExecuteNonQuery();
--> this invokes a timeout.


It's something I was expecting cause the stored procedure takes a few minutes before it's done.

What I need is that the function can be called from a webpage, and will run in a seperate service so that the user can still work while the function is processing.

As example:

C#
public static void Upload(string filePath, string tagName, string user)
{
    var query = "Exec ExcelImport.sp_ExcelImport '" + filePath +
            "', '" + user + "', '" + tagName + "'";
    var con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DataSourceConnectionString"].ConnectionString);
    con.Open();
    new SqlCommand(query, con).ExecuteNonQuery();
    con.Close();
}


This should run seperate without returning a timeout, and if possible return an answer that it's done or failed.

What would be my best approach?


Thanks in advance.
Posted

Hi,
You can try this one...

public static void Upload(string filePath, string tagName, string user)
{
var query = "Exec ExcelImport.sp_ExcelImport '" + filePath +
"', '" + user + "', '" + tagName + "'";
var con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DataSourceConnectionString"].ConnectionString);
con.Open();
SqlCommand com = new SqlCommand(query, con)
com.Timeout = 0;
com.ExecuteNonQuery();
con.Close();
}
 
Share this answer
 
v2
By Adding the code that abhishek_singh provided me the timeout was resolved.

But I also added a thread so it could run seperate from the rest now.

C#
protected void btnUpload_Click(object sender, EventArgs e)
{
    if (excelUpload.HasFile)
    {
        string fileName = Path.GetFileName(excelUpload.PostedFile.FileName);
        string fileLocation = Server.MapPath("~/App_Data/" + fileName);
        excelUpload.SaveAs(fileLocation);

        var thread = new Thread(() => Performthread(fileLocation, fileName));
        thread.Start();

    }
}

private void Performthread(string fileLocation, string fileName)
{
    ExcelService.Upload(fileLocation, "BILLING", "tom");
    File.Delete(Server.MapPath("~/App_Data/" + fileName));
    _syncId = 0;
}
 
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