c# - 如何从数据库中获取数据列表中的特定 ID?
问题描述
我正在尝试id
从数据库中获取特定信息,但由于某些原因,我在id
.
在页面加载中:
string id = Request.QueryString["ProductID"];
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["myCart"] == null)
{
myCart = new Cart();
Session["myCart"] = myCart;
}
string id = Request.QueryString["ProductID"];
int id2 = this.DataList1.SelectedIndex;
myCart = (Cart)Session["myCart"];
DataTable dt = ds.SelectQuery("SELECT * FROM Products where ProductID = '" + id + "'");
DataRow row = dt.Rows[0];
myCart.Insert(new CartItem(int.Parse(id), row["ProductName"].ToString(), int.Parse(row["UnitPrice"].ToString()), 1));
}
解决方案
好的,所以我的建议是:将以下链接按钮添加到您的项目模板
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="select">Select</asp:LinkButton>
然后SelectedIndexChange
在代码文件中处理事件:
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = DataList1.SelectedIndex;
//Your product id should be bound to a label in the item template
Label lbl = (Label)DataList1.Items[index].FindControl("IDoftheControlcontainingid");
string id=lbl.Text;
//use the id variable to get the product and add to cart. your old code
if (Session["myCart"] == null)
{
myCart = new Cart();
Session["myCart"] = myCart;
}
myCart = (Cart)Session["myCart"];
DataTable dt = ds.SelectQuery("SELECT * FROM Products where ProductID = '" + id + "'");
DataRow row = dt.Rows[0];
myCart.Insert(new CartItem(int.Parse(id), row["ProductName"].ToString(), int.Parse(row["UnitPrice"].ToString()), 1));
}
推荐阅读
- google-api - google sheet api调用的高延迟
- vb.net - 查询表达式 'Kad Matrik='DDWC2017/060550 中的语法错误(缺少运算符)
- xml - 更新和保存 XML 文件会将其内容更改为 UWP 中无法识别的字符
- python - 将 dat 文件导入熊猫数据框
- vb.net - 获取字符串中字符组合后的字符
- linux - 如何在linux中的文件中添加带有特定单词的新列?
- cassandra - ReadTimeout:来自服务器的错误:code=1200 [协调节点超时等待副本节点的响应]
- javascript - SQLite 插入方法写入未识别的值
- r - 使用 dplyr 查找数据框中不同级别的变量数量?
- vba - AddressEntry 对象是否具有类似 Index Number 的属性来查找同名人员?