Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Complex problem with binding, UserControl & null
Message
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Complex problem with binding, UserControl & null
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01336725
Message ID:
01336725
Vues:
53
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.
If it's not broken, fix it until it is.


My Blog
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform