Friends,
I have created an application, from that i have tried to upload the pdf files into database and retrieve from database. Upload has been done successfully. But i can’t retrieve that pdf file from database. Please see my code and suggest me a way to come out from this issue.
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd1 = new SqlCommand("select Docdata from SaveDoc where DocID='" + TextBox1.Text + "'", con);
con.Open();
byte[] b = null;
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da = new SqlDataAdapter(cmd1);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
b = ((byte[])dt.Rows[0][0]); // Error has came here
Response.ContentType = "application/pdf";
Response.BinaryWrite(b);
}
and the error is "Unable to cast object of type ‘System.String’ to type ‘System.Byte[]’."
Do like this: