1

I want to retrieve an image from an Oracle database to an Image control in asp.net. I tried but it's not working.

This is the code used for inserting image into database:

protected void btnUpload_Click(object sender, EventArgs e)
{
        int imgLength = 0;
        string imgContentType = null;
        string imgFileName = null;

        Stream imgStream = FileUpload.PostedFile.InputStream;
        imgLength = FileUpload.PostedFile.ContentLength;
        imgContentType = FileUpload.PostedFile.ContentType;
        imgFileName = FileUpload.PostedFile.FileName;

        if (imgContentType == "image/jpeg" || imgContentType == "image/gif" ||
        imgContentType == "image/pjpeg"
          || imgContentType == "image/bmp")
         {
            OracleConnection DbConnection = new OracleConnection(con1);
            DbConnection.Open();
            FileStream fls;
            fls = new FileStream(@imgFileName, FileMode.Open, FileAccess.Read);

            byte[] blob = new byte[fls.Length];
            fls.Read(blob, 0, System.Convert.ToInt32(fls.Length));
            fls.Close();

            string query = "insert into image(id,name,photo) values(1,'" + imgFileName + "'," + " :BlobParameter )";
            // Establish a new OracleCommand
            OracleCommand cmd = new OracleCommand();

            cmd.CommandText = query;

            cmd.Connection = DbConnection;

            cmd.CommandType = CommandType.Text;

            System.Data.OracleClient.OracleParameter paramImage = new System.Data.OracleClient.OracleParameter("image",
              Oracle.DataAccess.Client.OracleDbType.Blob);
            paramImage.ParameterName = "BlobParameter";
            paramImage.Value = blob;
            paramImage.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(paramImage);

            cmd.ExecuteNonQuery();
}

Table:

  Id      Name                                 Photo
   1      C:\\user\pictures\animal.jpeg        (BLOB)

Below is the code used to retrieve the image into an image control but this code is not working. For the past two days I've been struggling with this

void GetImagesFromDatabase()
{
        try
        {
            OracleConnection DbConnection = new OracleConnection(con1);
            DbConnection.Open();
            OracleCommand cmd = new OracleCommand("Select name,photo from Image", DbConnection);
            OracleDataReader oda = cmd.ExecuteReader();

            while (oda.Read())
            {
                string path = oda[0].ToString();
                img.ImageUrl = path;
                if(oda.GetValue(1).ToString() !=""){
                    FileStream fls;
                    fls = new FileStream(@path, FileMode.Open, FileAccess.Read);

                    byte[] blob = new byte[fls.Length];
                    fls.Read(blob, 0, System.Convert.ToInt32(fls.Length));

                    fls.Close();
                    MemoryStream memStream = new MemoryStream(blob);

                    img.ImageUrl = oda[2].ToString();

                }
            }
        }
        catch (Exception ex)
        {
        }
}

Any ideas? Thanks in advance

1
  • 1
    The ImageUrl is just that - an URL that points to the image. You cannot just assign the byte stream to that url ..... see this other SO question for a way to do this using a HttpHandler to return images Commented Oct 21, 2013 at 9:35

1 Answer 1

1

maybe this code can help you:

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.