Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a page on which i have a dropdown which contains distinct usernames from database(username column is unique in database),after selecting a username from it,it fills the gridview on the same page

the problem arising is that after selectinga username,the names doesnot get distinct ie it shows duplicate values for each username in dropdown

Can any one tells me the solution
Code of Getting USers===>

C#
private void LoadUsers()
  {
    DBConnection d = new DBConnection();
    d.OpenConnection();
    SqlCommand select = new SqlCommand();
    select.CommandText = "Select Distinct Username,UserId from tblUser";
    select.Connection  = d.myconn;
    dropdownuser.DataSource = select.ExecuteReader();
    dropdownuser.DataTextField = "Username";
    dropdownuser.DataValueField = "UserId";
    dropdownuser.DataBind();
//Load users
}




Code of DropDown ==>

XML
<asp:DropDownList ID="dropdownuser" runat="server" CssClass="tx_box">
                </asp:DropDownList
Posted
Updated 1-Feb-11 0:30am
v2
Comments
PunkIsNotDead 1-Feb-11 14:42pm    
"Select Distinct Username,UserId from tblUser"... isn't necessary the 'DISTINCT' if UserId column is IDENTITY

You should only load users, if it is not a postback[^].

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadUsers();
    }    
}
 
Share this answer
 
Comments
Syed Salman Raza Zaidi 1-Feb-11 6:45am    
Excellent Brother
Manas Bhardwaj 1-Feb-11 6:48am    
my pleasure :)
There is nothing wrong with dropdown list.
Once test your query in sql by changing like below you will know why you are getting duplicate values..
Select Distinct Username from tblUser

and
Select Distinct UserId from tblUser
 
Share this answer
 
C#
if (!IsPostBack){
      //code here
 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900