0

我的 asp.net 网页上有一个按钮,我已对其进行编码以在页面加载时读取 access 数据库中的布尔列的值,并且该按钮的作用根据该列的值是真还是假而改变。

本质上,此按钮是产品的显示/隐藏按钮(单击它以隐藏产品,或者如果已经隐藏,请单击它使其可见)。

当产品被隐藏时,我遇到了一些奇怪的行为,单击以使产品可见(更新数据库),但是,隐藏它不起作用,我无法弄清楚为什么一个会起作用,而另一个不会.

代码如下:

 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);
}

抱歉,代码太长了。

4

1 回答 1

2

如果命令按钮的目的是切换[live]Yes/No 字段的值,则让 db 引擎执行您需要的操作。

UPDATE products SET live = (Not live) WHERE prod_id=@product

Not返回布尔值的倒数。所以Not True返回FalseNot False返回True。因此,该UPDATE语句设置live为在执行UPDATE. 在 Access 会话中尝试将其作为新查询来检查它的运行方式。

如果这令人满意,您将不需要单独的隐藏和显示例程。

于 2013-01-17T17:51:12.990 回答