Reputation: 545
I am sending form data to a sql database and I display the outputs from my users in another form. I just added a checkbox field and I set the values at 'Y' and 'N' for yes and no. I am able to save them and retrieve it but I am trying to bind that value to a checkbox field in the form.
So my question is how can I display the value of the field for that specific item in the database in my checkbox field?
Here is what I have so far and it hasn't worked:
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;
}
}
Upvotes: 1
Views: 287
Reputation: 58452
try this:
privateItem.Checked = dataReader["Private"] == null ? false : dataReader["Private"].ToString() == "Y";
But instead of storing the value as Y
and N
why not change that field into a bool in the database and then you can just save it as the checked state of the checkbox (ie true
for Y
, false
for N
) and will map a lot easier when you bring it back:
privateItem.Checked = (bool)dataReader["Private"];
Upvotes: 2
Reputation: 5771
You will need to compare and return a boolean to set the checked value
privateItem.Checked = (dataReader["Private"].ToString() == "Y");
Upvotes: 3