2

I want to read Filestream column form Sql database. After reading I want to show the file content in a TextBox. In case of .txt file it is working fine but in other type like pfd or doc it is not readable.

Here is the code:

SqlConnection objSqlCon = new SqlConnection(constr);
objSqlCon.Open();
SqlTransaction objSqlTran = objSqlCon.BeginTransaction();
SqlCommand objSqlCmd = new SqlCommand("_p_CV_Download", objSqlCon, objSqlTran);
objSqlCmd.CommandType = CommandType.StoredProcedure;

SqlParameter objSqlParam1 = new SqlParameter("@Branch", SqlDbType.NVarChar);
objSqlParam1.Value = Session["Branch"].ToString();
objSqlCmd.Parameters.Add(objSqlParam1);

SqlParameter objSqlParam2 = new SqlParameter("@Doc_No", SqlDbType.VarChar);
objSqlParam2.Value = (dataItem["Doc_No"].FindControl("DocNoLabel") as Label).Text;
objSqlCmd.Parameters.Add(objSqlParam2);

string path = string.Empty;
string fileType = string.Empty;
SqlDataReader sdr;
using (sdr = objSqlCmd.ExecuteReader())
{
    while (sdr.Read())
    {
        path = sdr[0].ToString();
        fileType = sdr[1].ToString();
    }
}
objSqlCmd = new SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", objSqlCon, objSqlTran);
byte[] objContext = (byte[])objSqlCmd.ExecuteScalar();

SqlFileStream objSqlFileStream = new SqlFileStream(path, objContext, FileAccess.Read, FileOptions.SequentialScan, 0);
byte[] buffer = new byte[(int)objSqlFileStream.Length];
objSqlFileStream.Read(buffer, 0, buffer.Length);
string results = System.Text.Encoding.ASCII.GetString(buffer);
ListBox1.Items.Add(results);
objSqlFileStream.Close();
objSqlTran.Commit();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

0 Answers0