Click here to Skip to main content
12,697,525 members (26,911 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi ^_^

i have an error when download files from gridview

i saved path in data base and files in a folder in project files ,

and this is error message:[^]

this is download code file using gridview:

protected void Button3_Click(object sender, EventArgs e)
        Button bn = sender as Button;
        GridViewRow gr = bn.NamingContainer as GridViewRow;
        string path = GridView1.DataKeys[gr.RowIndex].Value.ToString();
        Response.ContentType = "image/jpg";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + path + "\"");
        Response.TransmitFile(Server.MapPath(path)); // The error is here


and this is upload code :

protected void Button1_Click(object sender, EventArgs e)
            string filename2 = Path.GetFileName(FileUpload1.PostedFile.FileName);
            FileUpload1.SaveAs(Server.MapPath("Files/" + filename2));
            SqlCommand com = new SqlCommand("insert into song (sname,path) values (@s,@p)",;
            com.Parameters.AddWithValue("@s", SqlDbType.NVarChar).Value = TextBox1.Text;
            com.Parameters.AddWithValue("@p", SqlDbType.NVarChar).Value = "Files/" + filename2;

any help ?
Posted 15-Jan-13 10:06am
richcb 15-Jan-13 15:12pm
What is the error message? Have you set a breakpoint at the declaration of the "path" variable to see what value it is being assigned?
leana88 15-Jan-13 15:46pm
The value path is nvarchar(MAX)
This is table :

id int
sname nvarchar(MAX)
path nvarchar(MAX)
Member 9581488 15-Jan-13 15:36pm
What is the value of path? Also, You have to define path when downloading file. Where you upload your file?
leana88 15-Jan-13 15:43pm
The value path is nvarchar(MAX)
This is table :

id int
sname nvarchar(MAX)
path nvarchar(MAX)
Member 9581488 15-Jan-13 15:48pm
Details provided by you is datatype of path. I asked the value of path on this line
string path = GridView1.DataKeys[gr.RowIndex].Value.ToString();

put breakpoint and see the value.
leana88 15-Jan-13 15:58pm
I do not understand what do you mean value,
but these values ​​table:-
Member 9581488 15-Jan-13 16:02pm
Do you know how to use debugger??
Use breakpoint and see what is the value of string path. Is it returning some value from database? because error says your file is not found means your path (to find the file) is wrong.
leana88 15-Jan-13 16:13pm
Sorry I do not know how to use breakpoint :(.....
Thanks for the help :)
Member 9581488 15-Jan-13 16:16pm
give it try.
Response.TransmitFile(Server.MapPath(path)); //change it to
Response.TransmitFile(Server.MapPath("Files/5.png")); and if it doesnt work then try the below line.
give it try and respond.
leana88 15-Jan-13 16:39pm
this worked:
But I have a lot of files within the gridview , How can download when click on any file ?
Member 9581488 15-Jan-13 16:47pm
Response.TransmitFile(Server.MapPath("Files/5.png")); if this is working then something is wrong when you assign value on this line
string path = GridView1.DataKeys[gr.RowIndex].Value.ToString();
Its not getting proper value of datakeys.
Check your gridview data.

Member 9581488 15-Jan-13 16:50pm
Another try:
string path = GridView1.DataKeys[gr.RowIndex].Values["path"].ToString();
You have to have Datakeys in your gridview tag and One hidden boundfield with DataField="path"

Hope you understand what i mean.
leana88 15-Jan-13 17:44pm
i checked with this code :-
string path = GridView1.DataKeys[gr.RowIndex].Values["path"].ToString();
but i received in this line this error:-
Object reference not set to an instance of an object.

Member 9581488 15-Jan-13 19:29pm
<asp:gridview id="GridView1" runat="server" datakeynames="path">

<asp:boundfield datafield="path"

Use the above statements and run your code.
leana88 16-Jan-13 3:28am
Thank you a lot ^_^
Member 9581488 16-Jan-13 9:05am
Should I copy it in ans? SO you can accept the ans and close this thread.
leana88 16-Jan-13 14:46pm

I have another note on this topic :-
The upload code does not accept uploading large files(any file), for example, 4Mb or more than that... Why?
And when try it, received this error:-

This webpage is not available
The connection to localhost was interrupted.
Error 101 (net::ERR_CONNECTION_RESET): The connection was reset.
*Sorry for the more questions* ^_^
Member 9581488 16-Jan-13 15:40pm
copy this line in your web.config before closing tag of system.web <httpruntime maxrequestlength="2097151">
I am not able to copy that line here!!!!!!
Member 9581488 16-Jan-13 15:43pm
Its not accepting large file because it has some default default file size and it depends on which version of .net framework you are using.

leana88 16-Jan-13 16:08pm
i use NET Framework 3.5 - VS2008
Member 9581488 16-Jan-13 16:15pm
I am posting the ans in solution.
because i m not able to copy it in comments.
leana88 16-Jan-13 16:37pm
Thank you very much...
Now i can complete my project ^_^
Спасибо большое моему другу.
ты умный человек
Member 9581488 16-Jan-13 16:39pm
I am so glad to help you..
good luck!
and thanks for the comment..but I had to use google translation... :)
leana88 16-Jan-13 16:48pm
I always use google translation, because i do not understand English well ^_^
Member 9581488 16-Jan-13 16:49pm
Thanks to google then.... ppl from around world can communicate.
good luck with your project.
leana88 16-Jan-13 16:53pm
Thank you ^_^
leana88 15-Jan-13 15:43pm
The value path is nvarchar(MAX)
This is table :

id int
sname nvarchar(MAX)
path nvarchar(MAX)

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

<httpRuntime maxRequestLength="2097151" executionTimeout="9999"/>

Please copy first line before closing tag of system.web in your web.config file.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100