You must Sign In to post a response.
  • Category: LINQ

    Download and view excel sheet in asp.net

    Hello Developers,
    i am successfully uploaded a file with filename,filepath,filetype and stored it inside the database but now am trouble with Download and View the excel sheet.So please help me for how am download and view the excel sheet
    Below is my code,
    <--Download-->
    protected void lnkDownload_Click(object sender, EventArgs e)
    {
    string path = Server.MapPath("~/");
    string phy_path = "http://localhost:22439/ALUMNI%20PROCESS";
    string filePath = (sender as LinkButton).CommandArgument;
    Response.ContentType = ContentType;
    Response.AppendHeader("Content-Disposition", "ExcelSheet; filename=" + path + phy_path + getFilename(filePath));
    Response.WriteFile(filePath);
    Response.End();

    }
    <--View-->

    I Dontr know what to write here


    Thanks with,
    Paul.S
  • #762265

    Hi Paul Raj,

    You are storing the file in database, while retrieving the same you need to get back the same by writing some sql query, but I didn't see any code part for the same.

    Yesterday I give you one link for upload and download the file, did you tried using that post..?

    It should work 100% without issues, Please try that first. If you got any error please share to us, we will help you to clear that issue.

    Refer below link.

    http://www.aspsnippets.com/Articles/Upload-and-Download-files-from-SQL-Server-Database-in-ASPNet.aspx


    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762266
    Yes i has been tried but i did not got exactly output.The error is like this

    Unable to cast object of type 'System.String' to type 'System.Byte[]'.

    Because i can't able to understand this concept well.

    anyway thanks mr.naveen

  • #762267

    Hi Paul Raj,

    As per error details, You are trying to convert string data into byte format, at that time there is some issue while convert/cast the object, that is the reason you may get this type of issue.

    Any how you should understand the post in that link, they were explain step by step with simple example. Design your structure as same like the post and see,

    Mr . Mudassar Khan, is the one to explain any type of article with simple and easy manner.

    So, I request you to understand the post first and then try same like, and do your modifications later.


    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762274
    hmmmm, u r right, Let me understand.
    sorry for my inconvenience.
    Thanks with
    Paul.S

  • #762276
    i am using OLEDB database mr.naveen so there is any issue by that.So i got error ah or oledb is also possible ah mr.neveen.

  • #762289
    Hi Paul Raj,

    OLEDB or SQL these for storage purpose, there is no relation using OLEDB. As per error message you are trying to cast string to byte[] format. In that case you are getting this error. I request you to please put a breakpoint over byte conversion and see. May be string is null type that is the reason while casting time you are getting this error, put a breakpoint and check line by line and confirm me which line, you are getting this error and what's the value of the string in that particular row.

    Please let me know...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762293
    yes , you are right mr.naveen filename,contenttype and byte all are null value now.The break point is jump over the line. they are not run. am trying to clear the error . Thanks a lot naveen.

    Paul.S

  • #762303
    When i go to download i got error only on this line.
    Like below is myquery

    protected void DownloadFile(object sender, EventArgs e)
    {
    int id = int.Parse((sender as LinkButton).CommandArgument);
    byte[] bytes;
    string fileName, contentType;
    using (OleDbCommand cmd = new OleDbCommand())

    // string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (OleDbConnection con = new OleDbConnection(constr))
    {

    {
    cmd.CommandText = "select Name, Data, ContentType from tbl_FileUpload where Id=@Id";
    cmd.Parameters.AddWithValue("@Id", id);
    cmd.Connection = con;
    con.Open();
    using (OleDbDataReader sdr = cmd.ExecuteReader())
    {
    sdr.Read();
    bytes = (byte[])sdr["Data"];
    contentType = sdr["ContentType"].ToString();
    fileName = sdr["Name"].ToString();
    }
    con.Close();
    }
    }
    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = contentType.ToString();
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
    }

    Error :
    Unable to cast object of type 'System.String' to type 'System.Byte[]'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Byte[]'.

    Source Error:


    Line 46: {
    Line 47: sdr.Read();
    Line 48: bytes = (byte[])sdr["Data"];
    Line 49: contentType = sdr["ContentType"].ToString();
    Line 50: fileName = sdr["Name"].ToString();

    Source File: d:\Paul\BACKUP\ALUMNI PROCESS\usersettings\ViewDirectory.aspx.cs Line: 48


    Anyone know the solution please let me know.

    Thanks with
    Paul.S

  • #762304
    Hi PaulRaj,

    Before convert string to byte check this condition.


    if(sdr["Data"]!=null)
    bytes = (byte[])sdr["Data"];


    I guess this will work fine,

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments