943,587 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 10850
  • C# RSS
Jan 5th, 2009
0

How to refresh Combobox items

Expand Post »
Hi

I am populating my combobox with table's data (example table name is "ITEMS" and table is having only one column and that is displayed as combobox items). In my form i am having i combobox,button and textbox. I will enter a new item in textbox and if i click on button, that new item will be stored in a table i.e. "ITEMS"). my problem is that new item has to be displayed in combobox items without closing my form and again opening it.
If i click button by entering new item in textbox that value has to be stored in table and combobox has to be populated with new item.

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nmakkena is offline Offline
14 posts
since Dec 2008
Jan 5th, 2009
0

Re: How to refresh Combobox items

A form doesn't close by itself. Could you send your code with the eventhandler of the button?
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,735 posts
since Oct 2008
Jan 5th, 2009
0

Re: How to refresh Combobox items

When you "open" the form, include as part of the calling to open method an update of your combobox items.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 6th, 2009
0

Re: How to refresh Combobox items

Yo dude, you can always just refresh the connection, you don't need to close your form,

i take it you want something like this

C# Syntax (Toggle Plain Text)
  1. using System.Data.SqlClient;
  2.  
  3.  
  4. namespace TestDummyTWO
  5. {
  6. public partial class Form1 : Form
  7. {
  8. SqlConnection sqlcon =
  9. new SqlConnection("Data Source=.;Initial Catalog=your_table;Integrated Security=True;Asynchronous Processing = True");
  10.  
  11. SqlCommand sqlCom = new SqlCommand("SELECT ITEMS FROM YOUR_TABLENAME");
  12.  
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17.  
  18. private void Form1_Load(object sender, EventArgs e)
  19. {
  20. SqlDataReader reader = sqlCom .ExecuteReader();
  21.  
  22. // Looping throuhg the list of data
  23. while (reader.Read())
  24. {
  25. // Fill combo Box with data
  26. comboBox1.Items.Add(reader["ITEMS"]);
  27. }
  28. }
  29.  
  30. private void button1_Click(object sender, EventArgs e)
  31. {
  32. // you added the new item from the tex box into the DATA TABLE
  33. // Not the combobox
  34. // use sqlCom again to do so
  35.  
  36. sqlCom.CommandText = "Insert into blah blah";
  37.  
  38. /* THIS IS WHAT YOU HAVE TO DO */
  39. sqlcon.Close();
  40. sqlcon.Open();
  41. }

Hope that helps ...
Last edited by cVz; Jan 6th, 2009 at 4:11 am.
cVz
Reputation Points: 29
Solved Threads: 7
Junior Poster
cVz is offline Offline
139 posts
since Mar 2008
Jan 7th, 2009
0

Re: How to refresh Combobox items

Hi
Thanks alot. it worked for me
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nmakkena is offline Offline
14 posts
since Dec 2008
Jan 7th, 2009
0

Re: How to refresh Combobox items

Cooooollll
cVz
Reputation Points: 29
Solved Threads: 7
Junior Poster
cVz is offline Offline
139 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: update access database
Next Thread in C# Forum Timeline: Data collection from dataset using query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC