Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What i am needing is a student register himself and during registration he can apply for number of subjects with their own individual details.I have created stud_Table, Subject_Table and Temp_Subject_Table.During registration subjects opted an their details are stored in Temp_Subject_Table.When user click on register then i copied the subjects into Subject_Table using
C#
"INSERT INTO Subject_Table SELECT * FROM Temp_Subject_Table"
...but not working?
Is anyone having better way of achieving this...
Thank's in advance.......
Posted
Updated 31-Mar-14 5:36am
v2
Comments
Jawad Ahmed Tanoli 31-Mar-14 11:44am    
hi what you want to do is that when student register his/her self and select multiple subjects you want to save selected subjects with student record?
Pawan Wagh 31-Mar-14 14:25pm    
Yes...But student and subjects table must be separate....
Student adds the subjects required for him before registration and it is stored in temp table....
after registration they are moved to subjects table

1 solution

Please, visit this link: Adding Rows by Using INSERT and SELECT[^]

You should specify the list of columns and the data types should be equal.
 
Share this answer
 
Comments
Pawan Wagh 31-Mar-14 14:27pm    
Tried this earlier but not working.data is inserted in temp table but not in subject table after "INSERT INTO SELECT" is performed......
Maciej Los 31-Mar-14 14:33pm    
What have you tried? Have you tried to specify columns? Does the data types are equal in both tables? Please, be more specific and provide more details if you want our help.
Pawan Wagh 2-Apr-14 14:09pm    
both tables are having same no of columns and datatypes....What i did is---
"INSERT INTO Subject_Table SELECT * FROM Temp_Subject_Table WHERE Stud_Name='@Stud_Name'"..
but not working..
My Code Segment
protected void StudReg_Button_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
con.Open();
SqlTransaction tran = con.BeginTransaction();
try
{

SqlCommand cmd = new SqlCommand();
cmd.Transaction = tran;
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO SUBJECTS_TBL SELECT * FROM TEMP_SUBJECT_TBL WHERE Stud_Name='@Stud_Name'";
cmd.Parameters.Add("@Stud_Name", System.Data.SqlDbType.VarChar).Value = this.Session["Stud_Name"].ToString().ToUpper();
cmd.CommandText = "DELETE FROM TEMP_SUBJECT_TBL WHERE Stud_Name='@Del_Stud_Name'";
cmd.Parameters.Add("@Del_Stud_Name", System.Data.SqlDbType.VarChar).Value = this.Session["Stud_Name"].ToString().ToUpper();
cmd.ExecuteNonQuery();
tran.Commit();
}
catch (Exception ex)
{
tran.Rollback();
string error_message = ex.Message;
Response.Write("<script LANGUAGE='JavaScript'>alert('" + error_message + "')</script>");
}
finally
{
con.Close();
}
}

}

It's just deleting not inserting............
Maciej Los 2-Apr-14 14:17pm    
Command text is wrong! You haven't visited the link i had provided! Please, read related article!
Pawan Wagh 2-Apr-14 14:45pm    
Thanks for you help but CommandText is not wrong....
I just forget to perform cmd.ExecuteNonQuery() after adding parameter for "INSERT INTO SELECT" query...
Now it's working fine...
Thank you......

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