我在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 [email protected]";
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 [email protected]";
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);
}
很抱歉的长码。
OMG ....在后面的代码中的sql语句字符串..什么是一个很好的编程模式 –
是不好的?我对asp.net相当陌生...... –
取决于你认为不好的东西....对我来说,将整个应用程序放在一个单独的代码文件中,没有关注点分离或数据访问层,甚至是一个ORM。 –