I have a function that is meant to get values from a SQL database table (called ProductProfiles) and display them inside a .csv document that users can download and view. The results are based on a drop down list's selection (called ddlClassification). The function works almost perfectly, but the while loop used to write the sql data to a .csv file keeps repeating data. For example, if 3 entries exist (we will call them A, B and C), the .csv file should only show 3 entries. However, with things as they are now, these 3 entries are repeated multiple times in the .csv file, which is incorrect.
I am not sure what I'm missing from the function to end this repeating issue.
This is the function
private void WriteProductProfile(string ProfileID, ref TextWriter tw)
{
string CatList = GetCatList();
string sqlQuery = @"SELECT [ProfileID], [Name], [Description], [SpeciesLink],
[LineDraw], [LineDrawThumbnail], [ProfileThumbnail], [ComponentThickness],
[ComponentWidth], [ComponentFactor], [FinishedThickness], [FinishedWidth],
ISNULL([ClassificationID],-1) as ClassificationID, [StockOrCust],
[Visibility], [Notes], [OrderBy]
FROM ProductProfile
WHERE ClassificationID = @ClassificationID";
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
{
SqlCommand cmd = new SqlCommand(sqlQuery, cn);
cmd.Parameters.Add(new SqlParameter("@ClassificationID", ddlClassification.SelectedValue));
cmd.CommandType = CommandType.Text;
cn.Open();
using (IDataReader reader = cmd.ExecuteReader())
{
while(reader.Read())
{
string myLine = reader["ProfileID"] + "\t"
+ ImportHelper.CleanHTMLText(reader["Name"].ToString()) + "\t"
+ ImportHelper.CleanHTMLText(reader["Description"].ToString()) + "\t"
+ reader["SpeciesLink"] + "\t"
+ reader["LineDraw"] + "\t"
+ reader["LineDrawThumbnail"] + "\t"
+ reader["ProfileThumbnail"] + "\t"
+ reader["ComponentThickness"] + "\t"
+ reader["ComponentWidth"] + "\t"
+ reader["ComponentFactor"] + "\t"
+ reader["FinishedThickness"] + "\t"
+ reader["FinishedWidth"] + "\t"
+ reader["ClassificationID"] + "\t"
+ reader["StockOrCust"] + "\t"
+ reader["Visibility"] + "\t"
+ ImportHelper.CleanHTMLText(reader["Notes"].ToString()) + "\t"
+ reader["OrderBy"] + "\t";
tw.WriteLine(myLine);
}
}
cn.Close();
}
}
This is the code calling the WriteProductProfile function
private void ProcessCategory(Category MainCat, ref TextWriter tw, ref string ProdUseList)
{
string sqlQuery = "SELECT [ProfileID], [ClassificationID] FROM baird_ProductProfile WHERE ClassificationID = @ClassificationID";
string profId = "";
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
{
SqlCommand cmd = new SqlCommand(sqlQuery, cn);
cmd.Parameters.Add(new SqlParameter("@ClassificationID", ddlClassification.SelectedValue));
cmd.CommandType = CommandType.Text;
cn.Open();
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
profId = reader["ProfileID"].ToString();
}
}
cn.Close();
}
foreach (CatalogNode subCat in MainCat.CatalogNodes)
{
//process the children items
switch (subCat.CatalogNodeType)
{
case CatalogNodeType.Category:
ProcessCategory((Category)subCat.ChildObject, ref tw, ref ProdUseList);
break;
case CatalogNodeType.Product:
if (AddProduct(profId, ref ProdUseList))
WriteProductProfile(profId, ref tw);
break;
}
}
}