Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Binding textbox to combobox selected value when the selected value is blank

$
0
0

I have a simple form that binds data to a combobox and when one of the values in the combobox is selected it fills a textbox with the corresponding column in that table.  However, if that combobox value is empty(meaning that table cell is empty) then the textbox will not fill with the correct value.  I am finding this hard to explain so I will lay it out.

The table[COA] is as follows:

IDFundFund DescriptionBus UnitBus Unit DescriptionObjObj DescriptionSubPEC
12789Account Refund10000General Fund1500ASC Voucher10
22789Account Refund10000General Fund1500Voucher

The following is my code:

private void subComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            pECTextBox.Clear();

                using (SqlConnection connection = new SqlConnection(@"Data Source=MARYANNEBORJA\SQLEXPRESS;Initial Catalog=jdeDatabase;Persist Security Info=True;User ID=Jordan;Password=*****"))
                {
                    SqlCommand command = new SqlCommand("SELECT * FROM [COA] WHERE [Bus Unit]='" + bus_UnitComboBox.Text + "' AND [Obj] ='" + objComboBox.Text + "'AND [Sub] ='" + subComboBox.Text + "'");
                    command.Connection = connection;
                    connection.Open();

                    SqlDataReader read = command.ExecuteReader();

                    while (read.Read())
                    {
                        if (!read.IsDBNull(8))
                        {
                            pECTextBox.Text = (read["PEC"].ToString());
                        }
                        else
                        {
                            pECTextBox.Text = "";
                        }
                        obj_DescriptionTextBox.Text = (read["Obj Description"].ToString());

                    }
                    read.Close();
                }

        }

I fill my combobox with Sub values based on the Bus Unit and Obj.  Since the Bus Unit and the Obj in both rows are the same the combobox is filled with 10 and "".  

Here is where the problem occurs.  When I select "10" in the combobox I want a textbox to fill with correct Obj Description field.  So selecting "10" would give me 'ASC Voucher' in the textbox and selecting "" should give me 'Voucher' in the textbox, but this is not the case.  I can only get 'ASC Voucher' to fill the textbox.  When I select "" it keeps the textbox at 'ASC Voucher'.

My guess us that is has something to do with the value being empty in that table cell because when I am selecting other values it works.  

Any guess as to what I should do?



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>