Link to home
Start Free TrialLog in
Avatar of ANINDYA
ANINDYAFlag for India

asked on

ComboBox related Problem

Experts
I have a problem with the control movement in my code.
If you see the picture then you will be able to understand that .

I am attaching the full code here.

Thanking you ,
Anindya
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace oppurtunity
{
    public partial class SelectTechnologyForOpportunity : Form
    {
        public SelectTechnologyForOpportunity()
        {
            InitializeComponent();
        }
        Class_ConnectionString Connectionstring = new Class_ConnectionString();
        private void SelectTechnologyForOpportunity_Load(object sender, EventArgs e)
        {
            populate_combobox_withTechnology();
            //Show_Submenu();
        }

        private void Show_Submenu()
        {
            using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
                string query = "select SubTech,SubTechID,TechID from Table_SubTech where TechID='" + comboBox_tech.SelectedItem + "' ";
                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Table_SubTech");
                    dataGridView1.DataSource = ds.Tables[0];

                }
            }
        }
        private void populate_combobox_withTechnology()
        {
            using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
                string query = "select mainTech from Table_Technology";
                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Table_Technology");
                    comboBox_tech.DataSource = ds.Tables[0];
                    comboBox_tech.DisplayMember = "mainTech";
                    //comboBox_tech.ValueMember = "Value";
                }
            }

        }

        private void comboBox_tech_SelectedValueChanged(object sender, EventArgs e)
        {
            Show_Submenu();
        }

    }
}

Open in new window

error.JPG
Avatar of mamalik00
mamalik00
Flag of Pakistan image

The control should return to the next line of your breakpoint after returning from method Show_SubMenu().

It is okay that after assigning the datasource to the combo the selected value changed event will fire.
If you dont wat it to fire at this time try

ComboBox_tech.BeginUpdate();

ComboBox_tech.DataSource();
// other work

COmboBox_tech.EndUpdate();

Hope it helps.
Avatar of ANINDYA

ASKER

Expert mamalik00
I have tried your code but sir it is showing the same thing
please see the image.
Thanking you
Anindya
error.JPG
A work around would be to have a global boolean

bool ShowMenu = true;
private void populate_Combobox_withTechnology()
{
//your code here
ShowMenu = false;
combobox_tech.dataSource = ds.tables[0];
//your code
ShowMenu = true;
}

private void Combo_tech_selectedValueChanged(object sender .....)
{
 if(ShowMenu)
 {
  Show_Submenu();
 }
}
It will go the SelectedValue changed event but the method will not be fired because of the boolean so your flow will not be disturbed.

Event (comboboxValueChanged) is firing because after assigning the new datasource the value of the combobox is actually changed and so the event is triggered.

As I said above that the control will be returned to the next line of your break point after the completion of the eventHandler Combo_tech_selecetedValueChanged(...).
you may add Bindingsource control from tool box and
do the following things

      bindingSource1.DataSource = ds.tables[0];
    comboBox1.DataSource = bindingSource1.DataSource;
 
 or you can bind        bindingSource1 to comobox and desing  time and then   bind ds.table[0]  to run
 time   as shown in image
 
11.JPG
Avatar of ANINDYA

ASKER

Experts mamalik00 and Divyang4481
I have tried both of your suggestions but I do not know why It is happening.
 

here is the full code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace oppurtunity
{
    public partial class Form_Technology : Form
    {
        public Form_Technology()
        {
            InitializeComponent();
        }
        Class_ConnectionString Connectionstring = new Class_ConnectionString();
        bool showmenu = false;

        private void Show_Submenu()
        {

            using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
                string query = "select SubTech,SubTechID,TechID from Table_SubTech where TechID='" + comboBox_tech.SelectedItem + "' ";
                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Table_SubTech");
                    dataGridView1.DataSource = ds.Tables[0];

                }
            }
        }

        private void Form_Technology_Load(object sender, EventArgs e)
        {
            populate_combobox_withTechnology();
            if (showmenu)
            {

                Show_Submenu();
            }
        }

        private void populate_combobox_withTechnology()
        {
            using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
                string query = "select mainTech from Table_Technology";
                using (SqlDataAdapter ad= new SqlDataAdapter(query, con))
                {
                    showmenu = false;
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Table_Technology");
                    BindingSource bindingsource1=new BindingSource();
                    bindingsource1.DataSource = ds.Tables[0];
                    comboBox_tech.DataSource = bindingsource1.DataSource;
                    //comboBox_tech.DataSource = ds.Tables[0];
                    comboBox_tech.DisplayMember = "mainTech";
                    //comboBox_tech.ValueMember = "Value";
                    showmenu = true;
                }
            }

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FormAddTechnology fat = new FormAddTechnology();
            fat.ShowDialog();
            populate_combobox_withTechnology();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (comboBox_tech.SelectedItem != null)
            {
                string mainTech;
                mainTech = comboBox_tech.Text;
                Form_AddSubTechnology fast = new Form_AddSubTechnology(mainTech);
                fast.ShowDialog();
            }
            else
            {
                MessageBox.Show("Please select a Main Technology.\nThen you will be permitted to add Sub Technology", "Select Main Technology", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void comboBox_tech_SelectedValueChanged(object sender, EventArgs e)
        {
            //Show_Submenu();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Show_Submenu();
        }

       
    }
}


thanking you
anindya

error.JPG
Avatar of Naman Goel
Control will go to SelectedValueChanged event as you are updating DataSource and if there will be any change in datasource it will call selectedValueChanged event.
attached code will be called when you are updating datasource you can try for the workaround suggested by mamalik00, That should work...
 
 
 

private void SetDataConnection(object newDataSource, BindingMemberInfo newDisplayMember, bool force)
{
    bool flag = this.dataSource != newDataSource;
    bool flag2 = !this.displayMember.Equals(newDisplayMember);
    if (!this.inSetDataConnection)
    {
        try
        {
            if ((force || flag) || flag2)
            {
                this.inSetDataConnection = true;
                IList list = (this.DataManager != null) ? this.DataManager.List : null;
                bool flag3 = this.DataManager == null;
                this.UnwireDataSource();
                this.dataSource = newDataSource;
                this.displayMember = newDisplayMember;
                this.WireDataSource();
                if (this.isDataSourceInitialized)
                {
                    CurrencyManager manager = null;
                    if (((newDataSource != null) && (this.BindingContext != null)) && (newDataSource != Convert.DBNull))
                    {
                        manager = (CurrencyManager) this.BindingContext[newDataSource, newDisplayMember.BindingPath];
                    }
                    if (this.dataManager != manager)
                    {
                        if (this.dataManager != null)
                        {
                            this.dataManager.ItemChanged -= new ItemChangedEventHandler(this.DataManager_ItemChanged);
                            this.dataManager.PositionChanged -= new EventHandler(this.DataManager_PositionChanged);
                        }
                        this.dataManager = manager;
                        if (this.dataManager != null)
                        {
                            this.dataManager.ItemChanged += new ItemChangedEventHandler(this.DataManager_ItemChanged);
                            this.dataManager.PositionChanged += new EventHandler(this.DataManager_PositionChanged);
                        }
                    }
                    if (((this.dataManager != null) && (flag2 || flag)) && (((this.displayMember.BindingMember != null) && (this.displayMember.BindingMember.Length != 0)) && !this.BindingMemberInfoInDataManager(this.displayMember)))
                    {
                        throw new ArgumentException(SR.GetString("ListControlWrongDisplayMember"), "newDisplayMember");
                    }
                    if (((this.dataManager != null) && ((flag || flag2) || force)) && (flag2 || (force && ((list != this.dataManager.List) || flag3))))
                    {
                        this.DataManager_ItemChanged(this.dataManager, new ItemChangedEventArgs(-1));
                    }
                }
                this.displayMemberConverter = null;
            }
            if (flag)
            {
                this.OnDataSourceChanged(EventArgs.Empty);
            }
            if (flag2)
            {
                this.OnDisplayMemberChanged(EventArgs.Empty);
            }
        }
        finally
        {
            this.inSetDataConnection = false;
        }
    }
}

 

 

Open in new window

Did you write the following code as well

private void Combo_tech_selectedValueChanged(object sender .....)
{
 if(ShowMenu)
 {
  Show_Submenu();
 }
}

Also note that

Program flow will still go the SelectedValue changed event but the method will not be fired because of the boolean so your flow will not be disturbed.

Event (comboboxValueChanged) is firing because after assigning the new datasource the value of the combobox is actually changed and so the event is triggered.

As I said above that the control will be returned to the next line of your break point after the completion of the eventHandler Combo_tech_selecetedValueChanged(...).
Avatar of ANINDYA

ASKER

Experts
I am trying but not getting the required result.
If it is compulsory that the SelectedValueChanged event will fire when the datasource in the combobox will change.

If so then you only tell me how can I solve it.
I thought it is a mistake from my side and perhaps it is a silly mistake but now I am seeing it is not a simple one .
can you provide me any solution.

that is there is a combobox which is populating data (display members) and the datagridview will populate data on the basis of the data which is populated in combobox.

I think it is a common issue .
But why it is getting so yough I do not know.
Thanking you
Anindya
Hi,

Explain your code requirements or objective clearly. And I will try to work on that. Because code modifications suggestions have been provided to you already.

Let us develop it from scratch.

Regards,
V.S.Saini
now  you must try  this
 this will solved you problem

1)  remove  event  handler of selection change
2) bind datasource
3) add event handler of selection change


this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedIndexChanged);


bindingSource1.DataSource = ds.tables[0];
    comboBox1.DataSource = bindingSource1.DataSource;

this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);





 
to avoid this you have to play with
 add/remove  of event handler only

this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedIndexChanged);


bindingSource1.DataSource = ds.tables[0];
    comboBox1.DataSource = bindingSource1.DataSource;

this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

Open in new window

The way I see your code is fine, in your question body.
Use different variables.

//in Show_Submenu
DataSet ds1 = new DataSet();

//in combo selected text
DataSet ds2 = new DataSet();


>>comboBox_tech.DisplayMember = "mainTech";
Be sure "mainTech" is the right spelling in your database field, it could be "maintecht" only
Hmm.. looks like a case where the code in the debugger does not match the compiled code (out of sync).

I would recommend a solution REbuild. Right click on solution and click rebuild. That should update all your PDB files (if the PDB files aren't up to date then your debugger will do weird stuff like skip lines because they've only just recently been added).

No harm trying.
Hi;
This is base on your original code on the question body.

I've change a few codes that will work as you wish and as I understand.
The code attach below works fine on my multiple test.  I hope you'll accept that as the answer.

Related answer to this question is on this link;
https://www.experts-exchange.com/blogs/systan/B_2926-Binding-different-datasource-to-different-control.html

Systan
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace oppurtunity
{
    public partial class SelectTechnologyForOpportunity : Form
    {


        public SelectTechnologyForOpportunity()
        {
            InitializeComponent();
        }




//I DONT KNOW where you got that Class, but the important is the connection is open.        
Class_ConnectionString Connectionstring = new Class_ConnectionString();



        private void SelectTechnologyForOpportunity_Load(object sender, EventArgs e)
        {
            comboBox_tech.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox_tech.SelectedValueChanged += new System.EventHandler(comboBox_tech_SelectedValueChanged);

            populate_combobox_withTechnology();

            comboBox_tech.Select();
        }


        //I WOULD like you to noticed that <Connectionstring.Connection> was replaced by  <Connectionstring> only, because I don't know what's the inside of your class.  Just change it if it is the procedure you wanted.  

        private void Show_Submenu()
        {
            using (SqlConnection con = new SqlConnection(Connectionstring))
            {
                string query = "select SubTech,SubTechID,TechID from Table_SubTech where TechID='" + comboBox_tech.SelectedItem + "' ";
                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {
                    //USE dataset if your basing multiple join tables 
                    //DataSet ds = new DataSet();
                    //ad.Fill(ds, "Table_SubTech");
                    //dataGridView1.DataSource = ds.Tables[0];
   
                    //USE dataTable, while you are only basing 1 table, FOR small memory consumption
                    DataTable dt = new DataTable();
                    ad.Fill(dt);
                    dataGridView1.DataSource = dt;

                }
            }
        }


        private void populate_combobox_withTechnology()
        {
            using (SqlConnection con = new SqlConnection(Connectionstring))
            {
               // i OPENED the connection, because I don't know if you have already open it in your class
               con.Open(); 

               // i CHANGED  mainTech TO TechID, because it doesn't reflect to the datagridview datasource              
                string query = "select  TechID from Table_Technology";

                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {

                    SqlDataReader dr = ad.ExecuteReader();
                    if (!dr.HasRows) return; 
                    while (dr.Read())
                    {
                        comboBox_tech.Items.Add(dr.GetValue(0));  
                    }
                    dr.Close();
                    comboBox_tech.SelectedIndex=0; 

                }
            }

        }

        private void comboBox_tech_SelectedValueChanged(object sender, EventArgs e)
        {
            Show_Submenu();
        }


    }
}

Open in new window

Avatar of ANINDYA

ASKER

Expert Systan
here is the code of the Class_ConnectionString

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oppurtunity
{
    class Class_ConnectionString
    {
        string _connection = "Data Source=COM2\\SQLEXPRESS; Initial Catalog=Opportunity;Integrated Security=SSPI";

        public string Connection
        {
            get
            {
                return _connection;
            }
            set
            {
                _connection = value;
            }
        }
    }
}
Ah, Ok;
If it is already open, then remove the <con.Open();>

            using (SqlConnection con = new SqlConnection(Connectionstring))
            {
               // i OPENED the connection, because I don't know if you have already open it in your class
=====>   con.Open();  //remove this line

and
restore back <Connectionstring.Connection>
using (SqlConnection con = new SqlConnection( =====> Connectionstring.Connection))

You must know the connection if it is already open or not, so you can Identify whick code to change.;
I hope you have read the related link I posted for more understanding.
Avatar of ANINDYA

ASKER

Expert Systan
I am seeing your code also.
Thanking you
The last code I attached [ID: 33462064 ],  just replace;

FROM
using (SqlConnection con = new SqlConnection(Connectionstring))

TO
using (SqlConnection con = new SqlConnection(Connectionstring.Connection))

and
Remove con.Open();    but be sure where your connection has openned.


Just like that, test it, let me hear it.
Ops, I tried it again;
DO NOT remove < con.Open(); > because your [coding system] = [the way you code] does not open when form loads, but it open's when a first command is handled.  So, lets stick to your coding, after a few research you may know what's the right usage, but depending on developer's coding style.


The last code I attached [ID: 33462064 ],  just replace;
FROM
using (SqlConnection con = new SqlConnection(Connectionstring))

TO
using (SqlConnection con = new SqlConnection(Connectionstring.Connection))


And it run's fine,  I think this subject is closed.
Avatar of ANINDYA

ASKER

Expert Systan
please see the image attached.
Also see the code attached below.
Thanking you,
anindya
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace oppurtunity
{
    public partial class Form_Technology : Form
    {
        public Form_Technology()
        {
            InitializeComponent();
        }
        Class_ConnectionString Connectionstring = new Class_ConnectionString();
        bool showmenu = false;

        private void Show_Submenu( )
        {

            using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
                string query = "select Table_SubTech.SubTech,Table_SubTech.SubTechID,Table_SubTech.TechID,Table_Technology.mainTech from Table_SubTech inner join Table_Technology on Table_Technology.TechID=Table_SubTech.TechID where Table_Technology.mainTech='" + comboBox_tech.SelectedItem.ToString() + "' ";
                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Table_SubTech");
                    dataGridView1.DataSource = ds.Tables[0];

                }
            }
        }

        private void Form_Technology_Load(object sender, EventArgs e)
        {
            
            populate_combobox_withTechnology();
            //string mainTech;
            //mainTech = comboBox_tech.SelectedValue.ToString();
            //get_TechID(mainTech);
            //Int32 TechID;
            //TechID = Convert.ToInt32(label3.Text);
            Show_Submenu();
            //populatecombobox1();
        }

        private void populatecombobox1()
        {
            SqlConnection con=new SqlConnection(Connectionstring.Connection);
            //string q="select SubTech where 

        }

        private void populate_combobox_withTechnology()
        {
            using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
                string query = "select mainTech from Table_Technology";
                using (SqlDataAdapter ad= new SqlDataAdapter(query, con))
                {
                    //showmenu = false;
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Table_Technology");
                    //List<string> MainTechName = new List<string>();
                    //MainTechName = AddValuetoList(ds.Tables[0]);

                    //textBox_tech.AutoCompleteCustomSource.AddRange(MainTechName.ToArray());
                    //textBox_tech.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    //textBox_tech.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    comboBox_tech.DataSource = ds.Tables[0];
                    comboBox_tech.DisplayMember = "mainTech";


                }
            }

        }

        private List<string> AddValuetoList(DataTable dataTable)
        {
            List<string> str = new List<string>();
            foreach (DataRow row in dataTable.Rows)
            {
                foreach (DataColumn col in dataTable.Columns)
                {
                    str.Add(row[0].ToString());
                }
            }
            return str;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FormAddTechnology fat = new FormAddTechnology();
            fat.ShowDialog();
            populate_combobox_withTechnology();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (comboBox_tech.SelectedItem != null)
            {
                string mainTech;
                mainTech = comboBox_tech.Text;
                Form_AddSubTechnology fast = new Form_AddSubTechnology(mainTech);
                fast.ShowDialog();
            }
            else
            {
                MessageBox.Show("Please select a Main Technology.\nThen you will be permitted to add Sub Technology", "Select Main Technology", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void comboBox_tech_SelectedValueChanged(object sender, EventArgs e)
        {
            //string mainTech;
            //mainTech = comboBox_tech.Text;
            //get_TechID(mainTech);
            //Int32 TechID;
            //TechID = Convert.ToInt32(label3.Text);
            Show_Submenu();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Show_Submenu();
        }

        private void comboBox_tech_MouseHover(object sender, EventArgs e)
        {
            //string mainTech;
            //mainTech = comboBox_tech.SelectedValue.ToString();
            //get_TechID(mainTech);
            //Show_Submenu();
        }
        
        private void get_TechID(string mainTech)
        {
            using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
                
                string query = "select TechID from Table_Technology where mainTech=@parammainTech";
                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    cmd.Parameters.AddWithValue("@parammainTech", mainTech);
                    con.Open();
                    SqlDataReader readerd = cmd.ExecuteReader();
                    
                    while (readerd.Read())
                    {
                        label3.Text = string.Format("{0}", readerd[0]);
                    }
                }
            }
            
        }
        Dictionary<int, Class_SelectTechnology> TechSelectDictionList = new Dictionary<int, Class_SelectTechnology>();
        Class_SelectTechnology st = new Class_SelectTechnology();

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            
        }
        public Int32 techID { get; set; }
        public Int32 SubtechID { get; set; }
        Dictionary<int, int> TechSubTechDictionaryList = new Dictionary<int, int>();
        public Dictionary<int, int> TechSubTech
        {
            get
            {
                return TechSubTechDictionaryList;
            }
            set
            {
                TechSubTechDictionaryList = value;
            }
        }
        public Int32 no = 0;
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //st._TechID=Convert.ToInt32(dataGridView1.CurrentRow.Cells[2].Value.ToString());
            //st._SubTechID=Convert.ToInt32(dataGridView1.CurrentRow.Cells[1].Value.ToString());
            try
            {
                if ((dataGridView1.CurrentRow.Cells[1].Value != null) || (dataGridView1.CurrentRow.Cells[1].Value.ToString() != "0"))
                {
                    techID =Convert.ToInt32( dataGridView1.CurrentRow.Cells[2].Value.ToString());
                    SubtechID =Convert.ToInt32( dataGridView1.CurrentRow.Cells[1].Value.ToString());
                    TechSubTechDictionaryList.Add(SubtechID,techID );

                }
                else
                {
                    MessageBox.Show("Please select a Sub Technology", "Select SubTechnology", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message);
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            no = 1;
            this.Close();
        }

        
    }
}

Open in new window

error.JPG
Avatar of ANINDYA

ASKER

Expert Systan
I think the image is not attached so please see here.

error.JPG
Avatar of ANINDYA

ASKER

a
1.JPG
THERE ARE to much image on my sight,     I'd like you to     copy and paste the code that I attached.      That is based from your original code attached at your question body code snippet.    That code was tested multiple times on my computer.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace oppurtunity
{
    public partial class SelectTechnologyForOpportunity : Form
    {

        public SelectTechnologyForOpportunity()
        {
            InitializeComponent();
        }


      Class_ConnectionString Connectionstring = new Class_ConnectionString();


        private void populate_combobox_withTechnology()
        {
          using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
               con.Open(); 

                string query = "select  TechID from Table_Technology";

                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {

                    SqlDataReader dr = ad.ExecuteReader();
                    if (!dr.HasRows) return; 
                    while (dr.Read())
                    {
                        comboBox_tech.Items.Add(dr.GetValue(0));  
                    }
                    dr.Close();
                    comboBox_tech.SelectedIndex=0; 

                }
            }

        }


        private void Show_Submenu()
        {
          using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
                string query = "select SubTech,SubTechID,TechID from Table_SubTech where TechID='" + comboBox_tech.SelectedItem + "' ";
                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {
                    DataTable dt = new DataTable();
                    ad.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
        }


        private void comboBox_tech_SelectedValueChanged(object sender, EventArgs e)
        {
            Show_Submenu();
        }

        private void SelectTechnologyForOpportunity_Load(object sender, EventArgs e)
        {
            comboBox_tech.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox_tech.SelectedValueChanged += new System.EventHandler(comboBox_tech_SelectedValueChanged);

            populate_combobox_withTechnology();

            comboBox_tech.Select();
        }



    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of systan
systan
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In-case you have followed, but still remains an error,
because of this lines ONLY; I was thinking it was changed, SORRY for that;

FROM (void populate_combobox_withTechnology)
using (SqlDataAdapter ad = new SqlDataAdapter(query, con))

TO (void populate_combobox_withTechnology)
using (SqlCommand  ad = new SqlCommand(query, con))


SEE   my   L.A.S.T   code   attached    A.B.O.V.E(with simple Form1,comboBox1,dataGridView1)   or   on    this   ID    B.E.L.O.W(base upon your original code at your question body).

Wheww!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace oppurtunity
{

    //class Class_ConnectionString
    //{
    //    string _connection = "Data Source=COM2\\SQLEXPRESS; Initial Catalog=Opportunity;Integrated Security=SSPI";

    //    public string Connection
    //    {
    //        get
    //        {
    //            return _connection;
    //        }
    //        set
    //        {
    //            _connection = value;
    //        }
    //    }
    //}


    public partial class SelectTechnologyForOpportunity : Form
    {

        public SelectTechnologyForOpportunity()
        {
            InitializeComponent();
        }


      Class_ConnectionString Connectionstring = new Class_ConnectionString();


        private void populate_combobox_withTechnology()
        {
          using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
               con.Open(); 

                string query = "select  TechID from Table_Technology";

//BECAUSE OF THIS LINES.....where very far, but it's good now.
using (SqlCommand  ad = new SqlCommand(query, con))
                {
                    SqlDataReader dr = ad.ExecuteReader();
                    if (!dr.HasRows) return; 
                    while (dr.Read())
                    {
                        comboBox_tech.Items.Add(dr.GetValue(0));  
                    }
                    dr.Close();
                    comboBox_tech.SelectedIndex=0; 

                }
            }

        }


        private void Show_Submenu()
        {
          using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
                string query = "select SubTech,SubTechID,TechID from Table_SubTech where TechID='" + comboBox_tech.SelectedItem + "' ";
                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {
                    DataTable dt = new DataTable();
                    ad.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
        }


        private void comboBox_tech_SelectedValueChanged(object sender, EventArgs e)
        {
            Show_Submenu();
        }

        private void SelectTechnologyForOpportunity_Load(object sender, EventArgs e)
        {
            comboBox_tech.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox_tech.SelectedValueChanged += new System.EventHandler(comboBox_tech_SelectedValueChanged);

            populate_combobox_withTechnology();

            comboBox_tech.Select();
        }



    }
}

Open in new window

Avatar of ANINDYA

ASKER

Expert Systan
You are really fabulous .I have been a member of Expert Exchange for the last 6 months but I have not seen anyone like you.
Although you are thousands of miles away from India but you can grab every bit of points .
Expert Systan I have created a new winform and executed your code and it is working  fine.

I am grateful to you  for rendering so much of help.
Expert Systan I have a request now and onwards you have to solve my queries if you have time .
You explain so well that it reminds me the my B.Tech  College days lecturers.

I want you to write some blogs also .
If you have some time.
Although It is a request but Expert Systan I want all should get benefited from your blogs.

Expert Systan I am a fresher and I know where the freshers face the problems.
if you have a bit of time please write in a lucid and placid way so that all the freshers can understand .

I am providing you some headings if you have a bit of time please write

1. 3-Tier architechture --How to do in winform and as well as in Web form from scratch
2. LINQ with Stored Procedure.(winform and Web form)

Please Expert Systan  do this favor to all the freshers.

Thanking you ,
Hope you will sympathetically consider this request of a fresher sitting thousands of miles away .

Anindya Chatterjee
Bangalore
India
I though you have the knowledge now.
That is your post, and there are links which leads you to the exact point of what you wanted.
https://www.experts-exchange.com/questions/26256116/How-3-Tier-architechture-to-be-created-ASP-net.html

BUT you are asking for a detailed code of:
1. 3-Tier architechture --How to do in winform and as well as in Web form from scratch
2. LINQ with Stored Procedure.(winform and Web form)

Ah, Ok;
I'll try some other day, and inform you when I'll see your comments on the other or some post.
Avatar of ANINDYA

ASKER

Expert Systan
That was my post only but it is not clear as there is nothing step by step approach.
I asked for the moderator to intervene in that matter.
But moderator replied to me that that will be big chapter which can not be considered as a solution to query rather it will be a blog (if some one write so).
There after I personally asked one expert (his username is Idle_mind)............but he also had not written so.
That is why I asked you .
Thanking you,
Actually those are too important for me to do.
Anindya Chatterjee
Bangalore
India