image storing problem in db

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; }
}
}
[/code]

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())
{
[color=Red]item.Photo = (byte[])(dr["Picture"]);[/color][size=2][/size]
}
}
finally
{
if (dr != null) dr.Close();
con.Close();
}
return item.Photo;

}
}

[/code]
[code]
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)
{
[color=Red] itemDal.GetPicture();
MemoryStream ms = new MemoryStream(itm.Photo);
this.pbxItem.Image = Image.FromStream(ms);[/color][size=3][/size]
}
}

[/code]

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.

Comments

  • 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`~
  • reply it now.
  • 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`~
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories