Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How to add multiple images into a table? Here test_table table which has two nvarchar fields (name, party) and two image fields(party_im,name_im). This code does not cause for any errors but data is not inserted in to the table?can any one please find what is wrong with the code?

Please help me.

C#
string dbconnstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\compaq\Desktop\new\n1\ClassLibrary1\Database1.mdf;Integrated Security=True;User Instance=True";

    SqlConnection conn = new SqlConnection(dbconnstr);                    
    string qry = "insert into test_table (name,party,party_im,name_im) values (textBox1.Text,textBox2.Text,@ImageData1,@ImageData2)";

    //Initialize SqlCommand object for insert.
    SqlCommand SqlCom = new SqlCommand(qry, conn);

    SqlCom.Parameters.Add(new SqlParameter("@ImageData1", (object)imagedata));
    SqlCom.Parameters.Add(new SqlParameter("@ImageData2", (object)imagedata));
    n = -1;

    //Open connection and execute insert query.
    conn.Open();
    n = SqlCom.ExecuteNonQuery();
                    
    conn.Close();
    conn.Dispose();
    MessageBox.Show("Succesfully Inserted");
Posted
Updated 30-May-11 3:54am
v3

1 solution

Let me guess: the table fields name and party contain the text "textBox1.Text" and "textBox2.Text"!

That is because
SqlConnection conn = new SqlConnection(dbconnstr);
     string qry = "insert into test_table (name,party,party_im,name_im) values (textBox1.Text,textBox2.Text,@ImageData1,@ImageData2)";
Hard codes the names of the textboxes into the SQL string, rather than the contents. Try:
SqlConnection conn = new SqlConnection(dbconnstr);
string qry = "insert into test_table (name, party, party_im, name_im) values (@NM, @PT, @ImageData1, @ImageData2)";

//Initialize SqlCommand object for insert.
SqlCommand SqlCom = new SqlCommand(qry, conn);

SqlCom.Parameters.Add(new SqlParameter("@NM", textBox1.Text));
SqlCom.Parameters.Add(new SqlParameter("@PT", textBox2.Text));
SqlCom.Parameters.Add(new SqlParameter("@ImageData1", imagedata));
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", imagedata));
Though why you are insertingthe same data to your table twice ("imagedata") I don't know...
 
Share this answer
 
Comments
Member 7779792 30-May-11 10:05am    
thank you very much!!! it actually works.thanx alot!!!
OriginalGriff 30-May-11 11:07am    
Welcome!
Member 7779792 3-Jul-11 16:00pm    
in this code,even though we insert 2 different pictures to picture boxes, it adds only one picture to the both fields(party_im, name_im)
how can we add these 2 different images to the table?how this code should change?
OriginalGriff 4-Jul-11 3:36am    
It's a bit obvious!
SqlCom.Parameters.Add(new SqlParameter("@ImageData1", imagedata));
SqlCom.Parameters.Add(new SqlParameter("@ImageData2", imagedata));
"imagedata" is the same for both fields - change one of them to refer to the other image. (I did mention that when I answered, over a month ago...)

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