I've spent a good bit of time searching the internet for an answer and can't come up with anything. Basically I have a very small database that consists of 5 tables total. My problem right now deals with just two of the them though. I have one table named Model (yes I know I did a bad job at naming this table. Going to try to rename it soon). Here is what Model looks like. Model table
Make ID refers to the unique ID in the table Makes. Here is what the table Make looks like. Make table
I have a windows form app that I created in C# using Visual Studios 2012. This database was created in that project. I have a form that has among other things, two combo boxes. The first one lists the info from the table Makes. It shows the 3 different car brands. The 2nd combo box shows the different models from the other table. I can get the first combo box to show all the Makes. I can get the 2nd combo box to show all the Models. But what I want is that if they select Ford in the first box, that it only shows the Fords in the 2nd box. When they select Ford in the first box, I need to somehow store the unique ID associated with Ford and then use it to filter the 2nd box by referencing the column Make ID in the Model table. I've done this in Access, but can't get it to work here. Here is my code I'm using to populate the first box.
private void enterNewVehcileForm_Load(object sender, EventArgs e)
{
vinAutoPopulateTextBox.Text = mainMenu.VIN;
mainMenu.connection.Open();
SqlCeCommand cs = new SqlCeCommand("SELECT * FROM Makes", mainMenu.connection);
SqlCeDataReader dr = cs.ExecuteReader();
while (dr.Read())
{
vehicleMakeComboBox.Items.Add(dr["Car Brand"]);
}
dr.Close();
dr.Dispose();
mainMenu.connection.Close();
}
This populates the first box just fine. I see Ford, Chevy, Dodge in the drop down box. But if I try this for the 2nd box, it doesn't work.
private void vehicleMakeComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int num = vehicleMakeComboBox.SelectedIndex;
mainMenu.connection.Open();
SqlCeCommand modelSearch = new SqlCeCommand("SELECT * FROM Model WHERE [Make ID] = @num", mainMenu.connection);
SqlCeDataReader modelRead = modelSearch.ExecuteReader();
while (modelRead.Read())
{
vehicleModelComboBox.Items.Add(modelRead["Model"]);
}
modelRead.Close();
modelRead.Dispose();
mainMenu.connection.Close();
I get an error at the line for while (modelRead.read())
It says a parameter is missing.
Can anyone help me or point me in the right direction. This is my first time messing with this, so it possible I did everything wrong.
Alright, I got it all working. I realized earlier that my logic was all messed up and I might not have explained myself very well. I did get it working, so here it is for anyone else that might find this and have the same issue.
This part populates the first combo box.
private void enterNewVehcileForm_Load(object sender, EventArgs e)
{
vinAutoPopulateTextBox.Text = mainMenu.VIN;
mainMenu.connection.Open();
SqlCeCommand cs = new SqlCeCommand("SELECT * FROM Makes", mainMenu.connection);
SqlCeDataReader dr = cs.ExecuteReader();
while (dr.Read())
{
vehicleMakeComboBox.Items.Add(dr["Car Brand"]);
}
dr.Close();
dr.Dispose();
mainMenu.connection.Close();
}
This part then filters the 2nd box based on what they picked in the first box.
private void vehicleMakeComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string ID = null;
string command = "SELECT * FROM Makes WHERE [Car Brand] = '" + vehicleMakeComboBox.Text + "'";
string command2 = null;
mainMenu.connection.Open();
SqlCeCommand makeSearch = new SqlCeCommand(command, mainMenu.connection);
// This part gets the ID of the car brand they picked in the first combo box. Ford is 1, Chevy is 2, Dodge is 3
SqlCeDataReader makeRead = makeSearch.ExecuteReader();
while (makeRead.Read())
{
ID = (makeRead["ID"].ToString());
}
makeRead.Close();
makeRead.Dispose();
vehicleModelComboBox.Items.Clear(); // Clears the combo box incase they picked a brand and then picked another
// This part now selects all rows in the Model table that have the same value in the Make ID column as the car brand they chose in the first combo box
command2 = "SELECT * FROM Model WHERE [Make ID] = " + ID;
SqlCeCommand modelSearch = new SqlCeCommand(command2, mainMenu.connection);
SqlCeDataReader modelRead = modelSearch.ExecuteReader();
while (modelRead.Read())
{
vehicleModelComboBox.Items.Add(modelRead["Model"]);
}
modelRead.Close();
modelSearch.Dispose();
mainMenu.connection.Close();
}
Thank you to @TomDoesCode and @Kami. You guys got me thinking in the right direction which made me see where my code was lacking.