I want select rows from datagridview and insert into another Table(give the table : Label)

Now my problem is this datagridview contain columns quantity and ItemPerPackage. Quantity column is Total quantity for this item and ItemPerPackage is 1 package can contain how many quantity.

therefore, I want this 1 record insert to Label Table become multiple record. eg. Quantity is 52 and ItemPerPackage is 10. So it must has 6 records and every record quantity is 10 except last record is 2.

I do like this is want print label for paste at item.

my Insert code is like this

SqlCommand cmdCheckID;
            SqlDataReader dtrCheckID;
            

            foreach (DataGridViewRow r in dataGridView1.SelectedRows)
            {


                string PONO = r.Cells[0].Value.ToString();
                string DeliveryDate = r.Cells[1].Value.ToString();
                string Customer = r.Cells[2].Value.ToString();
                string Name = r.Cells[3].Value.ToString();
                string Item = r.Cells[4].Value.ToString();
                string Model = r.Cells[5].Value.ToString();
                string FGrade = r.Cells[6].Value.ToString();
                string Thk = r.Cells[7].Value.ToString();
                string T_Unit = r.Cells[8].Value.ToString();
                string Width = r.Cells[9].Value.ToString();
                string W_Unit = r.Cells[10].Value.ToString();
                string Length = r.Cells[11].Value.ToString();
                string L_Unit = r.Cells[12].Value.ToString();
                string Qty = r.Cells[13].Value.ToString();
                string StockIn = r.Cells[14].Value.ToString();
                string Shortage = r.Cells[15].Value.ToString();
                string ItemPerPackage = r.Cells[16].Value.ToString();
                string Package = r.Cells[17].Value.ToString();
                string Remain = r.Cells[18].Value.ToString();
                Boolean InternalOrder = Convert.ToBoolean(r.Cells[19].Value.ToString());
                Boolean Loading = Convert.ToBoolean(r.Cells[20].Value.ToString());
                Boolean Finished = Convert.ToBoolean(r.Cells[21].Value.ToString());
                Boolean Urgent = Convert.ToBoolean(r.Cells[22].Value.ToString());
                string ReceivedDate = r.Cells[23].Value.ToString();
                string Remark = r.Cells[24].Value.ToString();
                

                //Connectt to Database
                ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
                string name = conSettings.ProviderName;
                string providerName = conSettings.ProviderName;
                string ConnectionString = conSettings.ConnectionString;

                

                
                SqlConnection con = new SqlConnection(ConnectionString);
               

                con.Open();
                

                string strChechID = "select * from Label where PONO='" + PONO + "' and Model='" + Model + "' and FGrade='" + FGrade + "'and Thk='" + Thk + "'and Width='" + Width + "' and Length ='" + Length + "' and Item='" + Item + "'";
                cmdCheckID = new SqlCommand(strChechID, con);
                dtrCheckID = cmdCheckID.ExecuteReader();

               

                SqlCommand comm = new SqlCommand("insert into Label values(@PONO,@DeliveryDate,@Customer,@Name,@Item,@Model,@FGrade,@Thk,@T_Unit,@Width,@W_Unit,@Length,@L_Unit,@Qty,@StockIn,@Shortage,@ItemPerPackage,@Package,@Remain,@InternalOrder,@Loading,@Finished,@Urgent,@ReceivedDate,@Remark)", con);
                SqlCommand comm1 = new SqlCommand("Delete From PDC where PONO=@PONO and Model=@Model and FGrade=@FGrade and Thk=@Thk and Width=@Width and Length=@Length and Item=@Item", con);

                try
                {

                    if (dtrCheckID.HasRows)
                    {
                        MessageBox.Show("Duplicate Data!!!!!!");

                        dtrCheckID.Close();
                    }
                    
                    else
                    {
                        dtrCheckID.Close();

                        comm.Parameters.AddWithValue("@PONO", PONO);
                        comm.Parameters.AddWithValue("@DeliveryDate", DeliveryDate);
                        comm.Parameters.AddWithValue("@Customer", Customer);
                        comm.Parameters.AddWithValue("@Name", Name);
                        comm.Parameters.AddWithValue("@Item", Item);
                        comm.Parameters.AddWithValue("@Model", Model);
                        comm.Parameters.AddWithValue("@FGrade", FGrade);
                        comm.Parameters.AddWithValue("@Thk", Thk);
                        comm.Parameters.AddWithValue("@T_Unit", T_Unit);
                        comm.Parameters.AddWithValue("@Width", Width);
                        comm.Parameters.AddWithValue("@W_Unit", W_Unit);
                        comm.Parameters.AddWithValue("@Length", Length);
                        comm.Parameters.AddWithValue("@L_Unit", L_Unit);
                        comm.Parameters.AddWithValue("@Qty", Qty);
                        comm.Parameters.AddWithValue("@StockIn", StockIn);
                        comm.Parameters.AddWithValue("@Shortage", Shortage);
                        comm.Parameters.AddWithValue("@ItemPerPackage", ItemPerPackage);
                        comm.Parameters.AddWithValue("@Package", Package);
                        comm.Parameters.AddWithValue("@Remain", Remain);
                        comm.Parameters.AddWithValue("@InternalOrder", InternalOrder);
                        comm.Parameters.AddWithValue("@Loading", Loading);
                        comm.Parameters.AddWithValue("@Finished", Finished);
                        comm.Parameters.AddWithValue("@Urgent", Urgent);
                        comm.Parameters.AddWithValue("@ReceivedDate", ReceivedDate);
                        comm.Parameters.AddWithValue("@Remark", Remark);


                        comm1.Parameters.AddWithValue("@PONO", PONO);
                        comm1.Parameters.AddWithValue("@Model", Model);
                        comm1.Parameters.AddWithValue("@FGrade", FGrade);
                        comm1.Parameters.AddWithValue("@Thk", Thk);
                        comm1.Parameters.AddWithValue("@Width", Width);
                        comm1.Parameters.AddWithValue("@Length", Length);
                        comm1.Parameters.AddWithValue("@Item", Item);

                        comm.ExecuteNonQuery();
                        comm1.ExecuteNonQuery();
                        lblMessage.Text = ("Successful Insert!!!");

                        con.Close();

                        timerLoad.Enabled = true;

                    }

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }

        }

Edited 5 Years Ago by lianpiau: n/a

Hi,
sorry, but I dont really understand what you are saying.
Would you please do a bit better explanation to the problem you are facing?
thx in advance.

This article has been dead for over six months. Start a new discussion instead.