Answered by:
How To Avoid Repeat Data In Dropdownlist

Question
-
User-807418713 posted
Hello
I Used This Code In My Page https://forums.asp.net/post/6265870.aspx
I Want DropDownList1 And DropDownList2 Make Distinct Name, It Showing
For example DropDownList1
ABCD
ABCD
ABCD
But I Want Only One Time
ABCD
KLMN
VWXY
How To Make Value Distinct, Thank You
Thursday, June 13, 2019 7:41 PM
Answers
-
User-719153870 posted
Hi Gopi.MCA,
In your method of binding DropDownList1 data, please use the following SQL statements:
select distinct Supplier from Personal_Details
In the aspx.cs, please refer to below codes:
[WebMethod] public static string DropDownList1Bind() { List<PersonalDetails> details = new List<PersonalDetails>(); SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Education;Integrated Security=True;"); SqlCommand command = new SqlCommand("SELECT distinct Supplier FROM Personal_Details;", conn); conn.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { PersonalDetails detail = new PersonalDetails(); detail.Supplier = (string)reader["Supplier"]; details.Add(detail); } reader.Close(); } conn.Close(); JavaScriptSerializer jss = new JavaScriptSerializer(); string myJson = jss.Serialize(details); return myJson; }
Here's my result of work demo:
Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 14, 2019 2:23 AM
All replies
-
User475983607 posted
Use DISTINCT.
SELECT DISTINCT Supplier FROM Personal_Details;
Thursday, June 13, 2019 8:09 PM -
User-719153870 posted
Hi Gopi.MCA,
In your method of binding DropDownList1 data, please use the following SQL statements:
select distinct Supplier from Personal_Details
In the aspx.cs, please refer to below codes:
[WebMethod] public static string DropDownList1Bind() { List<PersonalDetails> details = new List<PersonalDetails>(); SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Education;Integrated Security=True;"); SqlCommand command = new SqlCommand("SELECT distinct Supplier FROM Personal_Details;", conn); conn.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { PersonalDetails detail = new PersonalDetails(); detail.Supplier = (string)reader["Supplier"]; details.Add(detail); } reader.Close(); } conn.Close(); JavaScriptSerializer jss = new JavaScriptSerializer(); string myJson = jss.Serialize(details); return myJson; }
Here's my result of work demo:
Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 14, 2019 2:23 AM