简体   繁体   中英

Duplication of data on Webpage Asp.net MVC

I'm having this issue when I enter items into a database table and add those items on the webpage it keeps duplicating. After debugging I recognised that it was this line items.Add(item);. The stored procedure does return the amount of items in the database and the number of items on a requisition number. this is what it looks like enter image description here and this is what I expect enter image description here . I only entered two entered items and it comes as I entered four items

Stored Procedure

 @Req_No varchar (50)
AS
BEGIN



        SELECT a.ITEMNMBR, a.ITEMDESC, ab.employee_id, ab.department,ab.employee_name,quantity_requested,b.expense_acc, c.ACTDESCR+'/'+c.ACTNUMBR_1+'-'+c.ACTNUMBR_2 [Expense_Acc],
         b.unit_of_measure
    FROM [TWCL].[dbo].IV00101 a inner Join RequisitionItem b on a.ITEMNMBR = b.item_no
    Inner Join Requisition ab
        on ab.Req_No = b.Req_No
    Inner Join [TWCL].dbo.GL00100 c
        on b.expense_acc = c.ACTINDX
    where b.Req_No = @Req_No

Model

    public List<Item> getRequestItemByRquisition(string Req_No)
{
        List<Item> items = new List<Item>();
        SqlConnection TWCLOPConnect = new SqlConnection(connectionString.ToString());

        SqlCommand itemscommand = new SqlCommand();
        SqlDataReader itemRdr;

        itemscommand.CommandText = "requisition_sp_getItemNum ";
        itemscommand.CommandType = CommandType.StoredProcedure;
        itemscommand.Connection = TWCLOPConnect;
        itemscommand.Parameters.Add("@Req_No", SqlDbType.VarChar).Value = Req_No;

        try
        {
            TWCLOPConnect.Open();
            itemRdr = itemscommand.ExecuteReader();

            while (itemRdr.Read())
            {
                Item item = new Item();
                item.itemNumber = itemRdr.GetString(0);
                item.description = itemRdr.GetString(1);
                item.price = Convert.ToDouble(itemRdr[3]);
                item.quantity = Convert.ToDouble(itemRdr[4]);
                item.expense_account.index = itemRdr.GetInt32(5);
                item.expense_account.account_desc = itemRdr.GetString(6);

                item.selecteduomtext = itemRdr.GetString(8);
                items.Add(item);
            }

            itemRdr.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            TWCLOPConnect.Close();
        }

        return items;
}

Your stored procedure returns duplicate rows:

    SELECT a.ITEMNMBR, a.ITEMDESC, ab.employee_id, ab.department,ab.employee_name,quantity_requested,b.expense_acc, c.ACTDESCR+'/'+c.ACTNUMBR_1+'-'+c.ACTNUMBR_2 [Expense_Acc],
     b.unit_of_measure
FROM [TWCL].[dbo].IV00101 a inner Join RequisitionItem b on a.ITEMNMBR = b.item_no
Inner Join Requisition ab
    on ab.Req_No = b.Req_No
Inner Join [TWCL].dbo.GL00100 c
    on b.expense_acc = c.ACTINDX
where b.Req_No = @Req_No 

Run this stored procedure in the sql and you will see it.

Some times mismatch or extra data returned from the DB using inner join is easy. Remove the inner joins one at the time and check.

All this line of code does:

   itemRdr = itemscommand.ExecuteReader();

            while (itemRdr.Read())
            {
                Item item = new Item();
                item.itemNumber = itemRdr.GetString(0);

        item.description = itemRdr.GetString(1);
                item.price = Convert.ToDouble(itemRdr[3]);
                item.quantity = Convert.ToDouble(itemRdr[4]);
                item.expense_account.index = itemRdr.GetInt32(5);
                item.expense_account.account_desc = itemRdr.GetString(6);

                item.selecteduomtext = itemRdr.GetString(8);
                items.Add(item);
            }

is read your returned rows and while it is still reading, it will add rows. Meaning your stored procedure returned duplicated amount of rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM