0

There are 2 scenarios to check when executing this SQL Select Statement below..

retrieveQuery = "SELECT itemName, itemCategory, itemSubCategory, itemPrice, orders, (orders * itemPrice) as TotalPrice FROM Menu WHERE itemCategory = @selectedCat AND itemSubCategory LIKE '%@selectedSubCat%'";

The 2 fields in the parameterized query are selectedCat and selectedSubCat.

First scenario is when the user selects for only Food and as you can see the DropdownList Value selected is ALL which means to say that the query statement's Where Clause should have only selectedCat = 'Food' and selectedSubCat LIKE '%%' (ALL VALUES OF FIELD 'itemSubCategory' FOUND IN THE SQL TABLE)

First Scenario

Second scenario is when the user selects for only Food and selects the DropdownList Value to be Donut which means to say that the query statement's Where Clause should have only selectedCat = 'Food' and selectedSubCat LIKE '%Donut%'

Second Scenario

Upon when the user hits the Export to PDF button, these lines of codes are executed.. I have removed the unnecessary codes

  String itemCat = HF_TabListType.Value;
  String itemSubCatFood = ddlFood.SelectedValue;
  String itemSubCatBeverage = ddlBeverages.SelectedValue;

  //Retrieving Data to Populate to Report Table
  String strConnString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
  SqlConnection myConn = new SqlConnection(strConnString);
  myConn.Open();

  SqlDataAdapter da;
  DataSet dsPDF = new DataSet();

  String retrieveQuery = String.Empty;

  using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
  {
      PdfPTable reportTable = new PdfPTable(1);
      if (itemCat == "Food" || itemCat == "Beverages")
      {
          reportTable = new PdfPTable(foodGV.Columns.Count - 1);
      }
      else if (itemCat == "Combo")
      {
          reportTable = new PdfPTable(gvCombo1Total.Columns.Count);
      }
      reportTable.TotalWidth = 1000f;
      reportTable.LockedWidth = true;
      reportTable.HorizontalAlignment = Element.ALIGN_CENTER;
      reportTable.SpacingAfter = 30f;

      //For Food and Beverages
      String[] headerForFB = { "No.", "Item Name", "Item Category", "Item Sub Category", "Item Price($)", "Orders", "Total($)" };

      //Fod Combo
      String[] headerForC = { "No.", "Combo", "Total Amount of Orders", "Total Discounts($)", "Total Sales($)" };

      if (itemCat == "Food" || itemCat == "Beverages")
      {
          for (int i = 0; i < headerForFB.Length; i++)
          {
              PdfPCell hCell = new PdfPCell(new Phrase(headerForFB[i].ToString(), BoldFontForHeader));
              hCell.FixedHeight = 25f;
              reportTable.AddCell(hCell);
          }
      }
      else if (itemCat == "Combo")
      {
          for (int i = 0; i < headerForC.Length; i++)
          {
              PdfPCell hCell = new PdfPCell(new Phrase(headerForC[i].ToString(), BoldFontForHeader));
              reportTable.AddCell(hCell);
          }
      }

      retrieveQuery = "SELECT itemName, itemCategory, itemSubCategory, itemPrice, orders, (orders * itemPrice) as TotalPrice FROM Menu WHERE itemCategory = @selectedCat AND itemSubCategory LIKE '%@selectedSubCat%'";

      da = new SqlDataAdapter(retrieveQuery.ToString(), myConn);
      da.SelectCommand.Parameters.AddWithValue("@selectedCat", itemCat);
      //da.SelectCommand.Parameters.AddWithValue("selectedSubCat", "%" + itemSubCatFood + "%");

      if (HF_TabListType.Value.ToString() == "Food")
      {
          if (ddlFood.SelectedIndex != 0)
          {
              da.SelectCommand.Parameters.AddWithValue("@selectedSubCat", itemSubCatFood);
          }
          else
          {
              da.SelectCommand.Parameters.AddWithValue("@selectedSubCat", "%%");
          }
      }
      else if (HF_TabListType.Value.ToString() == "Beverages")
      {
          if (ddlBeverages.SelectedIndex != 0)
          {
              da.SelectCommand.Parameters.AddWithValue("@selectedSubCat", itemSubCatBeverage);
          }
          else
          {
              da.SelectCommand.Parameters.AddWithValue("@selectedSubCat", "%%");
          }
      }

      da.Fill(dsPDF);

      GridView1.DataSource = dsPDF;
      GridView1.DataBind();

      PdfPCell cell = null;
      PdfPCell cellCount = null;
      int j = 0;

      //Food/Beverages
      if (dsPDF != null || dsPDF.Tables.Count != 0 || dsPDF.Tables[0].Rows.Count != 0)
      {
          foreach (DataRow r in dsPDF.Tables[0].Rows)
          {
              cellCount = new PdfPCell(new Phrase((j + 1).ToString(), NormalFont));
              cellCount.FixedHeight = 15f;
              reportTable.AddCell(cellCount);

              for (int i = 0; i < dsPDF.Tables[0].Columns.Count; i++)
              {
                  cell = new PdfPCell(new Phrase(r[i].ToString(), NormalFont));
                  cell.FixedHeight = 15f;
                  reportTable.AddCell(cell);
              }

              j++;
          }
      }

When the PDF is generated the output of the PDF Table gives this..

First Image

Using the Second Scenario stated above, I have tried to amend the codes and in some instances i got 2 rows with cells that are empty. Meaning the Dataset produced has the rows and columns needed to set the number of rows and columns of the PDF Table respectively.

Could there be an issue with the Retrieve Statement? Or because of adding the da.SelectCommand.Parameters line into if else statements?

I have been trying to work this out for days..

Appreciate any help please, thanks!

domster
  • 556
  • 2
  • 8
  • 26
  • I think the problem is with the construction of your retrieve query. [See this question for more information](https://stackoverflow.com/questions/14237755/t-sql-and-the-where-like-parameter-clause) – Jon Susiak Dec 29 '17 at 09:09
  • @JonSusiak i changed the sql statement to `retrieveQuery = "SELECT itemName, itemCategory, itemSubCategory, itemPrice, orders, (orders * itemPrice) as TotalPrice FROM Menu WHERE itemCategory = @selectedCat AND itemSubCategory LIKE '@selectedSubCat'";` and im using `da.SelectCommand.Parameters.AddWithValue("@selectedSubCat", "%" + itemSubCatFood + "%");` but it still doesn't work.. – domster Dec 29 '17 at 09:13

0 Answers0