0

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 ! ......

7
  • 3
    call that code when you click the button? Commented Oct 20, 2017 at 8:05
  • Might be good to know what sort of parameters we are talking about too. How would you be filtering on the items table? Commented Oct 20, 2017 at 8:06
  • @geostocker i have a textbox1 to filter the data."WHERE item_id='"+textbox1.text+"' " Commented Oct 20, 2017 at 8:11
  • 1
    Your code is vulnerable to SQL injections, try using parameters Commented Oct 20, 2017 at 8:13
  • I'd suggest setting up a stored procedure that does all of that for you. 1. get the row from item based on query and simply insert the result into your cart table. Then display the cart based on a view or something. Should be fairly simple. Set up the stp and then when that call has been finished you do a seperate call to the view (or all in the same stp). :( Commented Oct 20, 2017 at 8:20

1 Answer 1

2
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
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.