Combobox Selected Index Changed event

    Date: 10/13/06 (C Sharp)    Keywords: database, sql

    Yes, I'm beating my same dead horse, of course. Now, I'm really confused. But at least my confusion is limited to a single control this time. C# 2005 standard, Windows XP Pro, tied to a SQL Server 2000 database.

    I have a control on a winform called cboName. Is is bound to a bindingsource called bsHosp, which goes back to a table called Hospitals. The bindingsource has a sort on it by name ascending, so everything in this box is in alphabetical order. Where I'm running into severe confusion is in the SelectedIndexChanged event (which has been the cause of all my woes over the past two weeks). The bindings on the combobox:
    SelectedItem: bsHosp.Name
    SelectedValue: bsHosp.ProvNo
    Tag: (none)
    Text: bsHosp.Name

    (ProvNo and Prov_ID

    In order to test the screwy databinding (check a few messages back in this community for a description), I put in a messagebox to check that the filter and sort on the other bindingsource were actually being changed. Here's the code for the SIC event:

    private void cboName_SelectedIndexChanged(object sender, EventArgs e)
            {
                try
                {
                    bsRate.Filter = "prov_id = " + txtProvNo.Text;
                    bsRate.Sort = "mexp desc";
                    bsRate.ResetBindings(false);
                    MessageBox.Show("Filter = " + bsRate.Filter.ToString() + "      Sort = " + bsRate.Sort.ToString());
                }
                catch (Exception ex)
                {
                }
                finally
                {
                }
            }


    (It's in the try block to prevent the program from throwing an exception when the form is closed.)

    Now, to show you what it's doing, I actually have to show you a piece of the table itself. Like I said, this is right weird.

    68267 Adams County Hospital
    62578 Bellevue Hospital
    4628 Belmont Pines Hospital

    Adams, being the first one on the list, is what pops in by default. Each of these contains one entry in the table.

    When I try to switch to Bellevue Hospital, this is what happens:

    1. Messagebox shows prov_id 68267, combobox shows Adams County Hospital.
    2. Messagebox shows prov_id 68267, combobox shows Bellevue Hospital.
    3. Messagebox shows prov_id 68267, combobox is blank.
    4. Messagebox shows prov_id 62578, combobox shows Belmont Pines Hospital.
    5. Messagebox shows prov_id 62578, combobox is blank.
    6. Messagebox shows prov_id 4628, combobox shows Belmont Pines Hospital.
    7. When I check the dropdown list in the combobox again, Adams County Hospital has disappeared and Bellevue Hospital is in the dropdown list twice. There is no change to the underlying table.

    I assume this behavior is not normal, especially since I'm not even getting the name I'm selecting in the textbox. Has anyone seen anything like this, and can you tell me why it's happening? And, preferably, how to fix it?

    * * *

    Variations:

    1. When I put the MessageBox command in the finally clause instead of the try clause, I get the same result.
    2. When I take out the try/catch clause altogether, again, the same thing happens.
    3. It makes no difference whether the ResetBindings command is commented out or not.

    Thanks.

    Source: http://community.livejournal.com/csharp/76871.html

« In an attempt to reinvent... || Adding an Event Handler in... »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home