Click here to Skip to main content
15,909,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
SqlCommand cmd = new SqlCommand("select top 1(CMTID),COMMENT,HEADING from FTAB ORDER BY CMTID DESC", conn);
        conn.Open();
        SqlDataReader sdr = cmd.ExecuteReader();
        if (sdr.Read() == true)
        {
            lblHead.Text = sdr["HEADING"].ToString();
            lblData.Text = sdr["COMMENT"].ToString();
           
        }
        conn.Close();


that is the code for normal data value retrieve from table now I want to get Picture from sql server that save as Binary data that insert code mention in bottom., so I want to retrieve picture on Image control of asp.net so please guide me .

C#
if (FileUpload1.HasFile)
            {
                conn.Close();
                String SqlQery;
                SqlQery = "select max(CMTID) from FTAB";
                SqlCommand cmdid = new SqlCommand(SqlQery, conn);
                conn.Open();
                MaxID = (int)(cmdid.ExecuteScalar()) + 1;
                conn.Close();
                byte[] img = new byte[FileUpload1.PostedFile.ContentLength];
                HttpPostedFile myimg = FileUpload1.PostedFile;
                myimg.InputStream.Read(img, 0, FileUpload1.PostedFile.ContentLength);
                SqlCommand cmd = new SqlCommand("insert into FTAB (CMTID,IMAGEDT,COMMENT,DATETM,HEADING) values (@imgid,@image,@comment,@datetm,@heading)", conn);

                SqlParameter imgid = new SqlParameter("@imgid",SqlDbType.Int);
                imgid.Value = MaxID;
                cmd.Parameters.Add(imgid);

                SqlParameter uploading = new SqlParameter("@image", SqlDbType.Image);
                uploading.Value = img;
                cmd.Parameters.Add(uploading);

                SqlParameter cmtt = new SqlParameter("@comment", SqlDbType.NVarChar);
                cmtt.Value = RadTextBox3.Text;
                cmd.Parameters.Add(cmtt);

                SqlParameter dttm = new SqlParameter("@datetm", SqlDbType.DateTime);
                dttm.Value = DateTime.Now;
                cmd.Parameters.Add(dttm);

                SqlParameter hhding = new SqlParameter("@heading", SqlDbType.NVarChar);
                hhding.Value = RadTextBox8.Text;
                cmd.Parameters.Add(hhding);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                lblHead.Text = "Image uploaded";
            }
            else
            {
                lblHead.Text = "No file selected";
            }


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 11-Oct-12 21:05pm
v3

Hi,

See the below links. It might be help you to find the solution.

Previewing Image in ASP.NET Image Control using C#[^]
http://www.dotnetcurry.com/ShowArticle.aspx?ID=129[^]

Thanks,
Viprat
 
Share this answer
 
For retrieving image u should use generic handler file , pass the imageid field to the file and in image control refer the handler file as source.

Refer this code which is to be placed inside the generic handler

C#
public void ProcessRequest(HttpContext context) {
id=Request.QueryString["Id"]
        SqlCommand command = new SqlCommand();
        try {
            command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings("FileUploadConnectionString").ConnectionString);
            command.CommandText = "select * from [Table] where ImageId = @Id";
            command.Parameters.Add("@FileId", SqlDbType.UniqueIdentifier).Value = id; 
            command.Connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            if (reader.Read()) {
                context.Response.Clear();
                context.Response.ContentType = reader["ImageType"].ToString();
                context.Response.AddHeader("Content-Disposition", string.Format("inline;filename={0};", reader["ImageName"].ToString()));
                context.Response.AddHeader("Content-Length", reader["ImageSize"].ToString());
                context.Response.BinaryWrite(DirectCast, reader["Image"], Byte());
                context.Response.End();
            }
        }
        finally {
            command.Dispose();
        }
 
Share this answer
 
v2

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