Click here to Skip to main content
15,909,829 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What i am trying to do is on button click i want to store some selected data from one table into other table
Posted
Comments
Maciej Los 10-Apr-14 1:38am    
OK, but what have you tried? Where are you stuck? What kind of issue do you have?
jayraj86 10-Apr-14 1:56am    
I have tables
1.Users
2.AppliedUsers

in AppliedUsers table I have few 6-7 columns which are same as in Users table. As I want those values from Users table and store those values in this new AppliedUsers table.

I have stored UserID (from users table) value in session, the thing I am doing here is on button click i want to store selected values from Users into AppliedUsers table where UserId=session[userid]. The user is applying for job on button click and i want to store the data of that user who applied into this new AppliedUsers table from Users table.

SQL
SELECT *
INTO carcopy2 ////this is table where you will insert
FROM carcopy;/////this is from whre you are retrieving
 
Share this answer
 
protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("connection string");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from Users where UserID='"+Session["userid"].ToString()+"'",con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                SqlCommand cmd2 = new SqlCommand("insert into AppliedUsers values('" + dt.Rows[0][0].ToString() + "','" + dt.Rows[0][1].ToString() + "',......) ", con);
                cmd2.ExecuteNonQuery();
            }
            con.Close();
        }
 
Share this answer
 
v3
Comments
jayraj86 10-Apr-14 2:08am    
Appreciate your answer Raajkumar,
how can I write insert query here ?
INSERT INTO UsersApplied (UserID,FirstName)Values (???)
what should i write in values ??
Raajkumar.b 10-Apr-14 2:12am    
("insert into UserApplied(UserID,FirstName) values('" + dt.Rows[0][0].ToString() + "','" + dt.Rows[0][1].ToString() + "')",con);
jayraj86 10-Apr-14 2:16am    
qlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");

con.Open();
SqlCommand cmd = new SqlCommand("select UserID,FirstName from Users where UserID='" + Session["UserID"].ToString() + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
SqlCommand cmd2 = new SqlCommand("insert into UsersApplied values('" + dt.Rows[0][0].ToString() + "','" + dt.Rows[0][1].ToString() + "',......) ", con);
cmd.ExecuteNonQuery();
}
con.Close();


I applied this code but i m not getting any data in AppliedUsers table. why ?
Raajkumar.b 10-Apr-14 2:19am    
SqlCommand cmd2 = new SqlCommand("insert into UsersApplied values('" + dt.Rows[0][0].ToString() + "','" + dt.Rows[0][1].ToString() + "') ", con);


remove .... in insert query
Raajkumar.b 10-Apr-14 2:19am    
r u getting any error

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