Click here to Skip to main content
15,918,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to store Image datatype in my database but not able to do this


Here is My code....

Applicant aa = new Applicant(); //Applicant is my class containing all private and public members for items in database like name,add,ph,Image
foreach (DataGridViewRow dgvr in dataGridView.SelectedRows)
{

DataRowView drv = (DataRowView)dgvr.DataBoundItem;
aa.m_dr = drv.Row;
aa.ID = (int)bb.m_dr["ID"];
aa.name = bb.m_dr["Name"].ToString();
aa.Foto = (Image)bb.m_dr["Foto"]; //After this line comes error Systen.DBNull kann not convert to System.Drawing.Image.
How to over come Problem with Null value Error
Posted
Comments
Zoltán Zörgő 22-Jan-15 4:09am    
Supposing you have data in that field you can not simply cast it to image.
User-8621695 22-Jan-15 4:43am    
convert image into base64 then store it into database with image datatype.

1 solution

Validate the object before casting it to another type

C#
if( bb.m_dr["Foto"] != null && bb.m_dr["Foto"] != System.DBNull.Value ) // add this line
aa.Foto = (Image)bb.m_dr["Foto"];
 
Share this answer
 
Comments
Member 11000455 22-Jan-15 6:12am    
already tried this one but same error cannot convert System.Byte[] in Type System.Drawing.Image.
Karthik_Mahalingam 22-Jan-15 6:29am    
what is the datatype of Foto colum in DB and in c# ?
Member 11000455 22-Jan-15 8:54am    
May be this information helps you my datatype is Image

I got a windows form with different Tab control connected with my database
i am able to store and reteive all values like name ,age ,DOB but i am having problem with storing pictire and retreiving it..
So far with storing to database work fine
i can see after select a picture and load datagrid view i can see my picture,
Here is my code for doing it------

// foreach (DataRow row in dataset.Tables[1].Rows)
{
row[Name] = textboxName.Text; //Values from my Tabcontrol Text box

row["Dob"] = datetimepickerDob.value;;

//Now for storing image
if (picFoto.Image != null)
{
Bitmap bmp = new Bitmap(picFoto.Image);
System.IO.MemoryStream imgMemoryStream = new System.IO.MemoryStream();
imgMemoryStream.ToArray();
bmp.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imgByteData = imgMemoryStream.GetBuffer();

row["Foto"] = imgByteData;
}

Now after this when i go bach ko my gridview which contains all table data i can see photo there...Ok
Now when i want to load this tabcontrol i want to load also this photo in my picturebox.
with other data types its perfectly ok but i am having problem on loading my pic
Here is my Code

private Image m_iFoto = null;

public Image Foto
{
get
{
return m_foto; //May be here i neew to convert to ByteArray to image
}
set { m_foto = value; }
}
Applicant aa = new Applicant();
//Apllicant is my class containing all member and properties
//for column items.

foreach (DataGridViewRow dgvr in dataGridView.SelectedRows)
{

DataRowView drv = (DataRowView)dgvr.DataBoundItem;

aa.m_dr = drv.Row;
aa.Name = bb.m_dr["Name"].ToString();
if (aa.m_dr["Foto"] != DBNull.Value)
{

//MemoryStream ms = new MemoryStream();
//Image returnImage = Image.FromStream(ms);
aa.Foto = (Image)bb.m_dr["Foto"];//After this line i got error message cannot convert System.Byte[] to System.Drawing.Image
}
Member 11000455 22-Jan-15 7:47am    
Image

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