Click here to Skip to main content
15,789,698 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//code for saving image into sql server 2008 r2//////
byte[] img = null;
FileStream fs = new FileStream(imgLoc,FileMode.Open,FileAccess.Read);
BinaryReader br=new BinaryReader(fs);
img = br.ReadBytes((int)fs.Length);

string conn = "data source=.;Initial catalog=RMSDB;user=sa;password=ibs;";
SqlConnection con = new SqlConnection(conn);
string cmdd = String.Format("INSERT INTO Employees (Emp_Pic_ImageData) VALUES('{0}')", @img);

SqlCommand cmd = new SqlCommand(cmdd, con);

cmd.Parameters.Add(new SqlParameter("@img",img));
int tempss = cmd.ExecuteNonQuery();

//code for retrival of image from sql server////////////

string sql = String.Format("Select Emp_Pic_ImageData From Employees where Emp_Id='{0}'", TxtBoxId.Text);
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
byte[] img = (byte[])(reader[0]);
if (img == null)
PicboxEmployee.Image = null;
MemoryStream mstrm = new MemoryStream(img);
PicboxEmployee.Image = new System.Drawing.Bitmap(mstrm); //there is error of parameter is not valid.


MessageBox.Show("this not exists");


1 solution

No, that won't work.
It may seem to, but it doesn't do what you think at all.
See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - explains the problem and solution.
Share this answer
[no name] 13-Sep-13 3:27am    
could you please send me the complete code about insertion and retrieval of image from sql server,please
OriginalGriff 13-Sep-13 3:41am    
Did you bother to read the article I linked to?
[no name] 13-Sep-13 3:28am    
i have tried above article suggestions but i am facing error there
OriginalGriff 13-Sep-13 3:42am    
This is not very helpful - what error are you facing?
[no name] 13-Sep-13 4:02am    
i am facing error on FormStream line that image is not found at this location,as i have pasted there own location of image

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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