hi all;
again i have a little bit wired problem here

what i'm trying to achive here is that i would like to to a search query to search an employee and after that change the depatment and after that update the data into access database and as you can see in the below pic

i'm using the search query in the text changed event in the text box to search for the employee after after that i'm using te following code to refile the combobox

AValue, string Adisplay)
        {
            string Value = AValue;
            string display = Adisplay;

            using (var CBConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\AMS.mdb;"))
            {
                CBConn.Open();
                DataTable CbDt = new DataTable();
                try
                {
                    OleDbCommand CBCmd = new OleDbCommand(Query, CBConn);
                    OleDbDataReader CBReader = CBCmd.ExecuteReader();
                    CbDt.Load(CBReader);
                }
                catch (OleDbException e)
                {
                    MessageBox.Show(e.ToString());

                    return;
                }
                DropDownName.DataSource = CbDt;
                DropDownName.ValueMember = Value;
                DropDownName.DisplayMember = display;
            }
        }

and after that i pass thefollowing to the methode here

 FillDropDownList("SELECT empcode,empname from Deparment WHERE TypeCode = '" + emp.Text + "'", TypeEmp_CB, "empcode", "empname");

and i tried recalling it one more time by using it in a different to refill the combobox with all the deparments so that the user will be able to select a different deparment like the below

 FillDropDownList("SELECT empcode,empname from Deparment ", TypeEmp_CB, "empcode", "empname");

how ever scine all of that is running during the text changed event it the text is refilled but the selected item is the fisrt item just in the table not the correct deparment of the employee so anby ideas

Attachments Combo.png 5.72 KB

TextChanged fires every time the text changes, which means you're doing a lot of unnecessary work. Keeping it simple, I'd add a search button that fills in your combo boxes so you have more control over exactly when it happens.

Following that, unless you have strong binding going down, you'll need to populate child combo boxes and explicitly select the correct item.

@decepikon

i did add the search button but my issue still the same what i would like to do is to get the correct depatment selected after the search and able to view all ather deparment also for future update

As I said before, if you're not binding these controls to a data set with foreign keys, you need to manually get the correct department from the query result and set the combo box in code.

Firstly you should be using a parameterized query. Do a quick search on that and you'll know how its used.
Coming back to the issue at hand.
step 1: enter employee name in text box and then hit the search button
step 2: Populate a label to display the current dept

select dept from employee where empname = 'xyz'

step 3: Populate a combo box with the list of all depts

select distinct dept from employee

iterate through the result to populate the combo box

step 4: select a dept from the drop down and then hit a save button

update employee
set dept = 'newdept'
where empname = 'xyz'

I have taken some liberties here w.r.t the table in the database since I do not know how ur data structure is. But this is the general idea.

You can initialize and fill the details of the combobox after the InitializeComponent() method in your form constructor.
That should solve your problem, call this everytime will just refresh your combobox and there is no place where your previous actions are saved like in your web pages.

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