ComboBox value as parameter for stored procedure?

D

Daniel Manes

Ok, I have a DataGridView, and I want to fill it with data from a
stored procedure (which is on a SQL server express database). This
procedure requires a parameter called "Country." The user selects the
country from a ComboBox elsewhere on the form.

All seems to be working fine except one thing: How do I get the value
from that Country ComboBox to be the value that gets used for the
Country parameter?

Do I need to do hand code this? Seems impossible with the GUI. There is
a "Parameters Collection" in the properties for the Fill query, but
when I go into that, there seems to be no way to bind to other
controls.

If I do need to write code for this, any tips?

Thanks,

-Dan
 
D

Dmytro Lapshyn [MVP]

Hi Daniel,

First of all, retrieve the lookup table of countries to a DataTable and bind
the ComboBox to that DataTable. Make sure you bind DisplayMember to the
country name data column and ValueMember to the country ID data column.

Now, when you get the combo box' SelectionChange event, just query the
SelectedValue for the selected country ID. Next, create a SqlCommand,
configure it with the stored procedure name, add the country ID parameter
and fill it with the retrieved ID value.

Finally, invoke the stored procedure and get the results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top