I want to directly display list of contact name using drop down list when company name is selected from the company list in a drop down list in the same interface. I have no idea how to do this.

I am using C#.Net and SQL database.

I try to use SqlDataSource configuration using WHERE and Control. But, it didn't work. Please help me...

Dani AI

Generated

The error comes from the SQL string and the method used to run it. In your posted code the literal text companyName ends up inside the SQL, so SQL Server treats it as a column name (hence "Invalid column name 'companyName'"). Also ExecuteNonQuery() is for INSERT/UPDATE/DELETE — not for SELECT results. pointed to using a SelectedIndexChanged handler and correctly noted the missing parameter; both are on the right track, but a few practical steps will make this reliable.

Key steps to fix and harden the solution:

  • Populate the company dropdown on first load only (wrap in if (!IsPostBack)).
  • Set the company dropdown AutoPostBack = true and wire SelectedIndexChanged.
  • In the handler, use a parameterized SELECT (no string concatenation) and fetch rows with a SqlDataReader or SqlDataAdapter/DataTable. Do not use ExecuteNonQuery() for SELECT.
  • Bind the results to the contact dropdown by setting DataSource, DataTextField, DataValueField, then calling DataBind().
  • Always use parameters to avoid SQL injection and to avoid quoting/escaping bugs.

Example pattern (conceptual) to use in the SelectedIndexChanged handler:

protected void CompanyList_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedCompany = CompanyList.SelectedValue;
    if (string.IsNullOrEmpty(selectedCompany)) { ContactList.Items.Clear(); return; }

    string sql = "SELECT contact_id, contact_name FROM Contact WHERE company_name = @company ORDER BY contact_name";
    // create connection/command, add parameter @company = selectedCompany
    // fill DataTable with SqlDataAdapter or use SqlDataReader
    // set ContactList.DataSource, DataTextField = "contact_name", DataValueField = "contact_id", then DataBind()
}

Troubleshooting checklist: confirm the event is actually hooked (markup or code-behind), check the connection string, clear the contact dropdown before binding, and test the SQL with a known value in SQL Server Management Studio. If you prefer no code-behind, a SqlDataSource with a ControlParameter pointing to the company dropdown will do the same filtering — but it must reference the correct control and parameter name.

Recommended Answers

All 4 Replies

I want to directly display list of contact name using drop down list when company name is selected from the company list in a drop down list in the same interface. I have no idea how to do this.

I am using C#.Net and SQL database.

I try to use SqlDataSource configuration using WHERE and Control. But, it didn't work. Please help me...

AS per my understanding, You need to handle the SelectedIndexChanged event of Comapany name dropdown. In this event you need to fetch the data from database table related to Contact name for a selected Comapny name, then assign the data in data table and bind your contact name drop down with data table.
Also set the AutoPostBack = true to Company Name drop down.
It will work for you :-)

search for Binding Dropdownlist in google
you'll get the code snippets too...

AS per my understanding, You need to handle the SelectedIndexChanged event of Comapany name dropdown. In this event you need to fetch the data from database table related to Contact name for a selected Comapny name, then assign the data in data table and bind your contact name drop down with data table.
Also set the AutoPostBack = true to Company Name drop down.
It will work for you :-)

Actually, the drop down list for company name is displaying company name from company table. For the contact drop down list, i want to display it from the contact table where the company name for the contact is equal to value that is selected from the company name drop down list. I try to do C# coding for that. But, i got error. Actually, i don't know how to do it. But, i just try in error. Here, is my coding:

protected void CompanyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SQLConn = myDB.OpenDB();

            String companyName = CompanyList.SelectedItem.Value;

            String displayContact = "SELECT (contact_name) FROM Contact WHERE company_name = companyName";
            SqlCommand cmd = new SqlCommand(displayContact, SQLConn);
            int returnValue = cmd.ExecuteNonQuery();

            myDB.CloseDB();
        }

The error that occur at cmd.ExecuteNonQuery is:
Invalid column name 'companyName'.

that's a correct error..
you are not providing any parameter value to company_name

it should be company_name = ' "+your value which fits for company name+" '

hope that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.