Click here to Skip to main content
15,914,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I developed a Windows Form Application. There are many forms available for data manipulation. In a form namely Sales_Details I enter information of a doctor in database such as Doctor Name, Address, Mobile etc. In one another form I used a comboBox control for Doctor Name, I retrieve the Doctor Name to this comboBox from Sales_Details where the Doctor Name entered first time. I wrote this code for that
C#
DataTable dt2 = new DataTable();
SqlConnection cn1 = new SqlConnection(Class1.cs);
SqlDataAdapter da2 = new SqlDataAdapter("select Doctor_Name from Sales_Details", cn1);
da2.Fill(dt2);
comboBox2.DataSource = new BindingSource(dt2, null);
comboBox2.DisplayMember = "Doctor_Name";
comboBox2.Text = "Select One...";
cn1.Close();

while I run this code it's running properly but it retrieves and shows in the comboBox a particular Doctor Name 5 times if I entered that particular Doctor Name 5 times by using the first form viz; Sales_Details. I want to show that particular Doctor Name only one time whether I entered it 5 or 6 times no matter. Please someone help.
I used C# and SQL Server 2005 to develop this application.
Posted
Updated 25-Mar-13 17:59pm
v2

SQL
select DISTINCT Doctor_Name from Sales_Details

But not a perfect solutions since two doctors can have the same name.
 
Share this answer
 
v2
Comments
_Amy 25-Mar-13 23:59pm    
+5! Agree..
You should have a code for each doctors. I would suggest you to display code along with the doctor names in DropDown. Try this:
C#
DataTable dt2 = new DataTable();
SqlConnection cn1 = new SqlConnection(Class1.cs);
SqlDataAdapter da2 = new SqlDataAdapter("SELECT (Doctor_Name +' - '+ Doctor_Code) AS Doctor_Name, Doctor_Code  FROM Sales_Details", cn1);
da2.Fill(dt2);
comboBox2.DataSource = new BindingSource(dt2, null);
comboBox2.DisplayMember = "Doctor_Name";
comboBox2.Text = "Select One...";
cn1.Close();

Now, if the two doctors are having same name also then user can easily identify with their codes.

--Amit
 
Share this answer
 
Comments
bbirajdar 26-Mar-13 2:29am    
+5 for updated solution
_Amy is right my solution is lite modified form
C#
DataTable dt2 = new DataTable();
SqlConnection cn1 = new SqlConnection(Class1.cs);
SqlDataAdapter da2 = new SqlDataAdapter("SELECT Doctor_Code, (Doctor_Name +' - '+ Doctor_Code) AS Doctor_Name, Doctor_Code  FROM Sales_Details", cn1);
da2.Fill(dt2);
comboBox2.DataSource = new BindingSource(dt2, null);
comboBox2.DisplayMember = "Doctor_Name";
comboBox2.ValueMember = "Doctor_Code";
comboBox2.Text = "Select One...";
cn1.Close();


now you can get the actual doctor code with comboBox.SelectedValue property.
 
Share this answer
 
Comments
bbirajdar 26-Mar-13 2:29am    
+5 for updated solution

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