Upload and Retrive image from database
I need to upload an image and retrive it with its ID and place it on an image control in a web application. I was able to get the picture on to the form but unable to assin it the the source of the control.I would be glad if someone could help me out.The code is as follows:
private void btnSubmit_Click(object sender, System.EventArgs e)
{
if(fileUpload.PostedFile!=null)
{
HttpPostedFile File = fileUpload.PostedFile;
byte[] Data = new Byte[File.ContentLength];
File.InputStream.Read(Data,0,File.ContentLength);
object obj = new object();
obj = Data;
con = new SqlConnection("server=II-PROXY;database=test;uid=test;pwd=test123");
con.Open();
cmd = new SqlCommand("insertImage",con);
cmd.CommandType=CommandType.StoredProcedure;
p = new SqlParameter[2];
p[0] = cmd.Parameters.Add("@img",SqlDbType.Image);
p[0].Value=obj;
p[1] = cmd.Parameters.Add("@ans",SqlDbType.VarChar,20);
p[1].Value=txtAns.Text;
cmd.ExecuteNonQuery();
con.Close();
}
}
private void btnView_Click(object sender, System.EventArgs e)
{
SqlConnection con = new SqlConnection("server=II-PROXY;database=test;uid=test;pwd=test123");
con.Open();
cmd = new SqlCommand("select codeImage from Imaging where ID=1",con);
dr=cmd.ExecuteReader();
dr.Read();
byte [] imagestr=(byte[])dr.GetValue(0);
Response.BinaryWrite(imagestr);
}
Thanks and Regards
Ashok Kumar
ashok
|