Click here to Skip to main content
15,915,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In my form I am using an option to attach a document. In this, the user is able to attach all kinds of documents. The user has the option to view the attached document, while clicking the view button they can view the document. For all types of document it works fine except for the excel. The original data is not displaying, it's displaying some unknown datas. Can anyone help me to solve this issue. Here is the code:


protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
try
{
    string fileName = ((Label)GridView1.Rows[e.NewSelectedIndex].FindControl("lnkFileName")).Text;
    Session["fileopen"] = fileName;
    string path = GetPath() + fileName;
    FileInfo file = new FileInfo(path);
    if (file.Exists)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MySqlDatabase"].ConnectionString);
        con.Open();
        cmd = new SqlCommand("select * from FileTypes with(nolock) where extension='" + file.Extension + "'", con);
        cmd.CommandTimeout = 0;
        myReader = cmd.ExecuteReader();
        cmd.CommandTimeout = 0;
        if (myReader.Read())
        {
            myReader.Close();
            SessionVarriables.ViewFileSession = path;
            SessionVarriables.ViewFileNameSession = "File";
            Response.Redirect("ViewDocument.aspx");
        }
        else
        {
            myReader.Close();
            Message1("Cannot open selected file");
            return;
        }
        con.Close();
    }
    else
    {
        Message1("File not found");
    }
}
catch (Exception ex)
{
    UserUtil.Message(ex.Message, this);

}

}


Thanks in advance.
Posted
Updated 7-Oct-11 2:14am
v3

1 solution

What does ViewDocument.aspx do? It sounds like it is opening the Excel file as a text file, which won't work because it is binary. Just response.Redirect to the xls file and the users computer will download it.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900