如果选中复选框,如何在数据库中将复选框“N”的默认值更改为“Y” [英] How to change default value of checkbox 'N' to 'Y' in database, if check box is checked
本文介绍了如果选中复选框,如何在数据库中将复选框“N”的默认值更改为“Y”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在sql数据库中创建了一个包含一些列的表。在一个列名称为'Featured'时使用char(1),默认值为'N'。如果我没有选中,则在aspx页面中填写表单精选复选框,在数据库中保存为N,但如果我选中精选复选框,则数据库列应保存为Y。任何人都可以帮我这个吗?
我尝试过:
I have created a table in sql database with some columns.In that one column name is 'Featured' with char (1) and default value is 'N'.While filling form in aspx page, if i didn't checked 'Featured' checkbox, in database in saving as 'N', but if i check that 'Featured' checkbox, the database column should save as 'Y'.Can any one help me with this?
What I have tried:
DAL:
public void save_Click(int pid, string pname,bool fea, string sdesc)
{
SqlCommand cmd = new SqlCommand("SPSAVEDATA", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@P_id", pid);
cmd.Parameters.AddWithValue("@P_name", pname);
cmd.Parameters.AddWithValue("@P_isfeatured", fea);
cmd.Parameters.AddWithValue("@P_shortdesc", sdesc);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
BAL:
public void Product(int pid, string pname,bool fea, string sdesC)
{
dll.save_Click(pid, pname,fea, sdesc);
}
CS:
protected void Btn_save_Click(object sender, EventArgs e)
{
if (HiddenField1.Value == "")
{
bll.Product(0, txt_proName.Text, Convert.ToBoolean(chk_featured.Checked), txt_sdesc.Text);
Response.Write("<script>alert('Product Added Successfully')</script>");
}
else
{
bll.Product(Convert.ToInt16(HiddenField1.Value), txt_proName.Text,Convert.ToBoolean(chk_featured.Checked), txt_sdesc.Text);
Response.Write("<script>alert('Product Updated Successfully')</script>");
}
}
Stored Procedure:
ALTER PROC [dbo].[SPSAVEDATA]
@P_id int,
@P_name varchar(50),
@P_isfeatured char(1),
@P_shortdesc varchar(150)
AS
BEGIN
if(@P_id = 0)
BEGIN
insert into Products1 (P_name,P_isfeatured,P_shortdesc) values (@P_name,@P_isfeatured,@P_shortdesc)
END
else
BEGIN
update Products1 set P_name=@P_name,P_isfeatured=@P_isfeatured,P_shortdesc=@P_shortdesc where P_id=@P_id
END
END
推荐答案
改变这个:
cmd.Parameters.AddWithValue(@ P_isfeatured,fea);
到此:
cmd.Parameters.AddWithValue(@ P_isfeatured,(fea)?Y:N );
这篇关于如果选中复选框,如何在数据库中将复选框“N”的默认值更改为“Y”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文