hi there,

i have a check box in a form. to this form i am loading information from the database(mdf file). and also in the checkbox checkchange event i have a code to do something. when i load the data if the checkbox value in the database is true then i make the check box checked which is give by the below code:

if (ITAR == "True")
chPITAR.CheckState = CheckState.Checked;(is contained in )

String ITAR = "";
            db10.openConnection();

            String query3 = @"Select TPW,TPR,TPI,TPM,ITAR From TopicContributor Where TopicNo = @topicNo and Phase = @phase";
            SqlCommand command3 = new SqlCommand(query3, DB.getConnection());

            command3.Parameters.Add("@topicno", SqlDbType.VarChar).Value = TopicNo;
            command3.Parameters.Add("@phase", SqlDbType.Int).Value = Phase;

            SqlDataReader reader3 = command3.ExecuteReader();

            while (reader3.Read())
            {
                cbPPW.Text = reader3[0].ToString();
                cbPPR.Text = reader3[1].ToString();
                cbPPI.Text = reader3[2].ToString();
                cbPPM.Text = reader3[3].ToString();
                ITAR = reader3[4].ToString();
            }

            if (ITAR == "True")
                chPITAR.CheckState = CheckState.Checked; //MessageBox.Show("ITAR checked");
                

            reader3.Close();
            db10.closeConnection();

but when the last line is executed it jumps to the checkchange event as shown below and gives and error saying data reader is close which is true.

private void chDITAR_CheckedChanged(object sender, EventArgs e)
        {

            if (chPITAR.CheckState == CheckState.Checked)
            {
                ClearITARCombo();

                String firstName;
                String lastName;
                String fullName;


                String query = @"Select FirstName,LastName From Employee Where Citizenship = 'US' ";
                SqlCommand comm90 = new SqlCommand(query, DB.getConnection());

                db10.openConnection();
                SqlDataAdapter da = new SqlDataAdapter(comm90);
                DataTable dt1 = new DataTable();

                da.Fill(dt1);

                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    firstName = dt1.Rows[i]["FirstName"].ToString();
                    lastName = dt1.Rows[i]["LastName"].ToString();
                    fullName = firstName + " " + lastName;

                    cbPPI.Items.Add(fullName);
                    cbPPW.Items.Add(fullName);
                    cbPPR.Items.Add(fullName);
                    cbPPM.Items.Add(fullName);


                }
                comm90.Dispose();
                da.Dispose();
                db10.closeConnection();
            }

what can i do to avoid jumping to the checkbox event ??? how can i check the check box without it triggerng to the event.

thanxx
please give me an solution

thanxx
appriciate a lot thanxxxxxxxxx

Hello again Krishnisilva,

Try detaching the event before setting CheckState:
chDITAR.CheckedChanged -= new EventHandler(chDITAR_CheckedChanged);

Then re-attach afterwards:
chDITAR.CheckedChanged += new EventHandler(chDITAR_CheckedChanged);

Nick.

Hello again Krishnisilva,

Try detaching the event before setting CheckState:
chDITAR.CheckedChanged -= new EventHandler(chDITAR_CheckedChanged);

Then re-attach afterwards:
chDITAR.CheckedChanged += new EventHandler(chDITAR_CheckedChanged);

Nick.

hey nick.crane,

where do i have to put u'r code

if (ITAR == "True")
        {  chPITAR.CheckState = CheckState.Checked;
            chPITAR.CheckedChanged -= new EventHandler(chDITAR_CheckedChanged);

            chPITAR.CheckedChanged += new EventHandler(chDITAR_CheckedChanged);

        }

this abouve code gives the same error
what shout i do
please help
thanxxxxxxxxxxxxx

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Do it like this:

chPITAR.CheckedChanged -= new EventHandler(chDITAR_CheckedChanged);
if (ITAR == "True")
{ 
   chPITAR.CheckState = CheckState.Checked;
}
chPITAR.CheckedChanged += new EventHandler(chDITAR_CheckedChanged);

Do it like this:

chPITAR.CheckedChanged -= new EventHandler(chDITAR_CheckedChanged);
if (ITAR == "True")
{ 
   chPITAR.CheckState = CheckState.Checked;
}
chPITAR.CheckedChanged += new EventHandler(chDITAR_CheckedChanged);

thanx
i realized after posting u a reply only

thanxxxxxxxxxx

Or this:

chPITAR.CheckedChanged -= new EventHandler(chDITAR_CheckedChanged);
chPITAR.Checked = (ITAR == "True");
chPITAR.CheckedChanged += new EventHandler(chDITAR_CheckedChanged);

Edited 6 Years Ago by nick.crane: n/a

Or this:

chPITAR.CheckedChanged -= new EventHandler(chDITAR_CheckedChanged);
chPITAR.Checked = (ITAR == "True");
chPITAR.CheckedChanged += new EventHandler(chDITAR_CheckedChanged);

k thankxx
i did it this way

This article has been dead for over six months. Start a new discussion instead.