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:
| ID | Fund | Fund Description | Bus Unit | Bus Unit Description | Obj | Obj Description | Sub | PEC |
|---|---|---|---|---|---|---|---|---|
| 1 | 2789 | Account Refund | 10000 | General Fund | 1500 | ASC Voucher | 10 | |
| 2 | 2789 | Account Refund | 10000 | General Fund | 1500 | Voucher |
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?