add data to drop down

Posted by Klbaiju under C# on 4/2/2012 | Points: 10 | Views : 1154 | Status : [Member] | Replies : 12
hi,
in my application ,there is a dropdownlist.
after saving data to data base.
i want to add one field to dropdownlist
after the save command i wote

SqlCommand cmd = new SqlCommand("select cname from registration ", con);
con.Open();
SqlDataReader dr1 = cmd.ExecuteReader();
while (dr1.Read())
{
DropDownList1.Items.Clear();
DropDownList1.Items.Add(dr1.GetString(0));
}
dr1.Close();
but adding duplicates.

i have added "select a company" to drop downlist as first item;

how to avoid the duplicate value.
suppose if there are 10 records,if i add another record my
drop down list should contains
only 11 records with select a company at top.


Regards
K L BAIJU




Responses

Posted by: Naraayanan on: 4/2/2012 [Member] Starter | Points: 25

Up
0
Down
Hi ,
Avoid Duplicate Entries:
 select Distinct Cname from registration 


Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/3/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi Klbaiju

Please Try Naraayanan answer



Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 4/3/2012 [Member] Starter | Points: 25

Up
0
Down
http://sandeep-chourasia.blogspot.in/2011/08/populating-dropdownlist.html

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
Use command select distinct cname from registration

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/4/2012 [Member] Bronze | Points: 25

Up
0
Down
Perfect.Chourasia@Gmail.Com

Ya Man You Are Right...

That distinct idea already Given by Naraayanan

You Give the different idea... this might be useful for all

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

Just add DISTINCT Keyword After SELECT


SELECT DISTINCT Cname FROM registration


R D Patel

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

Using another way :

DataTable distinctTable = originalTable.DefaultView.ToTable( /*distinct*/ true);


Thanks

R D Patel

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/4/2012 [Member] Bronze | Points: 25

Up
0
Down
Patel28rajendra

Good Right way Man

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/4/2012 [Member] Bronze | Points: 25

Up
0
Down
Patel28rajendra
Congratz Man

u r the
Runner up winner for the Forums section for the month of March 2012.

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down
Yes
I know
Thanks

R D Patel

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 4/4/2012 [Member] Starter | Points: 25

Up
0
Down


Sabari Mahesh P M

Same to you

R D Patel

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/5/2012 [Member] Bronze | Points: 25

Up
0
Down
Patel28rajendra

Cheers

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response