Click here to Skip to main content
Sign Up to vote bad
good
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 - 9:06
leana88578

Comments
richcb - 15 Jan '13 - 15:12
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:46
The value path is nvarchar(MAX) This is table : id int sname nvarchar(MAX) path nvarchar(MAX)
Member 9581488 - 15 Jan '13 - 15:36
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:43
The value path is nvarchar(MAX) This is table : id int sname nvarchar(MAX) path nvarchar(MAX)
Member 9581488 - 15 Jan '13 - 15:48
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:58
I do not understand what do you mean value, but these values ​​table:- http://www.4shared.com/photo/o4SKuWru/5554.html
Member 9581488 - 15 Jan '13 - 16:02
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:13
Sorry I do not know how to use breakpoint :(..... anyway, Thanks for the help :)
Member 9581488 - 15 Jan '13 - 16:16
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 - 15 Jan '13 - 16:39
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 - 15 Jan '13 - 16:47
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:50
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 - 15 Jan '13 - 17:44
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 - 15 Jan '13 - 19:29
<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 - 16 Jan '13 - 3:28
Thank you a lot ^_^
Member 9581488 - 16 Jan '13 - 9:05
Should I copy it in ans? SO you can accept the ans and close this thread.
leana88 - 16 Jan '13 - 14:46
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 - 16 Jan '13 - 15:40
copy this line in your web.config before closing tag of system.web I am not able to copy that line here!!!!!!
Member 9581488 - 16 Jan '13 - 15:43
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:08
i use NET Framework 3.5 - VS2008
Member 9581488 - 16 Jan '13 - 16:15
I am posting the ans in solution. because i m not able to copy it in comments.
leana88 - 16 Jan '13 - 16:37
Thank you very much... Now i can complete my project ^_^ Спасибо большое моему другу. ты умный человек
Member 9581488 - 16 Jan '13 - 16:39
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:48
I always use google translation, because i do not understand English well ^_^
Member 9581488 - 16 Jan '13 - 16:49
Thanks to google then.... ppl from around world can communicate. good luck with your project.
leana88 - 16 Jan '13 - 16:53
Thank you ^_^
leana88 - 15 Jan '13 - 15:43
The value path is nvarchar(MAX) This is table : id int sname nvarchar(MAX) path nvarchar(MAX)
Hasham Ahmad - 15 Jan '13 - 21:44
it would be great if you post the binding method of gridview here that will be more useful in debugging your code

1 solution

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

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 325
1 Sergey Alexandrovich Kryukov 140
2 Mohammed Hameed 123
3 Santhosh G_ 113
4 Ron Beyer 99
0 Sergey Alexandrovich Kryukov 8,286
1 OriginalGriff 6,561
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid