在下面执行此SQL Select Statement
时,有两种情况需要检查..
retrieveQuery = "SELECT itemName, itemCategory, itemSubCategory, itemPrice, orders, (orders * itemPrice) as TotalPrice FROM Menu WHERE itemCategory = @selectedCat AND itemSubCategory LIKE '%@selectedSubCat%'";
参数化查询中的2个字段为selectedCat
和selectedSubCat
。
第一种情况是当用户仅选择Food
时,您可以看到所选的DropdownList
值为ALL
,这意味着查询语句的Where Clause
应该是只有selectedCat = 'Food'
和selectedSubCat LIKE '%%' (ALL VALUES OF FIELD 'itemSubCategory' FOUND IN THE SQL TABLE)
第二种情况是用户仅选择Food
并选择DropdownList
值为Donut
,这意味着查询语句的Where Clause
应该只有selectedCat = 'Food'
{1}}和selectedSubCat LIKE '%Donut%'
当用户点击Export to PDF
按钮时,会执行这些代码行。
我删除了不必要的代码
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++;
}
}
生成PDF时,PDF表的输出会给出这个..
使用上述Second Scenario
,
我试图修改代码,在某些情况下我得到2 rows with cells that are empty
。这意味着生成的Dataset
分别需要rows
和columns
来设置number of rows and columns
的{{1}}。
检索声明是否存在问题?或者是因为在if else语句中添加了PDF Table
行?
我一直试图解决这个问题......
感谢任何帮助,谢谢!