storing image

Discussion in 'C#' started by maifs, May 29, 2009.

  1. maifs

    maifs New Member

    Joined:
    May 29, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    i am trying to store a image in sqlserver database and retrieve the image from it.

    Code:
        public class Item
        {
            private int code;
            private string name;
            private float price;
            private Category category;
            private byte[] photo;
    
            public Item()
            {
               
            }
    
            public Item(int code, string name)
            {
                this.Code = code;
                this.Name = name;
            }
    
            public Item(int code, string name, float price) : this(code,name)
            {
                this.Price = price;  
            }
    
            public int Code
            {
                get
                {
                    return this.code;
                }
                set
                {
                    this.code = value;
                }
            }
    
            public string Name
            {
                get
                {
                    return this.name;
                }
                set
                {
                    this.name = value;
                }
            }
    
            public float Price
            {
                get
                {
                    return this.price;
                }
                set
                {
                    this.price = value;
                }
            }
    
            public Category Category
            {
                get
                {
                    return this.category;
                }
                set
                {
                    this.category = value;
                }
            }
    
            public byte[] Photo
            {
                get { return this.photo; }
                set { this.photo = value; }
            }
        }
    
    picture is stored in db but when i try to retrieve it,it returning null reference.

    Code:
     public class ItemsDAL
        {
            private string selectItemByCode = "Select * from ItemsWithCategory where code=@code";
            private string selectAllItems = "Select * from ItemWithCategory"; // View
            private string insertItem = "InsertImage";
            private string selectImage = "Select Picture from Items where code =107";
    
            public void InserItem(Item pic)
            {
              
                SqlConnection con = new SqlConnection(DALHelper.ConnectionString);
                SqlCommand cmd = new SqlCommand(this.insertItem,con);
               
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = this.insertItem;
                cmd.Parameters.Add(new SqlParameter("@photo", SqlDbType.Image));           
               
                con.Open();
                try
                {
                   cmd.Parameters["@photo"].Value = pic.Photo;
                   cmd.ExecuteNonQuery();
               
                }
                finally
                {
                   
                    con.Close();
                }
              
    
            }
    
            public byte[] GetPicture()
            {
                SqlConnection con = new SqlConnection(DALHelper.ConnectionString);
                SqlCommand cmd = new SqlCommand(this.selectImage, con);
                //cmd.Parameters.Add(new SqlParameter("@code", code));
           
                Item item = null;
                SqlDataReader dr = null;
                con.Open();
                try
                {
                   
                    dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        item.Photo = (byte[])(dr["Picture"]);
                    }
                }
                finally
                {
                    if (dr != null) dr.Close();
                    con.Close();
                }
                return item.Photo;
    
            }
    }
    
    
    
    
      public partial class ManageItemsForm : BaseForm
        {
            Item itm;
            ItemsDAL itemDal = new ItemsDAL();
           
            MemoryStream mem = new MemoryStream();
            public ManageItemsForm()
            {
                InitializeComponent();
                itm = new Item();
                          
            }
    
            private void ManageItemsForm_Load(object sender, EventArgs e)
            {
    
            }
          
            private void button1_Click(object sender, EventArgs e)
            {
                          
                itm.Photo = mem.GetBuffer();
                itemDal.InserItem(itm);
                this.pbxItem.Image.Save(mem, this.pbxItem.Image.RawFormat);
              
            }
    
            private void btnSelectCode_Click(object sender, EventArgs e)
            {
                itemDal.GetPicture();
                MemoryStream ms = new MemoryStream(itm.Photo);           
                this.pbxItem.Image = Image.FromStream(ms);
            }         
        } 
    
    it returns null reference when i try to debug it.
    in this line, it throws an exception

    item.Photo = (byte[])(dr["Picture"]);
     
    Last edited by a moderator: May 29, 2009

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice