Click here to Skip to main content
12,818,489 members (33,349 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET Gridview
I am getting this error "Could not find a part of the path 'c:\users\mehdi\documents\visual studio 2012\Projects\Grid\Grid\Data Files\'. when downloading a file saved in the above directory.Code for downloading is:

protected void LinkButton1_Click(object sender, EventArgs e)
        LinkButton lnkbtn = sender as LinkButton;
        GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
        string filePath = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
        Response.TransmitFile(Server.MapPath("~/Data Files/"));

Please help..
Posted 25-Sep-13 9:55am
Sergey Alexandrovich Kryukov 25-Sep-13 15:14pm
How can you ask such question not giving us information on your paths? How do we know what is the root directory of your site? Any little mistake in paths could lead to this problem...
rf_libra 25-Sep-13 15:29pm
@Sergey Alexandrovich Kryukov, Thanks for replying. I used the same path ("~/Data Files/") to upload the file in the first place. To make sure of the path, I deleted the folder in the root directory and copied the path from the error message that is ( "c:\users\mehdi\documents\visual studio 2012\Projects\Grid\Grid\Data Files") and used " mkdir "c:\users\mehdi\documents\visual studio 2012\Projects\Grid\Grid\Data Files" " from cmd. Still getting the same error. Thanks
Sergey Alexandrovich Kryukov 25-Sep-13 16:33pm
Can you just use the debugger and see what MapPath actually returns? And then correct the path...
rf_libra 25-Sep-13 16:37pm
Sure. Good idea.
rf_libra 25-Sep-13 16:40pm
@SA, Actual Path returns as 'c:\users\mehdi\documents\visual studio 2012\Projects\Grid\Grid\Data Files\37' . 37 is the id of the record in the database server.(PK)
Sergey Alexandrovich Kryukov 25-Sep-13 16:49pm
Okay, you don't have the path with '37', apparently, get rid of it...
rf_libra 25-Sep-13 16:57pm
I removed FilePath and got rid of 37. Now its giving same error as "Could not find a part of the path 'c:\users\mehdi\documents\visual studio 2012\Projects\Grid\Grid\Data Files\"
Sergey Alexandrovich Kryukov 25-Sep-13 16:59pm
I guess, instead of '37' should be some file name? Come on, you are almost there...
rf_libra 25-Sep-13 17:04pm
37 was the id of the record saved in SQL server. I am displaying it in the gridview in the next column to the LinkButton (Download). I am wondering why is it reading id as the filePath when I have actually saved the file in a root folder.
rf_libra 25-Sep-13 17:08pm
Ok, I got it. DataKeys in the following line of code is giving me 37.

string filePath = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();

I commented out this line of code. But still giving me the same error:
"Could not find a part of the path 'c:\users\mehdi\documents\visual studio 2012\Projects\Grid\Grid\Data Files\'."
rf_libra 25-Sep-13 17:16pm
Can you please help me get the filename somehow. I am stuck at it. I am saving the file in the root directory folder called "Data Files". Cant I use something like:

string strName = Directory.GetFiles(Server.MapPath("~/Data Files"));

to get the filename?
Sergey Alexandrovich Kryukov 25-Sep-13 18:43pm
This is the name of the directory, and then the file name should follow it. You should know better what it is. Usually, it is stored as one of the attributes in your database table.
rf_libra 25-Sep-13 18:26pm
--SA, You are a true legend. Hats off to you. I learned best of my lessons from your way of teaching. Salute you. Thank you so much. I have got it done. Thanks alot. Blessings.
Sergey Alexandrovich Kryukov 25-Sep-13 18:44pm
Thank you for your nice words.

If your problem is solved, let's close it. For a record, I put a formal answer based on this dialog. If your problem is solved, please formally accept the answer (green "Accept" button, I guess).

rf_libra 25-Sep-13 16:03pm
You there?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Perhaps what you need to do it specify a single file, instead of a folder?
Response.TransmitFile(Server.MapPath("~/Data Files/"));
Did you mean:
Response.TransmitFile(Server.MapPath("~/Data Files/" + filePath));
rf_libra 25-Sep-13 15:40pm
@OriginalGriff , your solution is correct but I have a little problem. My code is reading DocumentsID(PK) as Filename. I have not saved filename anywhere in my database. I am saving it in a folder created in the root directory of my application. Help please???
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I hope our dialog in the comments to the questions helped to resolve your problem. If not, your follow-up questions will be welcome.

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.170308.1 | Last Updated 25 Sep 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