I cannot get word file saved as byte from database to display on webpage

Donald Symmons 3,066 Reputation points
2022-06-06T09:35:34.487+00:00

After uploading and inserting word file into my database; I am trying to display the file on another page but when it redirected to the next page, the file don't seem to show. Please how can I make this work?

This is how the ContentType is saved:
application/vnd.openxmlformats-officedocument.wordprocessingml.document

To explain better; After I upload a word file and load it in page1.aspx, I click on a button that will redirect to page2.aspx and call the same word file from database to display on that page2.aspx.
Here is how it shows:208725-word-display.png

this is code to insert and redirect to page2.aspx

  protected void buttonmail_Click(object sender, EventArgs e)  
        {  
                string filename = Path.GetFileName(files.PostedFile.FileName);  
                string contentType = files.PostedFile.ContentType;  
                using (Stream fs = files.PostedFile.InputStream)  
                {  
                    using (BinaryReader br = new BinaryReader(fs))  
                    {  
                        byte[] bytes = br.ReadBytes((Int32)fs.Length);  
                        using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))  
                        {  
                            string query = "INSERT INTO Table(email, Name, ContentType, Data, Uid, CreatedBy, Role, CreatedDate, Organization)" +  
                        " VALUES(@email, @Name, @ContentType, @Data, @Uid, @CreatedBy, @Role, @CreatedDate, @Organization); SELECT @@IDENTITY";  
                            using (SqlCommand cmd = new SqlCommand(query))  
                            {  
                                cmd.Connection = con;  
                                cmd.Parameters.AddWithValue("@email", user.Text.Trim());  
                                cmd.Parameters.AddWithValue("@Name", filename);  
                                cmd.Parameters.AddWithValue("@ContentType", contentType);  
                                cmd.Parameters.AddWithValue("@Data", bytes);  
                                cmd.Parameters.AddWithValue("@Uid", labelid.Text.Trim());  
                                cmd.Parameters.AddWithValue("@CreatedBy", createby.Text.Trim());  
                                cmd.Parameters.AddWithValue("@Role", role.Text.Trim());  
                                cmd.Parameters.AddWithValue("@CreatedDate", DateTime.Now);  
                                cmd.Parameters.AddWithValue("@Organization", named.Text.Trim());  
                                con.Open();  
                                object Invoice = cmd.ExecuteScalar();  
                                con.Close();  
                                Session["indocx"] = Invoice;  
                                Response.Redirect("Page2.aspx");  
                            }  
                        }  
                    }  
                }  
        }  

then Page2.aspx

 <div class="container-fluid shadow-sm p-3 mb5 bg-white rounded" id="card" style="font-family: 'Roboto', sans-serif; width: 90%; margin: 0 auto; border: 1px solid #e4e7e8;">  
                            <asp:Panel ID="pnlContents" runat="server">  
                                <div class="book" style="width: 100%; height: 100%;">  
                                    <div class="page" id="parent" style="width: 100%;">  
                                        <div class="subpage" style="width: 100%; height: 100%;">  
                                            <asp:Image ID="Image3" runat="server" BorderStyle="none" />  
                                        </div>  
                                    </div>  
                                </div>  
                            </asp:Panel>  
                        </div>  

Page2.aspx.cs

 protected void Page_Load(object sender, EventArgs e)  
    {  
        SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");  
        if (Session["indocx"] != null)  
        {  
            DisplayDocument();  
        }  
    }  
  
    private void DisplayDocument()  
    {  
        SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");  
        if (Session["indocx"] != null)  
        {  
            int id = Convert.ToInt32(Session["indocx"]);  
  
            string sql = "SELECT * FROM Table WHERE Id = @Id";  
            SqlParameter[] parameters = new[] {  
                    new SqlParameter("@Id", id)  
                };  
  
            // Bind Image3 for Card Image  
            DataTable dt = SelectFromDatabase(sql, parameters);  
            if (dt.Rows.Count > 0)  
            {  
                Image3.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dt.Rows[0]["Data"]);  
                Image3.AlternateText = "The Image has been found";  
            }  
            else  
            {  
                Image3.ImageUrl = "#";  
                Image3.AlternateText = "The Image does not exist in DataBase";  
            }  
        }  
    }  
  
    public static DataTable SelectFromDatabase(string sql, SqlParameter[] parameters)  
    {  
        using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))  
        {  
  
            using (SqlCommand cmd = new SqlCommand(sql, con))  
            {  
                if (parameters != null)  
                {  
                    cmd.Parameters.AddRange(parameters);  
                }  
  
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))  
                {  
                    DataTable dt = new DataTable();  
                    sda.Fill(dt);  
                    return dt;  
                }  
            }  
        }  
    }  
Developer technologies | ASP.NET | Other
Developer technologies | C#
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.