问题是,我正在尝试通过文本框上的用户输入更新我的数据库记录。 以下代码显示
protected void Page_Load(object sender, EventArgs e)
{
con1.Open();
try
{
//btn_save.Click += new EventHandler(btn_save_Click);
rs_id = Session["Res_Id"].ToString();
if (!this.IsPostBack)
{
getcategory();
getcuisine();
}
try
{
sitem_id = Session["item_id"].ToString();
if (sitem_id != "")
{
getitemdata();
getaddonprice();
getchoiceprice();
}
}
catch(Exception ex)
{
}
}
catch (Exception ex)
{
Response.Redirect("Default");
}
}
public void getitemdata()
{
try
{
SqlCommand cmd = new SqlCommand("select * from tbl_item where item_id='" + sitem_id + "'", con1);
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
string price,status;
txt_iname.Text = dr["item_name"].ToString();
txt_desc.Text = dr["item_description"].ToString();
drp_category.SelectedValue = dr["category_id"].ToString();
cui_drp.SelectedValue = dr["cusine_id"].ToString();
price_chk = dr["pos"].ToString();
status = dr["status"].ToString();
img_nname = dr["item_uname"].ToString();
img_dname = dr["item_img"].ToString();
if (price_chk == "1")
{
chk_price.Checked = true;
div_price.Style["display"] = "none";
div_choice.Style["display"] = "block";
div_addon.Style["display"] = "block";
}
else
{
chk_price.Checked = false;
div_price.Style["display"] = "block";
div_choice.Style["display"] = "none";
div_addon.Style["display"] = "none";
}
if(status=="1")
{
chk_status.Checked = true;
}
else
{
chk_status.Checked = false;
}
update_id = "1";
}
dr.Close();
}
catch(Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", ex.ToString(), true);
}
}
public void getaddonprice()
{
try
{
SqlCommand cmd = new SqlCommand("select vname as add_on_name,price as amt from tbl_price_master where item_id='" + sitem_id + "' and type='2'", con1);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
grid_addon.DataSource = dt;
grid_addon.DataBind();
}
catch(Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", ex.ToString(), true);
}
}
public void getchoiceprice()
{
try
{
SqlCommand cmd = new SqlCommand("select vname as choice_name,price as amt from tbl_price_master where item_id='" + sitem_id + "' and type='1'", con1);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
grid_choice.DataSource = dt;
grid_choice.DataBind();
}
catch (Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", ex.ToString(), true);
}
}
public void update()
{
try
{
string ch_status;
if(chk_status.Checked)
{
ch_status = "1";
}
else
{
ch_status = "0";
}
string item_uname = txt_iname.Text + "_" + rs_id;
if (chk_price.Checked == true)
{
if(img_res.HasFile)
{
deleteimg();
getimg();
SqlCommand cmd = new SqlCommand("master_crud_b2b", con1);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@item_name", txt_iname.Text);
cmd.Parameters.AddWithValue("@restaurant_id", rs_id);
cmd.Parameters.AddWithValue("@cuisine_id", cui_drp.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@category_id", drp_category.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@item_img", "Item_Images/" + newFileName);
cmd.Parameters.AddWithValue("@pos", "1");
cmd.Parameters.AddWithValue("@status", ch_status);
cmd.Parameters.AddWithValue("@item_desc", txt_desc.Text);
cmd.Parameters.AddWithValue("@item_uname", item_uname);
cmd.Parameters.AddWithValue("@item_id", sitem_id);
cmd.Parameters.AddWithValue("@mode", 21);
cmd.ExecuteNonQuery();
item_id = sitem_id;
deleteprice();
price_insert();
price_insert2();
}
else
{
SqlCommand cmd = new SqlCommand("master_crud_b2b", con1);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@item_name", txt_iname.Text);
cmd.Parameters.AddWithValue("@restaurant_id", rs_id);
cmd.Parameters.AddWithValue("@cuisine_id", cui_drp.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@category_id", drp_category.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@pos", "1");
cmd.Parameters.AddWithValue("@status", ch_status);
cmd.Parameters.AddWithValue("@item_desc", txt_desc.Text);
cmd.Parameters.AddWithValue("@item_uname", item_uname);
cmd.Parameters.AddWithValue("@item_id", sitem_id);
cmd.Parameters.AddWithValue("@mode", 20);
cmd.ExecuteNonQuery();
item_id = sitem_id;
deleteprice();
price_insert();
price_insert2();
}
}
else
{
if (img_res.HasFile)
{
deleteimg();
getimg();
SqlCommand cmd = new SqlCommand("master_crud_b2b", con1);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@item_name", txt_iname.Text);
cmd.Parameters.AddWithValue("@restaurant_id", rs_id);
cmd.Parameters.AddWithValue("@cuisine_id", cui_drp.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@category_id", drp_category.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@item_img", "Item_Images/" + newFileName);
cmd.Parameters.AddWithValue("@pos", "0");
cmd.Parameters.AddWithValue("@status", ch_status);
cmd.Parameters.AddWithValue("@item_desc", txt_desc.Text);
cmd.Parameters.AddWithValue("@item_uname", item_uname);
cmd.Parameters.AddWithValue("@item_id", sitem_id);
cmd.Parameters.AddWithValue("@mode", 21);
cmd.ExecuteNonQuery();
item_id = sitem_id;
deleteprice();
price_insert3();
}
else
{
SqlCommand cmd = new SqlCommand("master_crud_b2b", con1);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@item_name", txt_iname.Text);
cmd.Parameters.AddWithValue("@restaurant_id", rs_id);
cmd.Parameters.AddWithValue("@cuisine_id", cui_drp.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@category_id", drp_category.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@pos", "0");
cmd.Parameters.AddWithValue("@status", ch_status);
cmd.Parameters.AddWithValue("@item_desc", txt_desc.Text);
cmd.Parameters.AddWithValue("@item_uname", item_uname);
cmd.Parameters.AddWithValue("@item_id", sitem_id);
cmd.Parameters.AddWithValue("@mode", 20);
cmd.ExecuteNonQuery();
item_id = sitem_id;
deleteprice();
price_insert3();
}
}
Session["item_id"] = null;
update_id = "0";
Response.Redirect("Item_List");
}
catch (Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", ex.ToString(), true);
}
}
现在,为了确保一切正常,我尝试更改名为txt_desc的文本框的内容。令我惊讶的是,点击“保存”按钮后,txt_desc文本框的内容将返回其原始内容。
谢谢。
答案 0 :(得分:0)
功能getitemdata()
;总是被称为。它应该仅在第一次加载页面时调用。
单击按钮时,此功能会在text_desc
中再次填充数据库中的数据。
您应该在
中的数据库中移动填充数据的所有代码!IsPostback
像
在page_load()
if (!this.IsPostBack)
{
getcategory();
getcuisine();
getitemdata();
}
另请注意以下事项:
connection
对象作为全局class
变量可以在某些时候给您带来连接管理问题。将其移近SqlCommand
用法。 variables
SqlCommand
代替字符串连接进行SQL查询
醇>