【发布时间】:2014-12-13 15:41:23
【问题描述】:
我的应用程序需要帮助,因为我试图在每次搜索后显示产品总数!我做了一个选择语句,每次我使用单选按钮列表和一个搜索框搜索产品时,每次从我的表 Product 中显示一行。搜索可以是用户名或产品 ID,然后用户可以选择该行并将其从网格视图中删除。我还放置了一个标签,该标签应该显示剩余的产品数量。这个标签应该给我网格视图中的产品数量!我的问题是我使用的标签只显示第一,它只生成记录,我需要显示产品的总数。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Admin_AdminUserControl_Product : System.Web.UI.UserControl
{
DataBaseConn Db = new DataBaseConn();
Basket _b = new Product();
protected void Page_Load(object sender, EventArgs e)
{
// Label shows only one record at a time
lblQueue.Text = _b.Queue().ToString();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
grid.DataSource = _b.SelectRow(rdoField.SelectedValue,txtSearch.Text);
grid.DataBind();
lblMsg.Text = "";
btnRemove.Enabled = false;
grid.SelectedIndex = -1;
}
protected void btnRemove_Click(object sender, EventArgs e)
{
if (_b.RemoveProduct(grid.SelectedRow.Cells[1].Text))
lblMsg.Text = "Product is removed";
else
lblMsg.Text = "Unable to remove a Product!";
grid.DataBind();
btnRemove.Enabled = false;
}
protected void grid_SelectedIndexChanged(object sender, EventArgs e)
{
btnRemove.Enabled = true;
}
类产品
public DataTable Data(string txtField, string Value)
{
string SQL = String.Format("Select * from Product where {0} like'%{1}%'", txtField, Value);
return Data(SQL);
}
public DataTable Data(string Query)
{
try
{
return Db.RunQuery(Query);
}
catch
{
return new DataTable();
}
}
public bool RemoveProduct(string ProductId)
{
this.ProductID = ProductId;
return Delete();
}
public DataTable SelectRow(string Field, string Value)
{
string sql = string.Format("SELECT TOP 1 * FROM Product where {0} like '%{1}%' ORDER BY
ProductID ASC", Field, Value);
return SelectRow(sql);
}
public DataTable SelectRow(string Query)
{
try
{
return Db.RunQuery(Query);
}
catch
{
return new DataTable();
}
}
//This label should show number of all products i.e from 0- any number
public int Queue()
{
int customers;
String sql = String.Format("Select Count(*) from Product");
customers = Db.RunQuery(sql).Rows.Count;
return customers;
}
}
【问题讨论】:
-
这是什么
DataBaseConn对象?您不能运行查询来计算行数。你必须执行一个标量语句! -
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
这能回答你的问题吗? Capturing count from an SQL query
标签: c# asp.net sql-server