Click here to Skip to main content
16,009,238 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I want to extract admission_no from users but I don't want the phone_no and merge with admission_no with all the data from test. After that I want to insert them into one table which is the merge table.
Example Tables
users
admission_no,phone_no
testo
subject_id,subject_name,admission_no,date,time,venue,seat_no
merge
subject_id,subject_name,admission_no,date,time,venue,seat_no
HOW AM I SUPPOSE TO DO THAT IN C#?PLEASE HELP.ITS REALLY URGENT.
BELOW IS MY CURRENT CODES..
protected void Button1_Click(object sender, EventArgs e)
   {
     SqlConnection conn = new SqlConnection("Data Source= EN12-2-20-PC01\\SQLEXPRESS;" + "Initial Catalog=ssms;Integrated Security=SSPI");
     SqlDataAdapter adapSel;

     conn.Open();
     foreach (GridViewRow gvr in GridView2.Rows)   //loop through GridView
     {
       string viewqry = "INSERT INTO merge(subject_id,subject_name,admission_no,date,time,venue,seat_no) " + "(SELECT U.admission_no,T.subject_name,T.date,T.time,T.venue,T.seat_no " + "From users As U INNER JOIN testo As T ON U.admission_no = T.admission_no) VALUES ('" + gvr.Cells[0].Text + "','" + gvr.Cells[1].Text + "','" + gvr.Cells[2].Text + "','" + gvr.Cells[3].Text + "','" + gvr.Cells[4].Text + "','" + gvr.Cells[5].Text + "','" + gvr.Cells[6].Text + "')";
       adapSel = new SqlDataAdapter(viewqry, conn);

       DataSet dsSel = new DataSet();
       adapSel.Fill(dsSel);
       GridView2.DataSource = dsSel;
       GridView2.DataBind();
       conn.Close();

    }
Posted
Updated 25-May-11 17:52pm
v2
Comments
fjdiewornncalwe 25-May-11 23:55pm    
It is of no concern to us that it is really urgent to you. We all volunteer out time here to help you and we will do so in our good time. Your question seems a little jumbled to me and I can't make out exactly what you are trying to accomplish. Perhaps you could clarify what you have now, and what you want to get to so that we can help with the code to do so.
Cheers.

What is wrong with my codes?
Many!

To start with:
1. You are not using parameterized query
2. You are setting gridview source again and again inside the foreach loop that does not make any sense
3. Gridview datasource needs to be a datatable and you set a dataset

Last but not the least, do make sure that the query you are using works. Just put the raw query in SQL and see if it is ok and then move ahead.
 
Share this answer
 
Your query seems to be so wrong.
You are trying to use -
INSERT INTO table (column1, column2, column3,...) SELECT column1, column2, column3,... FROM othertable VALUES (value1, value2, value3,...)
which is not valid at all.

You can use one of following.
SQL
INSERT INTO table VALUES (value1, value2, value3,...)
INSERT INTO table (column1, column2, column3,...) VALUES (value1, value2, value3,...)
INSERT INTO table SELECT column1, column2, column3,... FROM othertable
INSERT INTO table (column1, column2, column3,...) SELECT column1, column2, column3,... FROM othertable
 
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