Not positive on your question here. First, I'll make a few assumptions...1) you are using WinForm 2) for the sake of example, the combo box that presents the choices 2017 and 2018 is named
comboBox2
.
In that case, after your login is complete, I'd do the following. Select a connection string based on the value of
comboBox2.SelectedIndex
. For example:
string cs = comoBox.SelectedIndex == 1 ? connectionString2018 : connectionString2017;
Then, I would set the data source for your other combo box as follows (passing cs as a parameter):
comboBox1.DataSource = GetDatabaseList(cs);
Alternatively, if the 2017/2018 combo box appears elsewhere, you could also subscribe to the
SelectedIndexChanged
event and initialize the
DataSource
(as described previously) in that event.
UPDATE:
I notice now that I have misread your question and that your
GetDatabaseList
function is actually getting the list of database names, to populate your database selection list.
If you really only have the possibility of 2017 and 2018, hard code them in the combo box and select a connection string as described above.
If, instead, you need to support a random number of databases, you can populate the database list as you currently do and subscribe to the
SelectedIndexChanged
event to (somehow) get a connection string for that database.
I would consider this a poor design. If you have a bit more flexibility, consider moving to a single database that is not year-specific. Then, add a new table
PartnersByYear
that has a
year
column and a
partnerId
column.
Then, with a fairly basic
INNER JOIN
, between
partnersByYear
and
partners
you can select the partners for the year that they participate, using a combo box to get the year in the
WHERE
clause. This would be a lot less cumbersome than maintaining a bunch of separate databases.