Home All Groups Group Topic Archive Search About
Author
27 Nov 2004 12:13 AM
Jeronimo Bertran
I am using forms databinding to show an image in the database in a
pictureBox.  To do this I am handling the Format event to convert the byte
[] to a bitmap:



Binding bind = new System.Windows.Forms.Binding("Image", this.dataSet1,
"Employee.Image");
bind.Format += new ConvertEventHandler(Image_Format);


private void Image_Format(object sender, ConvertEventArgs e)
{
    if (e.Value is System.DBNull)
    {

                ///   How do we handle the DBNull???
        return;
    }


    // e.Value is the original value
    Byte[] img = (Byte[]) e.Value;

    MemoryStream ms = new MemoryStream();
    ms.Write(img, 0, img.Length);
        Bitmap bmp = new Bitmap(ms);
    ms.Close();

        // Writes the new value back
    e.Value = bmp;
}


My problem occurs when the value in the database is NULL.    if I don't do
anything then I get an Invalid cast from System.DBNull to
System.Drawing.Image.   I also tried :

e.Value = (Bitmap) null;

but i get a memory exception

Thanks

Jeronimo

Author
29 Nov 2004 4:50 AM
Sijin Joseph
Have you tried setting e.Value = null ?

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

Jeronimo Bertran wrote:
Show quote
> I am using forms databinding to show an image in the database in a
> pictureBox.  To do this I am handling the Format event to convert the byte
> [] to a bitmap:
>
>
>
> Binding bind = new System.Windows.Forms.Binding("Image", this.dataSet1,
> "Employee.Image");
> bind.Format += new ConvertEventHandler(Image_Format);
>
>
> private void Image_Format(object sender, ConvertEventArgs e)
> {
>     if (e.Value is System.DBNull)
>     {
>
>                 ///   How do we handle the DBNull???
>         return;
>     }
>
>
>     // e.Value is the original value
>     Byte[] img = (Byte[]) e.Value;
>
>     MemoryStream ms = new MemoryStream();
>     ms.Write(img, 0, img.Length);
>         Bitmap bmp = new Bitmap(ms);
>     ms.Close();
>
>         // Writes the new value back
>     e.Value = bmp;
> }
>
>
> My problem occurs when the value in the database is NULL.    if I don't do
> anything then I get an Invalid cast from System.DBNull to
> System.Drawing.Image.   I also tried :
>
> e.Value = (Bitmap) null;
>
> but i get a memory exception
>
> Thanks
>
> Jeronimo
Author
1 Dec 2004 3:35 AM
Jeronimo Bertran
Yes, I tried setting e.Value to null but I later get a memory exception.

Sijin Joseph <sijinNOSPAMdotnet@hotmail.com> wrote in
Show quote
news:ubMnn7c1EHA.936@TK2MSFTNGP12.phx.gbl:


> Have you tried setting e.Value = null ?
>
> Sijin Joseph
> http://www.indiangeek.net
> http://weblogs.asp.net/sjoseph
>
Author
1 Dec 2004 6:35 PM
Tampa .NET Koder
You could create a 1px gif image that would become the placeholder for that
image box if the field is null.  Or, create a standard image file that a user
can see if there is not image avaliable.  Use either case as the image source
for the picture box


Show quote
"Jeronimo Bertran" wrote:

> I am using forms databinding to show an image in the database in a
> pictureBox.  To do this I am handling the Format event to convert the byte
> [] to a bitmap:
>
>
>
> Binding bind = new System.Windows.Forms.Binding("Image", this.dataSet1,
> "Employee.Image");
> bind.Format += new ConvertEventHandler(Image_Format);
>
>
> private void Image_Format(object sender, ConvertEventArgs e)
> {
>     if (e.Value is System.DBNull)
>     {
>
>                 ///   How do we handle the DBNull???
>         return;
>     }
>
>
>     // e.Value is the original value
>     Byte[] img = (Byte[]) e.Value;
>
>     MemoryStream ms = new MemoryStream();
>     ms.Write(img, 0, img.Length);
>         Bitmap bmp = new Bitmap(ms);
>     ms.Close();
>
>         // Writes the new value back
>     e.Value = bmp;
> }
>
>
> My problem occurs when the value in the database is NULL.    if I don't do
> anything then I get an Invalid cast from System.DBNull to
> System.Drawing.Image.   I also tried :
>
> e.Value = (Bitmap) null;
>
> but i get a memory exception
>
> Thanks
>
> Jeronimo
>

AddThis Social Bookmark Button