我的asp.net网页上有一个按钮,我已编码为读取页面加载中访问数据库中布尔列的值,按钮的作用是根据列的值是真还是假而改变。
本质上,此按钮是产品的显示/隐藏按钮(单击它可隐藏产品,或者如果已隐藏则单击它以使其可见)。
我得到一些奇怪的行为,就像产品被隐藏,点击使产品可见(更新数据库),然而,隐藏它没有,我无法弄清楚为什么一个会工作,但另一个不会。
代码如下:
if (!IsPostBack)
try
{
s = WebConfigurationManager.ConnectionStrings["LNDatabase"].ConnectionString;
conn = new OleDbConnection(s);
cmd = new OleDbCommand("SELECT * FROM products WHERE products.prod_id = @test", conn);
OleDbParameter test = new OleDbParameter("@test", OleDbType.Integer);
test.Value = Request.QueryString["prod_id"];
cmd.Parameters.Add(test);
conn.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dr.Read();
title.Text = dr["shortdesc"].ToString();
description.Text = dr["longdesc"].ToString();
price.Text = dr["price"].ToString();
productcat = dr["cat"].ToString();
product_live = dr["live"].ToString();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
dr.Close();
conn.Close();
}
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
{
bool prod_live_bool = Convert.ToBoolean(product_live);
if (prod_live_bool == true)
{
live_label.Text = "This product is visible to customers";
livelabelbutton.Text = "Hide this product";
livelabelbutton.Click += new EventHandler(this.hideproduct_click);
}
else
{
live_label.Text = "This product is not visible to customers";
livelabelbutton.Text = "Make this product visible";
livelabelbutton.Click += new EventHandler(this.showproduct_click);
}
}
protected void hideproduct_click(object sender, EventArgs e)
{
string prodid = Request.QueryString["prod_id"];
s = WebConfigurationManager.ConnectionStrings["LNDatabase"].ConnectionString;
string str = "UPDATE products SET live = @hide WHERE prod_id=@product";
using (OleDbConnection conn = new OleDbConnection(s))
{
using (OleDbCommand cmd = new OleDbCommand(str, conn))
{
OleDbCommand mycommand = new OleDbCommand();
OleDbParameter hideparam = new OleDbParameter("@hide", OleDbType.Boolean);
hideparam.Value = false;
cmd.Parameters.Add(hideparam);
OleDbParameter product = new OleDbParameter("@product", OleDbType.VarChar);
product.Value = prodid;
cmd.Parameters.Add(product);
conn.Open();
cmd.ExecuteNonQuery();
}
}
Response.Redirect(Request.RawUrl);
}
protected void showproduct_click(object sender, EventArgs e)
{
string prodid = Request.QueryString["prod_id"];
s = WebConfigurationManager.ConnectionStrings["LNDatabase"].ConnectionString;
string str = "UPDATE products SET live = @show WHERE prod_id=@product";
using (OleDbConnection conn = new OleDbConnection(s))
{
using (OleDbCommand cmd = new OleDbCommand(str, conn))
{
OleDbCommand mycommand = new OleDbCommand();
OleDbParameter hideparam = new OleDbParameter("@show", OleDbType.Boolean);
hideparam.Value = true;
cmd.Parameters.Add(hideparam);
OleDbParameter product = new OleDbParameter("@product", OleDbType.VarChar);
product.Value = prodid;
cmd.Parameters.Add(product);
conn.Open();
cmd.ExecuteNonQuery();
}
}
Response.Redirect(Request.RawUrl);
}
很抱歉长代码。
答案 0 :(得分:2)
如果命令按钮的点是切换[live]
是/否字段的值,请让db引擎执行您需要的操作。
UPDATE products SET live = (Not live) WHERE prod_id=@product
Not
返回布尔值的倒数。因此Not True
会返回False
而Not False
会返回True
。因此,UPDATE
语句将live
设置为执行UPDATE
之前包含的任何内容的反转。在Access会话中尝试将其作为新查询来检查它的运作方式。
如果这是令人满意的,你就不需要单独的例子来隐藏和显示。