Click here to Skip to main content
14,871,989 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,

Here I am using a code for submit button with the help of this button I want to insert records in two table
1.customer
2.order

Here insert records for customer by text box and product name using by drop down list and rate comes from database I insert quantity from text box on the basis of rate that will calculate and make total_amount and insert records in order table I am using following code.
This code updating only single table

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SqlConnection con=new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
            SqlCommand cmd;
            DataSet ds=new DataSet();
          
            try
            {
                cmd = new SqlCommand("insert into customer(customer_name,customer_phone,customer_address)values(@customer_name,@customer_phone,@customer_address)", con);
                //cmd.Parameters.Add("@product_name", SqlDbType.NVarChar).Value = DropDownList1.Items.ToString();
                cmd.Parameters.Add("@customer_name", SqlDbType.NVarChar).Value = txtName.Text;
                cmd.Parameters.Add("@customer_phone", SqlDbType.NVarChar).Value = TxtPhone.Text;
                cmd.Parameters.Add("@customer_address", SqlDbType.NVarChar).Value = txtAddress.Text;
                
                cmd = new SqlCommand("insert into product_order(product_name,product_quantity,product_amount)values(@product_name,@product_quantity,@product_amount)", con);
                cmd.Parameters.Add("@product_name", SqlDbType.NVarChar).Value = DropDownList1.SelectedItem.Text;
                cmd.Parameters.Add("@product_quantity", SqlDbType.NVarChar).Value = txtqty.Text;
                cmd.Parameters.Add("@product_amount", SqlDbType.NVarChar).Value = txtAmount.Text.ToString();
                con.Open();
                cmd.ExecuteNonQuery();
                Response.Write("yr records updated..");
                //da = new SqlDataAdapter("select * from product", con);
                //GridView1.DataSource = ds;
                //GridView1.DataBind();
                cmd.Dispose();
             }
            catch(Exception exe)
            {
                Response.Write(exe);
            }
          
            ds.Dispose();
            con.Close();
            
            
        }
Posted
Updated 13-Sep-10 3:25am
v2

You have one cmd object and after you create the customer you get rid of it before you insert the record with your next cmd = new SqlCommand command.

You have to call cmd.ExecuteNonQuery(); before you set up your command to insert the order.

Cheers.
   
Comments
R. Giskard Reventlov 13-Sep-10 9:40am
   
Must learn to refresh! I'll leave my answer there but you should get any points.
TheyCallMeMrJames 13-Sep-10 9:43am
   
lol...no worries, good call on the transaction. cheers.
Quick answer: you need to put another cmd.ExecuteNonQuery() prior to the 2nd cmd = new SqlCommand(). You should also wrap this in a transaction. That should get you going.
   
Use two Different Command Like

SQL
SqlCommand cmd = new SqlCommand();
-- assign first Query to this command

SqlCommand cmd1 = new SqlCommand();
-- assign second Query to this command

then use
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();

Thanks,
   

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