Click here to Skip to main content
15,887,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I saw many many articles on this but none helped so far.
My ComboBox name is cbPlan. I want to Retrieve PlanName in it's display but want to actually hold it PlanID.
Following code displays both Names and IDs. I tried ValueMember, DisplayMember, properties but couldn't get it sorted yet.
Finally, even if this works out, how will I get to insert PlanID in another table? Will i use Convert.ToString(cbPlan.Text) - which would bring the PlanName and not the ID.
Please help on this - A big thank you in advance! :)
P.S. PlanID's data type is int.


private void cbPlan_Click(object sender, EventArgs e)
       {
           cbPlan.Items.Clear();

           string pullsub = "select PlanID,PlanName from fbkPlanMaster(nolock)";
           string connString = ConfigurationManager.ConnectionStrings["Dbconn"].ToString();
           SqlConnection connection = new SqlConnection(connString); // defining sql connection
           SqlCommand cmd = new SqlCommand(pullsub, connection);
           cmd.CommandText = pullsub;
           connection.Open();
           SqlDataReader drd = cmd.ExecuteReader();
           while (drd.Read())
           {

               cbPlan.Items.Add(drd["PlanID"]);
               cbPlan.Items.Add(drd["PlanName"]);
               cbPlan.ValueMember = "PlanID";
               cbPlan.DisplayMember = "PlanName";


           }
       }


What I have tried:

- Several articles on similar lines.
- Code as displayed above.
- ValueMember and DisplayMember properties.
- Conversion into int.
Posted
Updated 24-Feb-17 0:00am
Comments
Michael_Davies 24-Feb-17 6:04am    
Why convert to string what is already a string?

"Convert.ToString(cbPlan.Text) - which would bring the PlanName and not the ID."
cluelessentity 24-Feb-17 6:10am    
Exactly, that doesn't work.
But even if I get the PlanID as valuemember in combobox, the same is not being retrieved while saving in the database for another table.

If used as cbPlan.ValueMember, it returns PlanID.
IF used as cbPlan.Text, it returns the PlanName.
Karthik_Mahalingam 24-Feb-17 6:45am    
Tip:Always use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

1 solution

try using SQl Data Adapter[^]

private void cbPlan_Click(object sender, EventArgs e)
      {
          cbPlan.Items.Clear();
          string pullsub = "select PlanID,PlanName from fbkPlanMaster(nolock)";
          string connString = ConfigurationManager.ConnectionStrings["Dbconn"].ToString();
          SqlConnection connection = new SqlConnection(connString); // defining sql connection
          SqlCommand cmd = new SqlCommand(pullsub, connection);
          cmd.CommandText = pullsub;
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataTable dt = new DataTable();
          da.Fill(dt);
          cbPlan.ValueMember = "PlanID";
          cbPlan.DisplayMember = "PlanName";
          cbPlan.DataSource = dt;


      }
 
Share this answer
 
Comments
cluelessentity 24-Feb-17 6:07am    
Hello Karthik,

That worked fine for the display.
But what could be done to pick only the value of the combobox in order to save it in another table.
Following is the query I am using:
cmd.CommandText = "INSERT into fbkPatientMaster values('" + fname + "','" + lname + "','" + gender + "'," + Environment.NewLine
+ "'" + address + "','" + state + "','" + city + "','" + email + "','" + mobile + "','" + landline + "'," + Environment.NewLine
+ "'" + dob + "'," + 0 + "," + cbPlan.Text + ",'" + comments + "'," + "getdate(),getdate())";

If used as cbPlan.ValueMember, it returns PlanID.
IF used as cbPlan.Text, it returns the PlanName.
madhav_jain 24-Feb-17 6:21am    
Use "SelectedValue" property to get value.
cluelessentity 24-Feb-17 6:26am    
Genius you are madhav_jain.
It worked.
Karthik's solution helped me to get value in the combobox. And your's helped to save it in another table.
You made my day!
Thanks a tonne :)
Karthik_Mahalingam 24-Feb-17 6:44am    
cool, that it worked :)
Maciej Los 26-Feb-17 8:28am    
5ed!

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