8 Replies - 413 Views - Last Post: 07 March 2012 - 01:36 AM Rate Topic: -----

#1 sanrooney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 25-April 11

Trouble in Updatin ComboBox

Posted 05 March 2012 - 12:27 PM

hi guys,

i'm doing small system using database 03. I had function to add data into a comboBox.

When i add a new data into the the same field using another form, then back to the main Form

the comboBox does not update the current data in database..

please help..codes i tried

'Form Load
Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\userinfo.accdb")
        'con.Open()
       
        Label5.Cursor = Cursors.Hand
        lblManage.Cursor = Cursors.Hand

        con.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=|DataDirectory|\userinfo.mdb"
        con.Open()

        getData()
        get_class()

End Sub


'function to add data into comboBox
Public Sub get_class()
        Dim PCMD As New OleDbCommand
        Dim PDR As OleDbDataReader

        'cbClass.DisplayMember = "FClass"
        'cbClass.ValueMember = "Id"

        PCMD.Connection = con
        PCMD.CommandText = "select FClass from tblClass"
        PDR = PCMD.ExecuteReader
        If PDR.HasRows Then
            While PDR.Read()
                cbClass.Items.Add(PDR("FClass"))
            End While
        End If
        PDR.Close()
    End Sub




Thanks Advance..

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble in Updatin ComboBox

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9389
  • View blog
  • Posts: 35,264
  • Joined: 12-June 08

Re: Trouble in Updatin ComboBox

Posted 05 March 2012 - 12:33 PM

Where in there is your update statement/query called?
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Trouble in Updatin ComboBox

Posted 05 March 2012 - 12:44 PM

OK, think of it this way, that combobox is a container. You filled it with information from a database. You then disconnected from that database because you no longer needed it. You already filled the combobox with the information. It is nothing more than text in the combobox items.

When you changed information in the database the combobox has no clue that happened.

In order for you to "refresh" the contents of that combobox, you need to call that get_class sub again.

Keep in mind though that you need to clear the previous items from the combobox or you will just continue to add strings to it leaving you with duplicated information.

Now there is the method of instead of filling the combobox with information, you fill a dataset (again, disconnected data)

Then you can assign that dataset to the comboboxes datasource property and use displayMember to show the contents of the field.

Now using that same instance of the dataset by setting it to a datagrid, if you were to alter it by adding rows to it. Those changes would also be reflected in the combobox as long as the select statement for the dataset encompasses those changes. This is a slightly different coding method than executing a query and using the reader to iterate through the data like you just did, but it is a very good way to automate these types of scenarios.

EDIT:
Now here's the real question. I see |DataDirectory| in your connection string which tells me you've added a dataset to your application. Why not just drag the column you want to display to the form as a combobox and then drag the table as a datagridview from that same datasource and this functionally will be there?

This post has been edited by CharlieMay: 05 March 2012 - 12:56 PM

Was This Post Helpful? 1
  • +
  • -

#4 sanrooney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 25-April 11

Re: Trouble in Updatin ComboBox

Posted 05 March 2012 - 06:37 PM

View Postmodi123_1, on 05 March 2012 - 12:33 PM, said:

Where in there is your update statement/query called?


i have called the get_class function on Form_activated so that every time i back to the form it will
get the data from database but it creates duplicate values..how..?

CharlieMay i don't have any idea regarding your explanation can have more details about the "EDIT" part..
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Trouble in Updatin ComboBox

Posted 05 March 2012 - 06:43 PM

It is duplicating values because in that sub you need to clear the combobox items. So that it doesn't just add to what you have.
Was This Post Helpful? 1
  • +
  • -

#6 sanrooney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 25-April 11

Re: Trouble in Updatin ComboBox

Posted 06 March 2012 - 03:03 AM

ya i realize that , but i don't know how to do that.. how to clear the comboBox before
i call the function..?
Was This Post Helpful? 0
  • +
  • -

#7 sanrooney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 25-April 11

Re: Trouble in Updatin ComboBox

Posted 06 March 2012 - 03:16 AM

i have got the solution as u say...Thanks man..

Private Sub FrmMain_Activated(ByVal sender As Object, ByVal e As System.EventArgs)Handles Me.Activated
        getData()
        cbClass.Items.Clear()
        get_class()
End Sub



i have called like this so every time the for activated it clear the comboBox and get the data back... :genius:
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Trouble in Updatin ComboBox

Posted 06 March 2012 - 05:05 AM

That will work. Or you could just put the cbClass.Items.Clear inside your get_class() code before you add your items so that you don't have to remember to clear it each time.
Was This Post Helpful? 0
  • +
  • -

#9 sanrooney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 25-April 11

Re: Trouble in Updatin ComboBox

Posted 07 March 2012 - 01:36 AM

ya your right i did it thanks man... i have another problem in this project please check for my new topic i posted... :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1