Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI
i want to insert record of two tabels in one tabel by inserting querry using asp.net with c# but records are not inserting my code is that


C#
SqlConnection sq = new SqlConnection(connectionString);
                SqlCommand cm;
                sq.Open();
                cm = new SqlCommand("INSERT INTO Result(Semester,TotalMarks,MarksTheory,MarksLab,GPA,Status)(Select student_id,FullName,FatherName,Class,Program FROM Student)) Values('" + Nametxt.Text + "','" + Fathertxt.Text + "','" + DropDownclass.SelectedValue + "','" + DropDownSmster.SelectedValue + "','" + TotalMarkstxt.Text + "','" + Markstheorytxt.Text + "','" + Markslabtxt.Text + "','" + GPAtxt.Text + "','" + DropDownStatus.SelectedValue + "','" + DropDownProgram.SelectedValue + "')", sq);
                cm.ExecuteNonQuery();
                sq.Close();
Posted
Updated 17-May-13 16:33pm
v2
Comments
[no name] 17-May-13 22:38pm    
Would be a lot easier to read if you used parameterized queries instead of trying to create an SQL injection attack.
Sergey Alexandrovich Kryukov 17-May-13 23:04pm    
Aha, now I know: Karachi is in U.S.A. :-)
—SA
Qadri Jillani 18-May-13 0:44am    
aoa ! brother tahir is the structure of all the three tables same or not?

1 solution

1.if the structure of your tables is same and you want to insert every record from both tables than you can use this :
SQL
INSERT INTO table1 (col1,col2,col3,.....,coln) 
     SELECT * FROM table2, table3

2.if you want to insert specific records based on some condition than you can use this:
SQL
INSERT INTO table1 (col1,col2,col3,.....,coln) 
     SELECT * FROM table2, table3 where table2.colname=table3.colname

you can use your own condition in "where" clause
 
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