Any comments on this code pieces to make it more professional will be gladly accepted. This is why I opened this topic. But the main concern is this; I need to pass item ids, store ids, and start date and end date to get sale records. One of the problem is because of SQL parameter limit (2100), I cannot send more than 2100 parameters to the statement. Now I am trying to receive records store by store (store number is more than 4000 and also item amount). But I could not figure out a good way to do it.
Any one who spent time to help appreciated.
This is data access:
public List<SalesList> ExecuteSales(List<string> items, int storeID, int W1,int W2,int vendorID,int retailerID)
{
SqlCommand command = new SqlCommand();
string statement = "SELECT I.ITEM_NBR,I.ITEM_DESC1,I.ITEM_DESC2,I.VENDOR_STK_NBR,SUM(SA.POS_QTY) AS POS_QTY,SUM(SA.POS_SALES) AS POS_SALES "
+ "FROM SALES_FTBL SA,ITEM_TBL I "
+ "WHERE SA.RETAILER_ID=I.RETAILER_ID "
+ "AND SA.RETAILER_ID = @RetailerID "
+ "AND SA.VENDOR_NBR = @VendorID "
+ "AND SA.STORE_NBR = @StoreID "
+ "AND SA.ITEM_NBR=I.ITEM_NBR "
+"AND SA.ITEM_NBR IN (";
command.Parameters.AddWithValue("@RetailerID", retailerID);
command.Parameters.AddWithValue("@VendorID",vendorID);
command.Parameters.AddWithValue("@StoreID", storeID);
for (int i = 0; i < items.Count; i++)
{
if (i > 0)
{
statement += ", ";
}
string paramStr = "@itemNo" + i;
statement += paramStr;
command.Parameters.Add(paramStr, System.Data.SqlDbType.Int);
command.Parameters[paramStr].Value = items[i];
}
statement += ") ";
//statement += "AND STORE_NBR IN (";
//for (int i = 0; i < stores.Count; i++)
//{
// if (i > 0)
// {
// statement += ", ";
// }
// string paramStr = "@storeNo" + i;
// statement += paramStr;
// command.Parameters.Add(paramStr, System.Data.SqlDbType.Int);
// command.Parameters[paramStr].Value = stores[i];
//}
//statement += ") ";
statement += "AND WEEK IN (";
for (int i = 0; i <1; i++)
{
if (i > 0)
{
statement += ", ";
}
string paramStr = "@W" + i;
statement += paramStr;
command.Parameters.Add(paramStr, System.Data.SqlDbType.Int);
command.Parameters[paramStr].Value = W1;
}
W1=W1+1;
for (int counter=W1; counter < W2;counter++ )
{
if (counter > 0)
{
statement += ", ";
}
string paramStr = "@W" + counter;
statement += paramStr;
command.Parameters.Add(paramStr, System.Data.SqlDbType.Int);
command.Parameters[paramStr].Value = W1++;
}
statement += ") ";
statement += "GROUP BY I.ITEM_NBR,I.VENDOR_STK_NBR,I.ITEM_DESC1,I.ITEM_DESC2 Order By I.ITEM_DESC2 ";
command.CommandText = statement;
command.Connection = connection;
List<SalesList> sales = new List<SalesList>();
SalesList sale;
string itemDescription;
string desc1;
string desc2;
int count = 0;
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
sale = new SalesList();
sale.ItemNumber = Convert.ToInt32(reader["ITEM_NBR"].ToString().TrimEnd());
if (reader["ITEM_DESC1"] != null)
{
desc1 = reader["ITEM_DESC1"].ToString().TrimEnd();
}
else { desc1 = ""; }
if (reader["ITEM_DESC2"] != null)
{
desc2 = reader["ITEM_DESC2"].ToString().TrimEnd();
}
else { desc2 = ""; }
if (!desc1.Equals(desc2) || !desc2.Equals(desc1))
{ itemDescription = desc1 + " " + desc2; }
else { itemDescription = desc2; }
sale.ItemDescription2 = itemDescription;
sale.PosQuantity = Convert.ToInt32(reader["POS_QTY"].ToString().TrimEnd());
sale.VendorStockNumber = reader["VENDOR_STK_NBR"].ToString().TrimEnd();
if (reader["POS_SALES"].ToString() != "")
{
sale.PosSales = Convert.ToDouble(reader["POS_SALES"].ToString().TrimEnd());
}
else { sale.PosSales = 0; }
sales.Add(sale);
}
}
catch (SqlException se)
{
Debug.WriteLine("---------- DEBUG INFORMATION ----------");
Debug.WriteLine(se.Message);
Debug.WriteLine("=======================================");
throw se;
}
finally
{
connection.Close();
}
return sales;
}
And this is business layer :
private List<SalesList> ExecuteSales(List<string> items, List<string> stores, string date1, string date2, int vendorID, int retailerID) {
int W1 = CalculateWeek(date1);
int W2 = CalculateWeek(date2);
SalesListDL salesListDO = new SalesListDL();
List<SalesList> sales = new List<SalesList>();
List<SalesList> salesX = new List<SalesList>();
for (int counter = 0; counter < stores.Count; counter++)
{
int storeID = Convert.ToInt32(stores[counter]);
salesX = salesListDO.ExecuteSales(items, storeID, W1, W2, vendorID, retailerID);
if (salesX.Count > 0)
{
foreach (SalesList saleX in salesX.ToList())
{
int index = sales.FindIndex(item => item.ItemNumber == saleX.ItemNumber);
if (index > 0)
{
sales[index].PosQuantity = +saleX.PosQuantity;
sales[index].PosSales = +saleX.PosSales;
salesX.Remove(saleX);
}
else { sales.Add(saleX); }
}
}
}
return sales;
}