C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
image storing problem in db Posted by maifs on 29 May 2009 at 4:55 AM
i am trying to store a image in sqlserver database and retrieve the image from it.

 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.

 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"]);

Red lines are error lines, read them.
Report
Re: image storing problem in db Posted by Psightoplazm on 29 May 2009 at 11:51 PM
That's a whole lot of code you posted there - can you be a little more specific with where your trouble spot is and then put it in code tags so it's easier to read please?
></\/~Psightoplasm`~
Report
Re: image storing problem in db Posted by maifs on 30 May 2009 at 3:00 AM
reply it now.
Report
Re: image storing problem in db Posted by Psightoplazm on 30 May 2009 at 9:09 AM
Okay - so a couple things.

I know, I know, that people have fallen in love with stored proceedures - but these are in all actuallity a horrible practice.

#1 you are putting logic in your data layer.
#2 you can't debug the stupid things very easily.

Stored proceedures were introduced as a result of databases not being terribly friendly or efficient at adhoc sql -

But this is no longer the case! writing your sql inside of your logic is every bit as efficient and way more friendly than your stored proceedures.

I am saying all of this only because it is difficult to know if your image was ever actually stored to the database. There are a million situations in which a stored proceedure will fail and never even alert anyone that anything was wrong.

Your code appears to be accessing the field properly - as long as "Picture" is in fact the name of the returned field. it also appears to be inserting the data properly

the SqlDbType.Image requires that the field in the db is actually an IMAGE type - is this the case?

also can you check that there is infact data in this field of the entry you are retreiving?
></\/~Psightoplasm`~



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.