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

SqlConnection sq = new SqlConnection(connectionString);
                SqlCommand cm;
                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);
Updated 17-May-13 16:33pm
[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     CRLF
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 :
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:
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