Retrieving image from database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sana krishna
    New Member
    • Apr 2008
    • 7

    Retrieving image from database

    Pls Help me--------------
    I use ASP(C#.NET) and oracle.
    When i retrive image from database .it display error message...
    System.InvalidC astException: Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
    my code in the show button is.pls anybody help me ,how to retrive and display the image
    protected void btnshow_Click(o bject sender, EventArgs e)
    {
    DataTable dt = ds.Tables[0];
    MemoryStream stream = new MemoryStream();
    foreach (DataRow dr in dt.Rows)
    { if (dr[0].ToString() == drpeno.Selected Item.ToString() )
    {
    byte[] blob = (byte[])dr[1];
    stream.Write(bl ob,0,blob.Lengt h);
    Bitmap bitm = new Bitmap(stream);
    Response.Conten tType = "image/jpeg";
    bitm.Save(Respo nse.OutputStrea m,ImageFormat.J peg);
    Label1.Text = dr[0].ToString();
    // pcimage.ImageUr l = "~/showimage.ashx? id=" + id;

    }
    }
    }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I know nothing about databases, let me say that now.

    But I think I can interpret the error message for you.
    The error reads "InvalidCastExc eption". So you are being told that you tried to cast something from one type to another and that cast failed.

    Looking at your code I suspect the offending command was "Bitmap bitm = new Bitmap(stream); " The syntax Bitmap(stream) reads like you are trying to convert a stream object to a Bitmap object. Instead try " = new Bitmap.FromStre am(stream);"

    I usually am doing "Image.FromStre am(xxx)", but I see that Bitmap inherits from Image and there is a FromStream member to Bitmap, so I'm guessing it should work.

    If not, maybe this will help point you in a workable direction.
    http://support.microso ft.com/kb/309482


    tlhIn'toQ

    Originally posted by sana krishna
    Pls Help me--------------
    I use ASP(C#.NET) and oracle.
    When i retrive image from database .it display error message...
    System.InvalidC astException: Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
    my code in the show button is.pls anybody help me ,how to retrive and display the image
    protected void btnshow_Click(o bject sender, EventArgs e)
    {
    DataTable dt = ds.Tables[0];
    MemoryStream stream = new MemoryStream();
    foreach (DataRow dr in dt.Rows)
    { if (dr[0].ToString() == drpeno.Selected Item.ToString() )
    {
    byte[] blob = (byte[])dr[1];
    stream.Write(bl ob,0,blob.Lengt h);
    Bitmap bitm = new Bitmap(stream);
    Response.Conten tType = "image/jpeg";
    bitm.Save(Respo nse.OutputStrea m,ImageFormat.J peg);
    Label1.Text = dr[0].ToString();
    // pcimage.ImageUr l = "~/showimage.ashx? id=" + id;

    }
    }
    }

    Comment

    Working...