Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are adding the current object, <code>this</code>, to the list and then, in subsequent iterations, you are modifying the same object (via the <code>this</code> reference) before adding it, again, to the list. This means you end up with a list containing the same object (the <code>this</code> object) over and over again. Its values will reflect only the latest values applied to it.</p> <p>What you want to do instead is add new instances of ProductsDAL to the list each time, i.e. instead of modiying <code>this</code>, instead you should create a new ProductsDAL, set its state and add that to the list.</p> <p>Here is a the first change you should make:</p> <pre><code> public List GetAllProducts(string sqlQuery) { DataSet dsinsert = GetDataSet(sqlQuery, "tblProducts"); List bPList = new List(); foreach (DataRow item in dsinsert.Tables[0].Rows) { ProductsDAL product = new ProductsDAL(); product.Product_ID = item["Product_ID"].ToString(); product.ProductDescr = item["ProductDescr"].ToString(); bPList.Add(product); } return bPList; } </code></pre> <p>Also:</p> <ol> <li>Because this method now has no effect on the current ProductsDAL instance (it's actually creating new ones) then it not longer makes sense for it to be a instance method. You could consider making it a static method (and making GetDataSet() static too) or moving this logic out into some other class that has the responsibility of fetching products from the database.</li> <li>You may want to change the constructor on ProductsDAL so that the mandatory fields have to be set so that it is not possible to create ProductsDAL objects that do not have their ID and description set.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload