0
private void MovieDisp1()
{
    SqlConnection cn = new SqlConnection("Data Source = localhost; Initial Catalog = CinemaProj; Integrated Security = SSPI");
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    cmd.Connection = cn;
    cmd.CommandType = CommandType.Text;
    cn.Open();
    cmd.CommandText = "Select CM_MovieName, CM_MovieDesc, CM_MovieImage from Tbl_CMovies where CM_ID = '1'";
    SqlDataReader rd = cmd.ExecuteReader();

    while (rd.Read())
    {
        labelmoviename1.Text = rd[0].ToString();
        labelmoviedesc1.Text = rd[1].ToString();
        byte[] img = (byte[])(rd[2]);
        if(img == null)
        {
            ImageMovie1.Image = null;
        }
        else
        {
            MemoryStream ms = new MemoryStream(img);
            ImageMovie1.Image = Image.FromStream(ms);
        }
    }
    cn.Close();
}

I've made a method named MovieDisp1 to retrieve all the information from my database including the image. But now I'm having an error at

ImageMovie1.Image = null

as well as

ImageMovie1.Image = Image.FromStream(ms)

The error is this:

'System.Web.UI.WebControls.Image' does not contain a definition for 'Image' and no extension method 'Image' accepting a first argument of type 'System.Web.UI.WebControls.Image' could be found (are you missing a using directive or an assembly reference?)

Does it mean I can't use .Image for Image Control? Is there any extension method to be used so that I can fix this error? Thanks!

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user808317
  • 21
  • 4
  • 1
    Everything that implements [IDisposable](https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx) interface needs to disposed of properly. You have not done so in your code. You either need to wrap them in [using statements](https://msdn.microsoft.com/en-us/library/yh598w02.aspx) or call `.Dispose()` on them in a `finally` block. – mason Mar 21 '16 at 03:03

2 Answers2

1

First thing to consider is that, the datatype of your Image in database should be VarBinary(MAX).

 using(SqlConnection cn = new SqlConnection("Data Source = localhost; Initial Catalog = CinemaProj; Integrated Security = SSPI"))
{
            SqlCommand cmd = new SqlCommand("SELECT IMAGE FROM t_TableName WHERE ID=1", cn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new DataSet();
           da.fill(ds);

        foreach(DataRow dr in ds.Table[0].Rows)
        {
            imgByte = (byte[])(dr["Image"]);
        }
        string strBase64 = Convert.ToBase64String(imgByte);
}

//for <img> tag use this (imgF is ID of the img TAG):

imgF.Src = "data:Image/png;base64," + strBase64;

//for <asp:Image> use this:

imgF.ImageUrl = "data:Image/png;base64," + strBase64;
JC Borlagdan
  • 3,318
  • 5
  • 28
  • 51
  • Everything that implements [IDisposable](https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx) interface needs to disposed of properly. You have not done so in your code. You either need to wrap them in [using statements](https://msdn.microsoft.com/en-us/library/yh598w02.aspx) or call .Dispose() on them in a finally block. – mason Mar 21 '16 at 13:15
  • oh yes, I stand corrected.. I focused too much on the solution to his question... i edited my answer :) thanks sir – JC Borlagdan Mar 22 '16 at 01:35
  • After struggling through many code samples, this worked for me. Thanks, JC. – John Mar 03 '20 at 20:19
-1

Try reading this: ASP Image Class

And this: Use Byte Array to Create an Image

And Another: how-to-display-image-from-database

Rather than trying to convert the Database Image into an Image Object, just fetch the data and store it in a byte[] Array, and use the binary data directly in your images src.

Create your image element:

<img runat="server" id="image" />

Then just pass that byte array to the image tags .src property. There is more information about that here.

Community
  • 1
  • 1
Nobody
  • 341
  • 2
  • 6