Click here to Skip to main content
15,903,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I run the below program the combobox disappear

foreach (Control control in this.Controls)

            {
                if (control is ComboBox)
                {
                    using (var tb = control as ComboBox)
                    {
                            SqlConnection con = new SqlConnection("Data Source = DELLXPS4; Initial Catalog = FingerPrintInfo; Integrated Security = True");
                            con.Open();
                            string str1 = "select * from " + tb.Tag.ToString();
                            SqlCommand cmd = new SqlCommand(str1, con);
                            SqlDataAdapter sda = new SqlDataAdapter(cmd);
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                            tb.ValueMember = tb.AccessibleDescription;
                            tb.DisplayMember = tb.AccessibleName;
                            tb.DataSource = dt;

                            con.Close();

                       
                    }
                }
            }

What I have tried:

<pre>                    using (var tb = control as ComboBox)
                    {
                            SqlConnection con = new SqlConnection("Data Source = DELLXPS4; Initial Catalog = FingerPrintInfo; Integrated Security = True");
                            con.Open();
                            string str1 = "select * from " + tb.Tag.ToString();
                            SqlCommand cmd = new SqlCommand(str1, con);
                            SqlDataAdapter sda = new SqlDataAdapter(cmd);
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                            tb.ValueMember = tb.AccessibleDescription;
                            tb.DisplayMember = tb.AccessibleName;
                            tb.DataSource = dt;

                            con.Close();

                       
                    }
                }
            }
Posted
Updated 28-Mar-18 20:38pm
Comments
Ehsan Sajjad 29-Mar-18 2:26am    
any exceptions you see? have you tried putting a breakpoint and see in debug mode?

It is probably because you are assigning 'tb' inside the using statement and it is getting disposed after you exit the using block which deletes the combo.

try

C#
if (control is ComboBox)
{
   var tb = control as ComboBox;
   using (SqlConnection con = new SqlConnection("Data Source = DELLXPS4; Initial Catalog = FingerPrintInfo; Integrated Security = True")
   {
      con.Open();
      ...
      ...
      ...
   }
 
Share this answer
 
Um.
You need to understand what a using block does:
It provides a scope for a variable and automatically calls Dispose on that variable's content when control leaves the block by whatever method.
So when you do this:
using (var tb = control as ComboBox)
You are saying "use this existing Control instance, and destroy it when I'm finished.

So the system does just that: your existing Control on the form releases all its resources - including it's Window handle and related information - when you leave the block.
And of course, that means it isn't a Control anymore, and it vanishes.

Only use using blocks with new class instances.
And if you use
if (a is B)
then you should just cast teh instance, not use as as well - as has to do an is test itself to check the cast so you are just repeating work!
C#
ComboBox cb = control as ComboBox;
if (cb != null)
   {
   ...
Is fine, and so is
C#
if (control is ComboBox)
   {
   ComboBox cb = (ComboBox) control;
   ...
But using both is inefficient.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900