I have a form in which I have a ComboBox control with two values.
Once the user selects one item in the list, I need to display a table of data corresponding to the option selected.
if (DropDownList1.SelectedValue == 1)
{
// open a SqlCommand
// read values
// fill table
}
else if (DropDownList1.SelectedValue == 2)
{
// etc
The problem is that I only want to fill the table after the user makes a selection. But when the form is loaded the ComboBox automatically chooses the first option and my code runs, filling the table.
How do I prevent this from happening?
what did you try so far?
there is no problem in having the table name coming from a DropDown, it’s more about understanding and designing the Data Access layer for your application. Do you have a save button in your WebForm and are you composing the SQL code dynamically in the code behind or using stored procedures?
starting point to connect to SQL Server and execute a command is generally similar to the following (if you do not use ORM like Entity Framework):
if you need more ideas about how to use EF (or simply a DAL technique) with ASP.NET (MVC or Web Forms), see my answer here: https://stackoverflow.com/a/7474357/559144