2013-01-17 77 views
0

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

很抱歉的长码。

+0

OMG ....在后面的代码中的sql语句字符串..什么是一个很好的编程模式 –

+0

是不好的?我对asp.net相当陌生...... –

+0

取决于你认为不好的东西....对我来说,将整个应用程序放在一个单独的代码文件中,没有关注点分离或数据访问层,甚至是一个ORM。 –

回答

2

如果命令按钮的位置是切换[live]是/否字段的值,则让数据库引擎执行您所需的操作。

UPDATE products SET live = (Not live) WHERE [email protected] 

Not返回一个布尔值的倒数。所以Not True返回FalseNot False返回True。因此UPDATE语句将live设置为与执行UPDATE之前包含的任何内容相反。在Access会话中尝试它作为一个新的查询来检查它是如何运作的。

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

+0

嗨HansUp,感谢您的回复。这是正确的,它只是为了切换'实时'列中的“是/否”字段的值。你能解释一下你的建议是多么的有趣吗?我试图实现它,但它不工作... –

+0

HansUp你是一个天才,这完美的作品,非常感谢你的帮助! –

+0

@HansUP'+ 1'因为'String不能保持空对话..从来没有机会表现出赞赏:) – bonCodigo