3

I have two tables in a SQL Server database, product and innvoice.

I am fetching selected data from table product and showing them in datagridview.

Now I want to store data from datagridview to table innvoice. This all operation takes on one button click.

Here is my code:

private void button5_Click_2(object sender, EventArgs e) //add produt
{
        SqlConnection con = new SqlConnection(@"Persist Security Info=False;User ID=usait;password=123;Initial Catalog=givenget;Data Source=RXN-PC\ROSHAAN");
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("SELECT product.p_name,product.p_category, product.sale_price FROM product where p_code='" + textBox16.Text + "'", con);
        DataTable dt = new DataTable();

        da.Fill(dt);
        dataGridView3.DataSource = dt;

        // here I want to insert values from datagridview3 to table innvoice.
}
4
  • You need an insert query. For goodness sake do a search on SO.. Commented Jun 16, 2013 at 9:11
  • 2
    It's Invoice - one "n" only. And you should stop concatenating together your SQL statements - that's just inviting hacker to exploit your SQL injection weakness! Use parametrized queries - always. Commented Jun 16, 2013 at 9:15
  • Instead of dataGridView, you can insert record from your dt. Commented Jun 16, 2013 at 10:01
  • I am not sure what the schema structure of your two tables looks like but you would be better off it you used an Id off the products table to act as a foreign key in your invoice table. This way the invoice table just has the invoice specific information and you are not duplicating information all over the place. Commented Jun 26, 2013 at 2:55

1 Answer 1

2

Please make a note.Using direct SQL query is not a good practise.

Hope this will help you.

 private void button5_Click_2(object sender, EventArgs e) //add produt
    {
            SqlConnection con = new SqlConnection(@"Persist Security Info=False;User ID=usait;password=123;Initial Catalog=givenget;Data Source=RXN-PC\ROSHAAN");
            Dataset ds=new Dataset();
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT product.p_name,product.p_category, product.sale_price FROM product where p_code='" + textBox16.Text + "'", con);
            da.Fill(ds);
            dataGridView3.DataSource = ds;

            //Code to insert values into Invoice
            for( i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
               sqlcommand cmd=new sqlcommand();
               cmd=new sqlcommand("insert into invoice(pname,pcategory,psaleprice) values('"+ds.Tables[0].Rows[i]["product.p_name"].ToString()+"','"+ds.Tables[0].Rows[i]["product.p_category"].ToString()+"','"+ds.Tables[0].Rows[i]["product.sale_price"].ToString()+"')",con);
               cmd.executeNonquery();
            }
            con.close();

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