when i check on checkbox whose datatype is bit ,its value i.e 1 or 0 is not being store in database it is giving error
当我检查其数据类型为bit的复选框时,其值(即1或0)未存储在数据库中,它给出了错误
Conversion failed when converting the varchar value 'System.Web.UI.WebControls.CheckBox' to data type bit
将varchar值'System.Web.UI.WebControls.CheckBox'转换为数据类型位时转换失败
if (CheckBoxDT.Checked)
{
cmd.CommandText = " Insert into ClientMaster(doTips) values ( 1 )";
cmd.Parameters.AddWithValue("doTips", bool.Parse("1"));
cmd.ExecuteNonQuery();
}
else
{
cmd.CommandText = "insert into ClientMaster(doTips) values (0)";
cmd.Parameters.AddWithValue("doTips", bool.Parse("0"));
cmd.ExecuteNonQuery();
}
3 个解决方案
#1
3
Inserting Bit value to the database
将位值插入数据库
cmd.Parameters.Add("@check", SqlDbType.Bit).Value = Convert.ToInt16(CheckBoxDT.Checked);
retrieving selected check boxes Try this
检索选中的复选框试试这个
<asp:CheckBox ID="CheckBoxDT" runat="server" Checked='<%#((bool)Eval("isActive"))%>'
Style="color: #FFFFFF" />
Your code
你的代码
if (CheckBoxDT.Checked)
{
cmd.CommandText = " Insert into ClientMaster(doTips) values ( 1 )";
cmd.Parameters.AddWithValue("doTips", bool.Parse("1"));
cmd.ExecuteNonQuery();
}
else
{
cmd.CommandText = "insert into ClientMaster(doTips) values (0)";
cmd.Parameters.AddWithValue("doTips", bool.Parse("0"));
cmd.ExecuteNonQuery();
}
Replace this with
替换为
cmd.CommandText = "insert into ClientMaster(doTips) values (@doTips)";
cmd.Parameters.Add("@doTips", SqlDbType.Bit).Value = Convert.ToInt16(CheckBoxDT.Checked)
cmd.ExecuteNonQuery();
#2
1
Chances are you're passing the control, rather than the value of its state; you should be passing the state, and converting that explicitly if necessary:
你有可能通过控制,而不是其状态的价值;你应该传递状态,并在必要时明确转换它:
CheckBox // a web control, and no standard db type to represent it
CheckBox.Checked // a boolean value, generally translatable to db bit
#3
0
You are most probably passing the actual control to your database and not the state of it. You need to use myCheckBox.Checked
instead of just myCheckBox
.
您很可能将实际控件传递给数据库而不是它的状态。您需要使用myCheckBox.Checked而不仅仅是myCheckBox。
#1
3
Inserting Bit value to the database
将位值插入数据库
cmd.Parameters.Add("@check", SqlDbType.Bit).Value = Convert.ToInt16(CheckBoxDT.Checked);
retrieving selected check boxes Try this
检索选中的复选框试试这个
<asp:CheckBox ID="CheckBoxDT" runat="server" Checked='<%#((bool)Eval("isActive"))%>'
Style="color: #FFFFFF" />
Your code
你的代码
if (CheckBoxDT.Checked)
{
cmd.CommandText = " Insert into ClientMaster(doTips) values ( 1 )";
cmd.Parameters.AddWithValue("doTips", bool.Parse("1"));
cmd.ExecuteNonQuery();
}
else
{
cmd.CommandText = "insert into ClientMaster(doTips) values (0)";
cmd.Parameters.AddWithValue("doTips", bool.Parse("0"));
cmd.ExecuteNonQuery();
}
Replace this with
替换为
cmd.CommandText = "insert into ClientMaster(doTips) values (@doTips)";
cmd.Parameters.Add("@doTips", SqlDbType.Bit).Value = Convert.ToInt16(CheckBoxDT.Checked)
cmd.ExecuteNonQuery();
#2
1
Chances are you're passing the control, rather than the value of its state; you should be passing the state, and converting that explicitly if necessary:
你有可能通过控制,而不是其状态的价值;你应该传递状态,并在必要时明确转换它:
CheckBox // a web control, and no standard db type to represent it
CheckBox.Checked // a boolean value, generally translatable to db bit
#3
0
You are most probably passing the actual control to your database and not the state of it. You need to use myCheckBox.Checked
instead of just myCheckBox
.
您很可能将实际控件传递给数据库而不是它的状态。您需要使用myCheckBox.Checked而不仅仅是myCheckBox。