Click here to Skip to main content
15,918,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using NTier Architecture small project. I want picture to display in gridview from database. I am using Northwind database. here is my code.
BO.cs
C#
public class CAt : ICat
    {
        int _categoryid; string _categoryname; string _description; byte _image;
        public int categoryid
        {
            get
            {
                return _categoryid;
            }
            set
            {
                _categoryid = value;
            }
        }

        public string categoryname
        {
            get
            {
                return _categoryname;
            }
            set
            {
                _categoryname = value;
            }
        }

        public string description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
            }
        }

        public byte image
        {
            get
            {
                return _image;
            }
            set
            {
                _image = value;
            }
        }
    }
}

in DAL.cs
C#
public List<cat> GetCategories()
        {
            List<cat> lst = new List<cat>();
            cn = new SqlConnection(con);
            string query = "Select Categoryid, categoryname, description, picture from Categories";
            cmd = new SqlCommand(query, cn);
            cn.Open();
            dr = cmd.ExecuteReader();
            try
            {
                while (dr.Read())
                {
                    CAt c = new CAt();
                    c.categoryid = (int)dr["categoryid"];
                    c.categoryname = dr["categoryname"].ToString();
                    c.description = dr["description"].ToString();
                    c.image = Convert.ToByte(dr["picture"]);
                    lst.Add(c);
                }
                return lst;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally 
            {
                cn.Close();
            }
        }


here i am getting error like this.
Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.

please help me
thank you.
Posted
Updated 29-May-12 8:19am
v2
Comments
Sergey Alexandrovich Kryukov 29-May-12 13:51pm    
In what line of code?
--SA

1 solution

First of all, your exception handling makes no sense: catching and re-throwing the same exception can be done using throw without the exception instance and makes no sense in your case, as this is all your do; this is equivalent to not handling exceptions at all, which is the best method of handling exceptions; instead, you should catch them all only at the very top of the stack of each thread. Just use try-finally without catch.

Now, the exception tells you the exact problem. You are trying to convert object to a single byte, as this is how you defined CAt.image. First, your object does not implement IConvertible, but it would not make any sense at anyway, because what would be the sense of "converting" byte[] to a single byte? And please don't ask "what to do?", as you did not explained to purpose of this strange thing.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900