在asp.net c中的数据库中插入Checkbox值 [英] Insert Checkbox value in database in asp.net c#

查看:74
本文介绍了在asp.net c中的数据库中插入Checkbox值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨frnds,



在我的网页上我有一个复选框,当在数据库中检查它应该插入1否则它应该是0



在数据库中,我有字段名称状态。



如果选中复选框,则必须保存为1



其他



应保存为0



请帮助。





谢谢。

解决方案

如果表列类型是INT而不是BIT。很容易将值保存到数据库中。



示例:

  var  sqlConnection =  new  SqlConnection(Settings.SqlConnectionString); 
sqlConnection.Open();

var sqlCommand = sqlConnecteion.CreateCommand();

if (checkBox1.Checked)
{
sqlCommand.CommandText = INSERT INTO tbl(_checkBoxValue)VALUES(1);
}
else
{
sqlCommand.CommandText = INSERT INTO tbl(_checkBoxValue)VALUES(0);
}


sqlCommand.ExecuteNonQuery();





代码可以是缩短为大约5行代码。


hi,

尝试这样:



< pre lang =c#> 字符串 str = ;
for int i = 0 ; i < = CheckBoxList1.Items.Count-1; i ++)
{
if (CheckBoxList1.Items [i] .Selected)
{
if (str ==
{
str = CheckBoxList1.Items [i] .Text;
}
else
{
str + = + CheckBoxList1.Items [i] .Text;
}
}
}





它将使用逗号分隔的复选框值创建字符串str。

然后将字符串str插入数据库列。


Hi frnds,

on my webpage i have a checkbox, when it is checked in database it should insert 1 else it should be 0

In database i have field name Status.

If checkbox checked it must save as 1

else

it should be save as 0

Please help.


Thanks.

解决方案

If the table column type is INT and not BIT. It is easy to save the value to your database.

Example:

var sqlConnection = new SqlConnection(Settings.SqlConnectionString);
sqlConnection.Open();

var sqlCommand = sqlConnecteion.CreateCommand();

if (checkBox1.Checked)
{
  sqlCommand.CommandText = "INSERT INTO tbl (_checkBoxValue) VALUES(1)";
}
else
{
  sqlCommand.CommandText = "INSERT INTO tbl (_checkBoxValue) VALUES(0)";
}


sqlCommand.ExecuteNonQuery();



The code can be shorted to about 5 lines of code.


hi,
try like this:

String str = "";
for (int i = 0; i <=CheckBoxList1.Items.Count-1; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
 if (str == "")
{
str = CheckBoxList1.Items[i].Text;
}
 else
{
str += "," + CheckBoxList1.Items[i].Text;
}
}
}



it will create a string str with comma separated checkbox values.
then insert the string str to your database column.


这篇关于在asp.net c中的数据库中插入Checkbox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆