0

I am trying to save an Excel file to a share location.

protected void Button1_Click(object sender, EventArgs e)
{
   string conn =      ConfigurationManager.ConnectionStrings["NameConn"].ConnectionString;
            DataTable dt = new DataTable();
using (SqlConnection odbcCon = new SqlConnection(conn))
            {
                odbcCon.Open();
                SqlCommand cmd = new SqlCommand("GetValues", odbcCon);
                cmd.Parameters.AddWithValue("@Name", "abc");
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dt);

            }
    string destFilename = HttpContext.Current.Server.MapPath("~/A.xls");
    System.Net.WebClient client = new System.Net.WebClient();
    client.DownloadFile(path, destFilename);
}

Web.config

<httpRuntime targetFramework="4.5" executionTimeout="10000" maxRequestLength="2147483647" shutdownTimeout="360" />

And I get this error:

  • The operation has timed out.

Can you tell me where I could be wrong.

hello
  • 15
  • 1
  • 6
  • is the file currently open in Excel? –  Mar 31 '15 at 14:42
  • @D.Mac I am making the file and storing it. i think **var destFilename = File.Create(@"\\Networkserver path\websites");** would convert to a string to store the dest file. – hello Mar 31 '15 at 14:48
  • @Bolu "path" will contain the downloaded data where as destFilename will receive the data. But I get error in the line **var destFilename = File.Create(@"\\Networkserver path\websites");** – hello Mar 31 '15 at 14:54
  • oh okay, it's just I often get this error when using C# to parse Excel docs and the actual document is left open. Could be a similar issue here, that it is being created but it's open. I wonder if this would help? : http://stackoverflow.com/questions/5156254/closing-a-file-after-file-create –  Mar 31 '15 at 14:54
  • you don't need to create a file to save the downloaded data, the WebClient.DownloadFile Method will do that for you, check my answer. – Bolu Mar 31 '15 at 14:55

1 Answers1

0

File.Create Method returns a FileStream, while WebClient.DownloadFile Method needs two string parameters one for The URI from which to download data. and the other for The name of the local file that is to receive the data.

So you should do something like:

//you may also need to check if the file is exist. 
string destFilename = @"\\Networkserver path\websites\destexcel.xlsx";

System.Net.WebClient client = new System.Net.WebClient();
client.DownloadFile(path, destFilename);
Bolu
  • 8,696
  • 4
  • 38
  • 70
  • If I do this: `string destFilename = HttpContext.Current.Server.MapPath("~/abc.xls"); System.Net.WebClient client = new System.Net.WebClient(); client.DownloadFile(path, destFilename);` **path is a web service call which will get the data** It works fine, But instead of storing on a local system, I want to store it on a network path. – hello Mar 31 '15 at 14:58
  • @hello, can't you setup a virtual directory in your IIS and maps it to your shared network path, then use `HttpContext.Current.Server.MapPath(path/abc.xls)` in the `DownloadFile` method? – Bolu Mar 31 '15 at 15:03
  • I get Time out Error. I have a database connectivity , so would there be a timeout for my sql connection or my application ? – hello Mar 31 '15 at 15:25
  • @hello, based on your code, it will most possibly be a WebException timeout. Can you update your code? – Bolu Mar 31 '15 at 15:32
  • @hello, just want to clarify: have you resolved the previous error? and this is a new error you are facing? – Bolu Mar 31 '15 at 15:59
  • I do not know whther the previous error has been solved or not. I tried to change the code s you mentioned and then started facing this issue. – hello Mar 31 '15 at 16:15
  • @hello, set breakpoints, and use debug, when you find your exact error and its related line of code, update it in your question. – Bolu Mar 31 '15 at 16:19
  • @hello, then, in your first comments above, did you say it works fine if you use local path like `HttpContext.Current.Server.MapPath("~/abc.xls")` ? If that is not what you mean, please post your `path` in your function `client.DownloadFile(path, destFilename);` as I can't guess what it is.... – Bolu Mar 31 '15 at 16:28