vendredi 22 février 2019

Why is my gridview populating the incorrect data in my rows?

I am building a bookstore using GridViews and data from my database. There are checkboxes and each row has a quantity textbox. I am validating to make sure the at least one checkbox is checked and that the selected row has a quantity input before hitting submit. When the user hits submit, the data selected should be populated into another gridview.

The issue i am having is that when i select two different books and hit submit, the books populated on the gridview is just repeating only one book twice.

*Also the lblError text is still showing when i set the visibility to false when I submit.

Here's a snippet of the submit button call:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    double saleCount = 0;

    Processor p = new Processor();
    Book objBook = new Book();

    foreach (GridViewRow row in gvBooks.Rows)
    {
        CheckBox chkbx = (CheckBox)row.Cells[0].FindControl("cbBook");
        string title = row.Cells[1].Text;
        string authors = row.Cells[2].Text;
        string isbn = row.Cells[3].Text;
        DropDownList gvType = (DropDownList)row.Cells[4].FindControl("ddBookType");
        DropDownList gvMethod = (DropDownList)row.Cells[5].FindControl("ddMethod");
        TextBox qty = (TextBox)row.Cells[6].FindControl("txtQty");

        String strType = Convert.ToString(gvType.Text);
        String strMethod = Convert.ToString(gvMethod.Text);

        if (chkbx.Checked && !(string.IsNullOrEmpty(qty.Text)))
        {
            panelHeader.Visible = false;
            panelStudentInfo.Visible = false;
            panelCampus.Visible = false;
            panelCatalog.Visible = false;
            panelStudentInfo2.Visible = true;
            panelCampus2.Visible = true;
            panelCatalog2.Visible = true;
            gvBooks.Visible = false;
            gvOrder.Visible = true;
            panelButtons.Visible = false;

            txtStudentID2.Text = txtStudentID.Text;
            txtStudentName2.Text = txtStudentName.Text;
            txtStudentAddr2.Text = txtStudentAddr.Text;
            txtPhoneNo2.Text = txtPhoneNo.Text;
            ddCampus2.Text = ddCampuses.Text;

            lblError.Visible = false;

            int quantity = Convert.ToInt32(qty.Text);
            objBook.Title = title;
            objBook.Authors = authors;
            objBook.ISBN = isbn;
            objBook.BookType = strType;
            objBook.Method = strMethod;
            objBook.Quantity = quantity;

            objBook.Price = p.Calculate(isbn, strType, strMethod);
            objBook.TotalCost = objBook.Price * objBook.Quantity;
            orderList.Add(objBook);

            saleCount += objBook.Quantity;

            orderTotal = objBook.TotalCost + orderTotal;

            p.UpdateDB(isbn, quantity, strMethod, objBook.TotalCost);
        }
        else
        {
            lblError.Text = "* Please select a book & enter a quantity";
            lblError.Visible = true;
        }

        gvOrder.DataSource = orderList;
        gvOrder.DataBind();

        gvOrder.Columns[0].FooterText = "Totals";
        gvOrder.Columns[5].FooterText = saleCount.ToString();
        gvOrder.Columns[6].FooterText = orderTotal.ToString("C2");
    }
}




Aucun commentaire:

Enregistrer un commentaire