Pradeep Ch
Pradeep Ch

Reputation: 103

I want to bind database table to dropdownlist without duplicates

I set DataTextField and DataValueField to columns in the database table.

The DataValueField will be Value for the dropdownlist and DataTextField is text for the dropdownlist.

The table has repeating values. The dropdownlist also shows the duplicate values that are present in the text field. I want unique values in the dropdownlist although the DataTextfield has repeating values.

Below is my code so far:

SqlConnection con = new SqlConnection(CS);

SqlCommand cmd = new SqlCommand("Select DoctorId, Location from DoctorR", con);
con.Open();

ddlLocation.DataSource= cmd.ExecuteReader();
ddlLocation.DataTextField = "Location";
ddlLocation.DataValueField = "DoctorId";
ddlLocation.DataBind();

ListItem liLocation = new ListItem("Select Location", "-1");
ddlLocation.Items.Insert(0, liLocation);

con.Close();

The Location column has repeating values. I do not want duplicate values to be bound to the dropdownlist.

Upvotes: 1

Views: 1247

Answers (1)

void
void

Reputation: 7890

you have to use Distinct keyword in data source when retrieving data from your database so duplicate values are not come. just in your sqlcommand try Select DISTINCT DoctorId,Location from DoctorR instead of Select DoctorId,Location from DoctorR

Upvotes: 2

Related Questions