lundi 25 avril 2016

C# How to set checkbox from database values

I created this search button, all textboxes are perfectly filed with values returned from database when I click the search button.

My problem is how to set the checkboxe.

This is what I have :

private void button4_Click(object sender, EventArgs e)
{
    SqlConnection cn = new SqlConnection(global::Physiotherapie.Properties.Settings.Default.PhysioDBConnectionString);
    try
    {
        cn.Open();
        string s = "select * from AssessmentForm where Name like '" + textBox2.Text + "%'";
        mcd = new SqlCommand(s, cn);
        mdr = mcd.ExecuteReader();

        if (mdr.Read())
        {
            //All working perfectly here,when search button pressed
            // i get returned values 100%
            textBox1.Text = mdr["Patient_ID"].ToString();
            textBox2.Text = mdr["Name"].ToString();
            textBox3.Text = mdr["Age"].ToString();
            comboBox1.Text = mdr["Day"].ToString();
            comboBox2.Text = mdr["Month"].ToString();
            comboBox3.Text = mdr["Year"].ToString();
            textBox4.Text = mdr["References"].ToString();
            textBox5.Text = mdr["Weight"].ToString();
            textBox6.Text = mdr["Height"].ToString();
            textBox7.Text = mdr["PhoneNumber"].ToString();
            textBox8.Text = mdr["Address"].ToString();
            comboBox4.Text = mdr["Gender"].ToString();
            textBox9.Text = mdr["Insurance"].ToString();
            textBox10.Text = mdr["Profession"].ToString();
            textBox11.Text = mdr["Email"].ToString();
            dateTimePicker1.Text = mdr["FirstConsultationTaken"].ToString();
            textBox12.Text = mdr["Question1"].ToString();
            textBox13.Text = mdr["Treatments"].ToString();
            textBox14.Text = mdr["Allergies"].ToString();
            textBox15.Text = mdr["SurgicalOperations"].ToString();

            //my problem is here with checkboxes, how do i make the 
            // the search button return the state of checkboxes i mean
            // show if they r checked or not
            checkBox1.Checked = Convert.ToBoolean(mdr["osteoporosis"].ToString());
        }
        else
        {
            MessageBox.Show("No Data found!");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        mdr.Close();
    }
}




Aucun commentaire:

Enregistrer un commentaire