How to display uploaded files in GridView and download them on clicking respective download linkbutton?

I am uploading files using fileupload, the details of the file such as file type, name of file, and complete path is getting stored in a table called exceldetails in database now i want to display the uploaded files and download them by clicking on a link button next to every file in GridView. This is how i am uploading files:

        string[] filePaths;
    string strFileType = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
    string strFileName = FileUpload1.PostedFile.FileName.ToString();
    FileUpload1.SaveAs(Server.MapPath("~/Import/" + strFileName + strFileType));
    string strNewPath = Server.MapPath("~/Import/" + strFileName + strFileType);
    string strNewPath1 = Server.MapPath("~/Import/" + strFileName + strFileType).ToString();
    fileSize = FileUpload1.PostedFile.ContentLength / 1024;

    DataTable dt8 = new DataTable();
    SqlCommand cmd8 = new SqlCommand("insert into exceldetails (name,type,details,size,path)" + "values(@name,@type,@details,@size,@path)", con);
    cmd8.Parameters.Add("@name", SqlDbType.VarChar).Value = strFileName;
    cmd8.Parameters.Add("@type", SqlDbType.VarChar).Value = strFileType;
    cmd8.Parameters.Add("@details", SqlDbType.VarChar).Value = DateTime.Now;
    cmd8.Parameters.Add("@size", SqlDbType.Int).Value = fileSize;
    cmd8.Parameters.Add("@path", SqlDbType.NVarChar).Value = strNewPath;
        SqlDataAdapter da8 = new SqlDataAdapter(cmd8);
    catch { }

I am a beginner as you can judge from the question itself so please tell me where i need to add what in detail

  • I am trying to create a stored procedure to create a login and a database user?
  • Include carriage returns when calculating substring
  • SQL query to delete databases with specific string
  • Output is being displayed twice
  • SQL UPDATE SET = subquery set (random)
  • The multi-part identifier “System.Data.DataRowView” could not be bound." SQL SERVER + VB.NET
  • Transaction Scope with Two Connection Methods
  • How to return multiple output parameters from stored procedure for C# function
  • SQL Query Returns More Than 1 Row - Need 1 Row
  • Can Enums in C# can be use of replacement of Lookup tables(e.g SportType,MatchType)
  • SQL Server procedure to find rows from table that its id starts with a specific number not working
  • How do I update a database that's in use?
  • 3 Solutions collect form web for “How to display uploaded files in GridView and download them on clicking respective download linkbutton?”

    Also your code is very messy

    Try changing it to something like this for the insert :

    using( SqlConnection cn = new SqlConnection( "Your Conn String" ) )
        string sqlInsert = "INSERT STATEMENT";
        string fileName = FileUploadControl.FileName;
        string fileExtension = fileName.Substring( fileName.LastIndexOf( "." ), fileName.Length - fileName.LastIndexOf( "." ) );
        string filePath = "~/Import/" + fileName + fileExtension;
        int fileSize = FileUploadControl.PostedFile.ContentLength / 1024;
        FileUploadControl.SaveAs( Server.MapPath( filePath ) );
        using( SqlCommand cmd = new SqlCommand( sqlInsert, cn ) )
            cmd.Parameters.AddWithValue( "@name", fileName );
            cmd.Parameters.AddWithValue( "@type", fileExtension );
            cmd.Parameters.AddWithValue( "@details", DateTime.Now );
            cmd.Parameters.AddWithValue( "@size", fileSize );
            cmd.Parameters.AddWithValue( "@path", filePath );

    And this to get data to bind

    using( SqlConnection cn = new SqlConnection( "Your Conn String" ) )
        string sqlSelect = "SELECT STATEMENT";
        using( SqlDataAdapter da = new SqlDataAdapter( sqlSelect, cn ) )
            DataTable dt = new DataTable();
            da.Fill( dt );
            //bind data

    Also look into using a repeater, it is much more flexible than a grid view.

    You will have to add a hyperlink column in your grid and its navigate URL should be set to

    "~/Import/" + strFileName + strFileType

    You are storing the entire physical path in database, along with this you should store the “~/Import/” + strFileName + strFileType in database and use that column as datasource for the download link column.

    Here is an example of how I do this using a gridview template item:

          <asp:HyperLink ID="HyperLink1"
             NavigateUrl='<%# Eval("AttachmentID", "GetAttachment.aspx?ID={0}") %>'

    However, my attachment files are stored in the database. I have a page that accepts a query string parameter that identifies an attachment, which is then used to get the attachment and let the user download it. You should modify the navigateURL property of the hyperlink to link to what needs to be downloaded. Note that ‘AttachmentID’ is a field that is selected by the grid’s datasource, you would likely use ‘FilePath’ or something similar.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.