Reputation: 809
protected void CompanyDropDown_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
using (SqlConnection cn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("[dropdownCompany]", cn);
cn.Open();
CompanyDropDown.DataSource = cmd.ExecuteReader();
CompanyDropDown.DataBind();
}
}
The stored procedure dropdownCompany is just one column with a list of company names.
For some reason this is not populating the drop down when I execute the web form.
Upvotes: 0
Views: 571
Reputation: 91
Before binding, specify DataTextField
and DataValueField
for the dropdown.
DataTextField
and DataValueField
should be the name of columns from the result set which you get after executing your stored procedure.
You should specify which field will be visible to the user in the dropdown. This will be your DataTextField
.
DataValueField
is not visible to the user. but you will need this value when you are performing some other operation based on the selected item in the dropdown.
CompanyDropDown.DataTextField = "Name";
CompanyDropDown.DataValueField = "ID";
CompanyDropDown.DataBind();
Upvotes: 1
Reputation: 10818
You will have to set the CommandType
property of your SqlCommand
object to CommandType.StoredProcedure
protected void CompanyDropDown_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
using (SqlConnection cn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("[dropdownCompany]", cn);
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
CompanyDropDown.DataSource = cmd.ExecuteReader();
CompanyDropDown.DataBind();
}
}
Upvotes: 0