Click here to Skip to main content
Rate this: bad
good
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:
 
http://www.4shared.com/photo/1qvk0uBC/one.html[^]
 
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
        Response.End();
        
 
        
    }
 
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)", c1.cn);
            com.Parameters.AddWithValue("@s", SqlDbType.NVarChar).Value = TextBox1.Text;
            com.Parameters.AddWithValue("@p", SqlDbType.NVarChar).Value = "Files/" + filename2;
            c1.cn.Open();
            com.ExecuteNonQuery();
            c1.cn.Close();
           
        
    }
 
any help ?
Posted 15-Jan-13 10:06am
leana88622
Comments
richcb at 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 at 15-Jan-13 15:46pm
   
The value path is nvarchar(MAX)
This is table :
 
id int
sname nvarchar(MAX)
path nvarchar(MAX)
Member 9581488 at 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 at 15-Jan-13 15:43pm
   
The value path is nvarchar(MAX)
This is table :
 
id int
sname nvarchar(MAX)
path nvarchar(MAX)
Member 9581488 at 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 at 15-Jan-13 15:58pm
   
I do not understand what do you mean value,
but these values ​​table:-
 
http://www.4shared.com/photo/o4SKuWru/5554.html
Member 9581488 at 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 at 15-Jan-13 16:13pm
   
Sorry I do not know how to use breakpoint :(.....
anyway,
Thanks for the help :)
Member 9581488 at 15-Jan-13 16:16pm
   
alright.
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.
Response.TransmitFile(Server.MapPath("~/Files/5.png"));
give it try and respond.
leana88 at 15-Jan-13 16:39pm
   
this worked:
Response.TransmitFile(Server.MapPath("Files/5.png"));
But I have a lot of files within the gridview , How can download when click on any file ?
Member 9581488 at 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 at 15-Jan-13 16:50pm
   
Another try:
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 at 15-Jan-13 17:44pm
   
Hi,
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 at 15-Jan-13 19:29pm
   
<asp:gridview id="GridView1" runat="server" datakeynames="path">
 
<asp:boundfield datafield="path"
Visible="false"
headertext="Path"/>
 

 
Use the above statements and run your code.
leana88 at 16-Jan-13 3:28am
   
Thank you a lot ^_^
Member 9581488 at 16-Jan-13 9:05am
   
Should I copy it in ans? SO you can accept the ans and close this thread.
leana88 at 16-Jan-13 14:46pm
   
Hi,
 
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 at 16-Jan-13 15:40pm
   
copy this line in your web.config before closing tag of system.web
I am not able to copy that line here!!!!!!
Member 9581488 at 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 at 16-Jan-13 16:08pm
   
i use NET Framework 3.5 - VS2008
Member 9581488 at 16-Jan-13 16:15pm
   
I am posting the ans in solution.
because i m not able to copy it in comments.
leana88 at 16-Jan-13 16:37pm
   
Thank you very much...
Now i can complete my project ^_^
Спасибо большое моему другу.
ты умный человек
Member 9581488 at 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 at 16-Jan-13 16:48pm
   
I always use google translation, because i do not understand English well ^_^
Member 9581488 at 16-Jan-13 16:49pm
   
Thanks to google then.... ppl from around world can communicate.
good luck with your project.
leana88 at 16-Jan-13 16:53pm
   
Thank you ^_^
leana88 at 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
good
Please Sign up or sign in to vote.

Solution 1

<httpRuntime maxRequestLength="2097151" executionTimeout="9999"/>
	</system.web>
 
Please copy first line before closing tag of system.web in your web.config file.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 Afzaal Ahmad Zeeshan 159
4 BillWoodruff 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
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