lundi 16 février 2015

How to Fill Comboboxes and Text Boxes Based on Multiple Combo Boxes Selected Values in C#

I have an SQL Table of Accounts[COA]. That Table has the following columns:



  • [Bus Unit] - Combobox

  • [BU Description] - TextBox

  • [Obj] - Combobox

  • [Obj Description] - TextBox

  • [Sub] - Combobox

  • [PEC] - TextBox


[Bus Unit] is bound to a combobox. A second combobox needs to be filled with [Obj] based on the [Bus Unit] combobox selected value and the third and final combobox needs to filled based on the [Obj] selected value. The remaining three columns are fields that need to populate text boxes based on the various selection of comboboxes.


For example: My [Bus Unit] combobox selected value is "10000". Now I need that selected index change to fill the [Obj] combobox with all the data associated with a [Bus Unit] of "10000". Not only that, but I also need it to fill a text box with [BU Description] that correlates directly with the [Bus Unit] of "10000".


The first two comboboxes will fill the following combobox as well as the following text box. The last combobox will fill the final textbox[PEC] as well as change the [Obj Description] text box if the value has changed.


I am struggling to figure out how to do all this. The following is what I have so far:



public void bindBusUnitToComboboxes()
{
SqlConnection cs = new SqlConnection(@"Data Source=MARYANNEBORJA\SQLEXPRESS;Initial Catalog=jdeDatabase;Persist Security Info=True;User ID=Jordan;Password=*******");
SqlDataAdapter daBU = new SqlDataAdapter("SELECT DISTINCT [Bus Unit] FROM COA", cs);

DataTable dt = new DataTable();
daBU.Fill(dt);

for (int i = 0; i < dt.Rows.Count; i++)
{
bus_UnitComboBox.Items.Add(dt.Rows[i]["Bus Unit"]);
}

bus_UnitComboBox.DisplayMember = "Bus Unit";
bus_UnitComboBox.SelectedIndex = 0;

}

private void bus_UnitComboBox_SelectedIndexChanged(object sender, EventArgs e)
{

if (bus_UnitComboBox.SelectedItem != null)
{
dv = jDEDatabaseDataSet.Tables[2].DefaultView;
dv.RowFilter = "[Bus Unit] = '" + bus_UnitComboBox.SelectedItem + "'";
objComboBox.DataSource = dv;
objComboBox.DisplayMember = "Obj";
objComboBox.ValueMember = "Obj";

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 + "'", connection);
connection.Open();

SqlDataReader read = command.ExecuteReader();

while (read.Read())
{
bU_DescriptionTextBox.Text = (read["BU Description"].ToString());
}
read.Close();
}
}
else { MessageBox.Show("Something is happening when you try to bind data to BUS UNIT COMBOBOX"); }

}

private void objComboBox_SelectedIndexChanged(object sender, EventArgs e)
{

dv = jDEDatabaseDataSet.Tables[2].DefaultView;
dv.RowFilter = "[Bus Unit] = '" + bus_UnitComboBox.SelectedItem + "' AND [Obj] ='" + objComboBox.Text + "'";
subComboBox.DataSource = dv;
subComboBox.DisplayMember = "Sub";
subComboBox.ValueMember = "Sub";

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 [Obj]='" + objComboBox.Text + "'", connection);
connection.Open();

SqlDataReader read = command.ExecuteReader();

while (read.Read())
{
obj_DescriptionTextBox.Text = (read["Obj Description"].ToString());
}
read.Close();
}

}

private void subComboBox_SelectedIndexChanged(object sender, EventArgs e)
{

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.GetString(8);
}
else
{
pECTextBox.Text = "";
}

obj_DescriptionTextBox.Text = (read["Obj Description"].ToString());
}
read.Close();
}

}


I am able to bind the [Obj] combobox and [Sub] combobox. The problem is that when I select a value in the [Obj] combobox it does not fill the [Obj Description] Textbox with the correct value every time. I have the same problem with the [Sub] Combobox trying to change the value of the [Obj Description] Textbox.


Aucun commentaire:

Enregistrer un commentaire