Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a little bit of a snag

I am saving documents (doc,xls, pdf,.....) into sql server database. I have table in sql, with columns Id, document, documentName

and it all works fine, but I need to retrieve documents back first to listbox or listview or something else,(i don't know) maybe dgv, and then by choosing some file from that list to save it on harddisk of course, but first thing first

C#
cs.Open();

SqlCommand cmdDocs = new SqlCommand("SELECT Document, documentName FROM Documents WHERE userID = @userID", cs);
SqlDataAdapter daDocs = new SqlDataAdapter(cmdDocs);
cmdDocs.Parameters.Add("@userID", SqlDbType.NVarChar).Value = DataDetails.id.Text;

DataTable dtDocs = new DataTable();
DataSet dsDocs = new DataSet();

daDocs.Fill(dtDocs);
daDocs.Fill(dsDocs);
//DataSet dsDocs = new DataSet();

object oDocs = cmdDocs.ExecuteScalar();

if (oDocs != DBNull.Value)
{

   Byte[] dataDocs = new Byte[0];

   foreach (DataRow dir in dtDocs.Rows)
   {
      dataDocs = (Byte[])(dsDocs.Tables[0].Rows[0]["Document"]);
      string NameOfImage = (String)dsDocs.Tables[0].Rows[0]["documentName"];

      MemoryStream mem = new MemoryStream(dataDocs);

      //here I have to put something to fill listview or listbox or dgv but I dont know how or what, to get those documents
                        
   }
}
else
{

}

cs.Close();
Posted
Updated 14-Nov-13 0:26am
v3

1 solution

Please Refer this code/.......

protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ImageID"] != null)
        {
            string strQuery = "select ID, DocumentName, Document from" +
                " tblFiles where ID=@id";
            String strConnString = System.Configuration.ConfigurationManager
                .ConnectionStrings["conString"].ConnectionString;
            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.Add("@id", SqlDbType.Int).Value
                = Convert.ToInt32(Request.QueryString["FileID"]);
            SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            DataTable dt = new DataTable();
            try
            {
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
            }
            catch
            {
                dt = null;
            }
            finally
            {
                con.Close();
                sda.Dispose();
                con.Dispose();
            }
            if (dt != null)
            {
                Byte[] bytes = (Byte[])dt.Rows[0]["Document"];
                Response.Buffer = true;
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.ContentType = dt.Rows[0]["ContentType"].ToString();
                Response.AddHeader("content-disposition", "attachment;filename="
                    + dt.Rows[0]["DocumentName"].ToString());
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
            }
        }
    }
 
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