Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
following code is used to insert 2 different images to a table.But it add the same picture (even though two different pictures are selected).how can we avoid this?any suggestions?
pls help!!!

openConnection();
string qry = "insert into candidates ([NIC],[cand_no],[cand_name],[party(name)],[party_im],[name_im]) values (@nic,@no,@nm,@pty,@ImageData1,@ImageData2)";
                   
SqlCom = new SqlCommand(qry, conn);                    
SqlCom.Parameters.Add(new SqlParameter("@nic", textBox1.Text));
SqlCom.Parameters.Add(new SqlParameter("@no", textBox2.Text));
SqlCom.Parameters.Add(new SqlParameter("@nm", textBox3.Text));
SqlCom.Parameters.Add(new SqlParameter("@pty", textBox5.Text));
SqlCom.Parameters.Add(new SqlParameter("@ImageData1", (object)imagedata));
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", (object)imagedata));
                    
SqlCom.ExecuteNonQuery();           
closeConnection();
MessageBox.Show("Succesfully Inserted");


candidates table
NIC	        varchar(50)	
cand_no	        int	
cand_name	nvarchar(50)	
[party(name)]	varchar(50)	
party_im	image	
name_im	        image	

for party_im & name_im fields pictures are taken from a OpenFileDialog, and selected pictures are put in to pictureBox1 and pictureBox2

how can we add these 2 different pictures separately to relevant fields?
pls help me!!!
Posted
Updated 3-Jul-11 10:39am
v3
Comments
RaviRanjanKr 3-Jul-11 16:38pm    
Always use "pre" tag for source code.

Its not easy to say what's going wrong without taking more snaps of your code.
I've some question what is imagedata and what type of formating you are using to store image in database.
As I am assuming you are assigning BLOB formated values to imagedata to store images in database.
if yes then assign two different value named imagedata1 and imagdata2 which will store byte[] value differently for each. and then use given code

byte[] imagedata1 = getimagedata as byte formated for image1
byte[] imagedata2 = getimagedata as byte formated for image2

and then use
MIDL
SqlCom.Parameters.Add(new SqlParameter("@ImageData1", (object)imagedata1)); // Change imagedata to imagedata1
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", (object)imagedata2)); // Change imagedata to imagedata2
 
Share this answer
 
v2
Comments
Wonde Tadesse 3-Jul-11 16:57pm    
My 5.:) I was writing an answer, but you already answered it.
RaviRanjanKr 3-Jul-11 16:59pm    
Thanks :)
Member 7779792 3-Jul-11 17:44pm    
thank you very much.appreciate your help! :))))
RaviRanjanKr 4-Jul-11 1:34am    
Its my pleasure :)
You must use two different variable to hold the selected images.
MIDL
SqlCom.Parameters.Add(new SqlParameter("@ImageData1", (object)imagedata));
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", (object)imagedata));

must be
MIDL
SqlCom.Parameters.Add(new SqlParameter("@ImageData1", (object)imagedata1));
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", (object)imagedata2));

The rest of the code is valid to me.
 
Share this answer
 

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