简体   繁体   中英

How do i execute 2 sql queries by clicking a button [C#]

I want to copy particular row in items table and copy it into a new table (name- cart ). And display the all records in cart table in a dataGridView . Help !

con = new System.Data.SqlClient.SqlConnection();

        con.ConnectionString = " Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\posdb.mdf;Integrated Security=True";



        string query = "INSERT INTO cart (item_id,Description,Unit_Price,Qty) SELECT item_id,Description,price,'" + txtqty.Text + "' FROM items WHERE item_id = '" + textBox1.Text + "'";
        string qry = "SELECT * FROM cart";

        System.Data.SqlClient.SqlDataAdapter da;
        System.Data.SqlClient.SqlDataAdapter da1;
        da = new System.Data.SqlClient.SqlDataAdapter(query, con);
        da1 = new System.Data.SqlClient.SqlDataAdapter(qry, con);

        ds1 = new DataSet();
        con.Open();
        da1.Fill(ds1,"cart");
        con.Close();
        dataGridView1.DataSource = ds1;
        dataGridView1.DataMember = "cart";

Help ! ......

protected void btn_Click(object sender, EventArgs e)
    {
     insert();
     View();
     }
    private void insert()
    {
      // write insert query 
    }
   private void View()
    {
     // write select query and bind to grid
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM