Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Complex problem with binding, UserControl & null
Message
De
11/08/2008 10:25:42
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01336725
Message ID:
01338029
Vues:
14
This message has been marked as a message which has helped to the initial question of the thread.
I'm not sure which line it's crashing on, but all you need to do is have a check to see if it's == DBNull.Value and do something else with it when it is.

~~Bonnie




>Hi everybody,
>
>Here is the code I'm trying for my UserControl for DefaultPicture property
>
>
>[Bindable(true, BindingDirection.TwoWay), Category("User-Defined"), Description("Default Picture")]
>public byte[] DefaultPicture
>{
>    get
>    {
>       // We need to convert picture to byte array
>        Byte[] imgByte = null;
>        try
>        {
>
>            if (this.myPic.HasFile && this.myPic.PostedFile != null)
>            {
>                if (this.myPic.FileBytes.Length > 10240) // exception case
>                    throw new Exception("Image File was is too big: " +
>                            (this.myPic.FileBytes.Length / 1024).ToString() + " kB.");
>                else
>                    imgByte = this.myPic.FileBytes;
>            }
>        }
>        catch (Exception e)
>        {
>            throw (e);
>            //exception handled
>        }
>       return imgByte ;
>    }
>
>  set
>    {
>        if (this.pnlImage.Visible == true)
>        {
>        //Here we need to set picture using JavaScript - ClientScriptManager
>        string Request_PhysicalPath = Request.PhysicalPath;
>        string Request_FilePath = Request.FilePath;
>        string dir = Request_PhysicalPath.Substring(0, (Request_PhysicalPath.Length - Request_PhysicalPath.Split('\\')[Request_PhysicalPath.Split('\\').Length - 1].Length));
>
>        string[] delFiles = Directory.GetFiles(dir, "*aspx*" + Profile.UserName + "*.jpg");
>        for (int i = 0; i < delFiles.Length; i++)
>        {
>            File.Delete(delFiles[i]);
>        }
>
>        string imgName = this.PersonID + Profile.UserName + ".jpg";
>        if (value == null || value.ToString()  =="")
>        {
>            String dImg = dir + "icon_no_photo_80x80.gif";
>            File.WriteAllBytes(Request_PhysicalPath + imgName, (byte[])File.ReadAllBytes(dImg));
>        }
>        else
>        {
>            File.WriteAllBytes(Request_PhysicalPath + imgName, value);
>        }
>        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowPic", "previewImage('" + Request_PhysicalPath + imgName + "')", true);
>          //  picresult.src = Request_PhysicalPath + imgName;
>    }
>}
>
>And in my page I try
DefaultPicture = '< %# Bind("DefaultPicture") % >'
>
>However, it gives me unhandled CAST exception when trying to convert DbNull to byte array.
>
>How else can I handle this problem? Can I remove SET portion and create a public method instead and call this method to set this property? Or make set to be private and again use some public method?
>
>Any ideas?
>
>Thanks in advance.
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform