I have a gridview contain 3 record, when I use OleDbDataReader to verify the record is match with data in other Table in access.

My problem is if the record is no match, I will end process and do not perform insert. But when 1st record is match and 2nd no match, the 1st will insert to database and 2nd will stop.But I is either 1 record no match will stop and no record will insert to database.

C# program

Loop through the rows in the gridview individually, checking if each one requires an update to the database. If you need more specific advice post up what code you have done so we can get a better idea of what you need.

protected void btnIntOrder_Click(object sender, EventArgs e)
    {

        OleDbConnection conAuthor;
        SqlConnection conAuthors;

        conAuthor = new OleDbConnection(SideOrderConn);
        conAuthors = new SqlConnection(WMConn);

        OleDbCommand cmdInsert, cmdCheckID;
        OleDbDataReader dtrCheckID;
        SqlCommand cmdUpdate;


        conAuthor.Open();
        conAuthors.Open();


        foreach (GridDataItem selectItem in rgCustPO.SelectedItems)
        {
            //GridDataItem selectItem = (GridDataItem)rgCustPO.SelectedItems[0];
            string Customer = selectItem["cust_name"].Text;
            string Brand = selectItem["brand"].Text;
            string Model = selectItem["model"].Text;
            double WidthS = Convert.ToDouble(selectItem["width"].Text);
            string Width = string.Format("{0:0.##}", WidthS);
            double LengthS = Convert.ToDouble(selectItem["length"].Text);
            string Length = string.Format("{0:0.##}", LengthS);
            double ThicknessS = Convert.ToDouble(selectItem["thickness"].Text);
            string Thickness = string.Format("{0:0.##}", ThicknessS);
            string Sizes = Width + " " + selectItem["width_unit"].Text + " x " + Length + " " + selectItem["length_unit"].Text + " x " + Thickness + " " + selectItem["thickness_unit"].Text;
            string PO_NO = selectItem["cust_pono"].Text;
            string Item_NO = selectItem["cust_poitemno"].Text;
            string Qty = selectItem["qty"].Text;
            string Unit = selectItem["qty_unit"].Text;
            string Dely_Date = selectItem["delivery_date"].Text;

            SideOrderConn = "Insert Into [SideOrder](Cust, PO_NO,  Item_NO, Date_Of_Order, Dept, Brand1, Size1, Qty, Unit,Assembly,Padding,Dely_Date,Status,Remarks,Department)Values(@Cust, @PO_NO,  @Item_NO, @Date_Of_Order, @Dept, @Brand1, @Size1, @Qty, @Unit,@Assembly,@Padding,@Dely_Date,@Status,@Remarks,@Department)";
            string strUpdate = "Update cust_poitem set po_status=@po_status where cust_pono=@cust_pono and cust_poitemno=@cust_poitemno";
            string strChechID = "select * from [Setting] where [Cust1]= '" + Customer + "' and [Brand]='" + Brand + "' and [Model]='" + Model + "' and [Size]='" + Sizes + "'";

            cmdCheckID = new OleDbCommand(strChechID, conAuthor);
            cmdInsert = new OleDbCommand(SideOrderConn, conAuthor);
            cmdUpdate = new SqlCommand(strUpdate, conAuthors);

            dtrCheckID = cmdCheckID.ExecuteReader();


            if (dtrCheckID.HasRows == true)
            {
                while (dtrCheckID.Read())
                {
                    string Dept = Convert.ToString(dtrCheckID["Dept"]);
                    string Brand1 = Convert.ToString(dtrCheckID["Brand1"]);
                    string Size1 = Convert.ToString(dtrCheckID["Size1"]);
                    string QtyS = Convert.ToString(dtrCheckID["Qty"]);
                    string UnitS = Convert.ToString(dtrCheckID["Unit"]);

                    double totalQty = (Convert.ToDouble(QtyS) * Convert.ToDouble(Qty));

                    cmdInsert.Parameters.AddWithValue("@Cust", Customer);
                    cmdInsert.Parameters.AddWithValue("@PO_NO", PO_NO);
                    cmdInsert.Parameters.AddWithValue("@Item_NO", Item_NO); //change access database(SideOrder)> Table(SideOrder)>Field(Item_NO) from number to text
                    cmdInsert.Parameters.AddWithValue("@Date_Of_Order", System.DateTime.Today.ToShortDateString());
                    cmdInsert.Parameters.AddWithValue("@Dept", Dept);
                    cmdInsert.Parameters.AddWithValue("@Brand1", Brand1);
                    cmdInsert.Parameters.AddWithValue("@Size1", Size1);
                    cmdInsert.Parameters.AddWithValue("@Qty", totalQty);
                    cmdInsert.Parameters.AddWithValue("@Unit", UnitS);
                    cmdInsert.Parameters.AddWithValue("@Assembly", 0);
                    cmdInsert.Parameters.AddWithValue("@Padding", 0);
                    cmdInsert.Parameters.AddWithValue("@Dely_Date", Dely_Date);
                    cmdInsert.Parameters.AddWithValue("@Status", "NEW");
                    cmdInsert.Parameters.AddWithValue("@Remarks", "-");
                    cmdInsert.Parameters.AddWithValue("@Department", Dept);

                    cmdInsert.ExecuteNonQuery();

                    cmdUpdate.Parameters.AddWithValue("@po_status", "ON HAND");
                    cmdUpdate.Parameters.AddWithValue("@cust_pono", PO_NO);
                    cmdUpdate.Parameters.AddWithValue("@cust_poitemno", Item_NO);

                    cmdUpdate.ExecuteNonQuery();
                }
                dtrCheckID.Close();


            }

            else
            {
                dtrCheckID.Close();
                Response.Write("<Script>alert('This item no have side order setting!!!')</Script>");
            }
        }

        conAuthor.Close();
        conAuthors.Close();
    }

Are you getting any errors when you run this code? If not, was is supposed to hapeen and what actually happens?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.