0
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.

2
  • Have you tried executing the stored procedure in SQL Server Management Studio to see if there is actually data being returned? Commented Mar 8, 2017 at 18:03
  • Yeah the stored procedure populated the results in sql server management studio Commented Mar 8, 2017 at 18:28

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

11 Comments

this is my sql stored procedure: SELECT distinct companyid, companyname from Company
Did you change the values from Name and ID to companyname and companyid?
Yep: CompanyDropDown.DataSource = cmd.ExecuteReader(); CompanyDropDown.DataTextField = "companyname"; CompanyDropDown.DataValueField = "companyid"; CompanyDropDown.DataBind();
Btw, why is your code in Dropdown's click event? You want the dropdown to be populated when u click on it? Or when the page is loading?
I want the drop down to be populated when you click on it, but would it make a difference if its when the page is loaded and then the user clicks on it?
|
0

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();
    }
}

1 Comment

I have added this line also: cmd.CommandType = CommandType.StoredProcedure;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.