페이지로드시 Access 데이터베이스에서 부울 열의 값을 읽도록 코딩 된 asp.net 웹 페이지의 버튼이 있고 버튼의 값이 열의 값 true 또는 false입니다.데이터베이스의 부울 값 업데이트
기본적으로이 버튼은 제품의 표시/숨기기 버튼입니다 (제품을 숨기려면 클릭하거나 이미 숨겨져 있으면 표시하려면 클릭하십시오).
제품을 숨기고 제품을 보이게하려면 클릭하여 (데이터베이스를 업데이트) 이상한 동작을 보이지만 숨기는 것이 아니라 작동하지 않는 이유는 무엇입니까? 다른 사람은하지 않을 것이다. 다음과 같이
코드는 다음과 같습니다 긴 코드에 대한
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. –