Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查数据库c#/.net中是否存在值_C#_Html_.net_Checkbox - Fatal编程技术网

检查数据库c#/.net中是否存在值

检查数据库c#/.net中是否存在值,c#,html,.net,checkbox,C#,Html,.net,Checkbox,我将表单数据发送到sql数据库,并以另一个表单显示用户的输出。我刚刚添加了一个复选框字段,并将“是”和“否”的值设置为“Y”和“N”。我可以保存并检索它们,但我正在尝试将该值绑定到表单中的复选框字段 所以我的问题是如何在我的复选框字段中显示数据库中该特定项的字段值 以下是我到目前为止所做的,但尚未奏效: private void LoadData() { Connection connection = new Connection(); try { con

我将表单数据发送到sql数据库,并以另一个表单显示用户的输出。我刚刚添加了一个复选框字段,并将“是”和“否”的值设置为“Y”和“N”。我可以保存并检索它们,但我正在尝试将该值绑定到表单中的复选框字段

所以我的问题是如何在我的复选框字段中显示数据库中该特定项的字段值

以下是我到目前为止所做的,但尚未奏效:

private void LoadData()
{
    Connection connection = new Connection();
    try
    {
        connection.connection1();
        SqlCommand com = new SqlCommand("Select Image,UserName,Description,Private,CompValue FROM FutureProjTbl Where id='" + projectId + "'", connection.con);
        SqlDataReader dataReader = com.ExecuteReader();

        while (dataReader.Read())
        {
            image1.Src = dataReader["Image"].ToString();
            Owner.Text = dataReader["UserName"].ToString();
            Description.Text = dataReader["Description"].ToString();
            //The following 2 are my attempts to retrieve the item and bind it to the checkbox on the project template all the values will load into.
            //privateItem.Checked = Convert.ToBoolean(dataReader["Private"].ToString());
            //privateItem = (CheckBox) dataReader["Private"];
            compValue.Text = dataReader["CompValue"].ToString();


            FillDataForEditProjectModal(dataReader);
        }
        dataReader.Close();
    }
    catch (Exception)
    {

        throw;
    }
}

您需要比较并返回一个布尔值来设置选中值

privateItem.Checked = (dataReader["Private"].ToString() == "Y");

您需要比较并返回一个布尔值来设置选中值

privateItem.Checked = (dataReader["Private"].ToString() == "Y");
试试这个:

privateItem.Checked = dataReader["Private"] == null ? false : dataReader["Private"].ToString() == "Y";
但是,与其将值存储为
Y
N
为什么不将该字段更改为数据库中的布尔值,然后您可以将其保存为复选框的选中状态(即
Y
true
N
false
),并且当您将其带回来时,映射会更加容易:

privateItem.Checked = (bool)dataReader["Private"];
试试这个:

privateItem.Checked = dataReader["Private"] == null ? false : dataReader["Private"].ToString() == "Y";
但是,与其将值存储为
Y
N
为什么不将该字段更改为数据库中的布尔值,然后您可以将其保存为复选框的选中状态(即
Y
true
N
false
),并且当您将其带回来时,映射会更加容易:

privateItem.Checked = (bool)dataReader["Private"];

你应该考虑使用类似于ORM的实体框架,它使你的生活在这样的操作中变得更容易。你应该考虑使用类似于ORM的实体框架,它使你的生活在这样的操作中变得更容易。我不知道我错过了什么,为什么他一开始就不会这么做。也许我并不是一个人。谢谢你在db中关于布尔运算的观点,我不知道我遗漏了什么,为什么他一开始就不会这么做。也许我并不孤单。