display database in textbox in 2fields combobox selection vb.net

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 11709 Views - Last Post: 20 March 2013 - 11:10 AM Rate Topic: -----

#1 beginner_emman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-August 12

display database in textbox in 2fields combobox selection vb.net

Posted 08 August 2012 - 08:11 AM

hi, im new here, i cant find a way to display data to textbox from ms access in combobox selection and take note my combobox has 2 fields contain from database.im lost..i dont have a problem loading two fields from database in combobox, but when i select a data from combobox, i cant figure out how can i do that if combobox has two fields...no problem if combobox has only one fields..here's my code..thanks

'here i successfully loaded data to combobox
Private Sub FillCombo()
        Try
            'Dim fillcon As New OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path to access file.mdb;")
            Dim asql As String = ("SELECT LastName +', '+ FirstName AS FullName FROM tbleMember order by Lastname")
            Dim da As New OleDbDataAdapter(asql, MyConn)
            Dim ds As New DataSet
            da.Fill(ds)
            cmbName.ValueMember = "FullName"
            cmbName.DataSource = ds.Tables(0)
            cmbName.SelectedIndex = 0
        Catch ex As Exception
            MsgBox("ERROR : " & ex.Message.ToString)
        End Try
    End Sub



'codes displaying into textbox..
Private Sub cmbName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbName.SelectedIndexChanged
        Dim asql As String = ("SELECT * FROM tbleMember where LastName='" & cmbName.Text & "'")
        MyConn.Open()
        Dim cmd As New OleDbCommand
        cmd.CommandText = asql
        cmd.Connection = MyConn
        dr = cmd.ExecuteReader
       
        If dr.Read = True Then
            txtFormNo.Text = dr("FormNo")
        End If
        MyConn.Close()

    End Sub

This post has been edited by smohd: 08 August 2012 - 08:20 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: display database in textbox in 2fields combobox selection vb.net

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: display database in textbox in 2fields combobox selection vb.net

Posted 08 August 2012 - 08:27 AM

I have two ideas here of what you are trying to do.
First you can the DisplayMember and ValueMember properties to differentiate the view and the working inside. That will be the best approach to deal with view in places like this. For example, if your table has ID, fname and lname, then you will display fname + lname (as what you did and make ID as the value member. So when user select a name from a combobox you get its id (or a last, wherever on what you set a valuemember). This will simplify the work here.

Another idea will be to get the last name from the selected text which is not the best idea.
Hope this will help.
Was This Post Helpful? 0
  • +
  • -

#3 beginner_emman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-August 12

Re: display database in textbox in 2fields combobox selection vb.net

Posted 08 August 2012 - 11:02 AM

View Postsmohd, on 08 August 2012 - 08:27 AM, said:

I have two ideas here of what you are trying to do.
First you can the DisplayMember and ValueMember properties to differentiate the view and the working inside. That will be the best approach to deal with view in places like this. For example, if your table has ID, fname and lname, then you will display fname + lname (as what you did and make ID as the value member. So when user select a name from a combobox you get its id (or a last, wherever on what you set a valuemember). This will simplify the work here.

Another idea will be to get the last name from the selected text which is not the best idea.
Hope this will help.



thanks for the attention, i did what you've just said and still it is the same, the combobox is working properly and honestly the idea of difference between displaymember and the value member is new to me...and how can i make the ID as the value member if i want it to display in the textbox when selecting data from combobox...i used so many codes and tried it but still no work...help will be much appreciated...

and by the way, selected text from combobox is really not a good idea, its just im mess up with this query..thanks again..
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: display database in textbox in 2fields combobox selection vb.net

Posted 08 August 2012 - 11:28 AM

ok if your table has an Id field then you can select it also ( or any thing that is unique or primary key). Then you can make your display as you like ( which will be the display member). The display member is the column that is displayed in the combobox collection list which will be fullname here, then add to the select list in your query another unique column like id or any which you want to use when user select an option. That will be a value member, a value member is the value returned when an option is selected from a combobox( if display is not set it also work as one). So try that and you will see if you say .selectedValue you will get the value member and not the displayed one.

Do that and you will get what you wanted. If you have any more problems you can reply and don't forget to post your updated code so we can go together.

This post has been edited by smohd: 08 August 2012 - 10:43 PM
Reason for edit:: fixed il tag

Was This Post Helpful? 0
  • +
  • -

#5 beginner_emman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-August 12

Re: display database in textbox in 2fields combobox selection vb.net

Posted 08 August 2012 - 08:00 PM

sorry for the late reply sir, its quite confusing for me where to put the .selectedvalue... as you can notice, my code has to member fields from database ms access called in combobox... the combination of LastName anf FirstName..the displaymember is Fullname... in my database i have a lot of fields.. ID LastName FirstName, Middlename, Address... i put only to fields in combobox, so when i select in combobox, the other data from the other fields will display to textbox..i'm really sorry, but i still dont know where to put where is where..thanks..thanks for the help..
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: display database in textbox in 2fields combobox selection vb.net

Posted 08 August 2012 - 10:51 PM

If you are following what I am telling you, you can at lest try something and show your updated code.
The idea is very simple, add an id in your select statement ( we like to use ID or primary key because it is best for the WHERE clause, lastname may happen to be more than one). so you will have like:
 Dim asql As String = ("SELECT LastName +', '+ FirstName AS FullName, ID FROM tbleMember order by Lastname")
so now we have ID also. Then you dont need to worry because you will only display what you wanted and the ID is just for simplifying the searching of data. To bind data you will have:
cmbName.DisplayMember= "FullName"
cmbName.ValueMember = "ID"
instead of only having value member.
Then your query will need to get cmbName.SelectedValue in another query instead of cmbName.Text. Hope that helps to give you an idea here
Was This Post Helpful? 1
  • +
  • -

#7 beginner_emman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-August 12

Re: display database in textbox in 2fields combobox selection vb.net

Posted 09 August 2012 - 12:25 AM

thanks for the reply, but i already get what you are trying to say.i've read about the topic of displaymember and valuemember..and your thoughts gave an idea on how to do it....and also thanks for the example it really helps me a lot..
i can move on now to another process....thanks..thanks..kudos..
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: display database in textbox in 2fields combobox selection vb.net

Posted 09 August 2012 - 03:33 AM

Glad we could help :)
Was This Post Helpful? 0
  • +
  • -

#9 beginner_emman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-August 12

Re: display database in textbox in 2fields combobox selection vb.net

Posted 09 August 2012 - 03:57 AM

View Postsmohd, on 09 August 2012 - 03:33 AM, said:

Glad we could help :)


hello, sorry but i overlook the codes, finally i got no error on my queries but also no display/output in textbox...i was thinking maybe my approach is not a good idea..it goes something like this..

'My database
FormNo Lastname FirstName Address
1      carter   john      Asia
2      smith    luke      australia
etc....

'here is my sub where i called database to connect to combobox
'i used LastName and FirstName as a selection in one combobox
Private Sub FillCombo()
        Try
           
            Dim asql As String = ("SELECT LastName +', '+ FirstName AS FullName,FormNo,Address FROM tbleMember order by Lastname")
            Dim da As New OleDbDataAdapter(asql, MyConn)
            Dim ds As New DataSet
            da.Fill(ds)
            cmbName.DisplayMember = "FullName"
            cmbName.ValueMember = "FormNo"
            cmbName.ValueMember = "Address"
            cmbName.DataSource = ds.Tables(0)
            cmbName.SelectedIndex = 0
        Catch ex As Exception
            MsgBox("ERROR : " & ex.Message.ToString)
        End Try
    End Sub
'i added the FormNo which is the primary key on my database access and address to display in another textbox...actually i will use more textbox to display..i only put here the FormNo and addres

'here i called the fillcombo sub function in formload

    Private Sub frmLoan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call FillCombo()
        cmbName.Text = ""
        txtFormNo.Text = ""
    End Sub

'here is my event under the combobox select
'here, i want to call the FormNo and Address field to display in textbox when selecting.
 Private Sub cmbName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbName.SelectedIndexChanged

        Dim asql As String = ("SELECT * FROM tbleMember where LastName='" & cmbName.SelectedValue & "'")
        MyConn.Open()
        Dim cmd As New OleDbCommand
        cmd.CommandText = asql
        cmd.Connection = MyConn
        dr = cmd.ExecuteReader

        If dr.Read = True Then
            txtFormNo.Text = dr("FormNo")
            txtAddress.Text = dr("Address")
        End If
        MyConn.Close()

    End Sub


'i really dont know why the data didn't display in the textbox.is my there a problem in ly approach/logic?
'thanks for the help...

This post has been edited by smohd: 09 August 2012 - 04:10 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: display database in textbox in 2fields combobox selection vb.net

Posted 09 August 2012 - 04:13 AM

You are setting the display member FormNo but in your query but in the second query you are dealing with the lastName!!!
The one you set as the value member is the one to be returned when you ask for selectedValue, so the query wont match anything.
Was This Post Helpful? 1
  • +
  • -

#11 beginner_emman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-August 12

Re: display database in textbox in 2fields combobox selection vb.net

Posted 09 August 2012 - 05:11 AM

thank you so much for the help. the codes did work..i change the lastname to address which is one of my valuemember.. and thanks for teraching me the display and valuemember idea...you guys are great..
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: display database in textbox in 2fields combobox selection vb.net

Posted 09 August 2012 - 05:31 AM

You are welcomed.
Just a note, you dont need two value members there. Only one is enough which you are using.
Was This Post Helpful? 1
  • +
  • -

#13 beginner_emman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-August 12

Re: display database in textbox in 2fields combobox selection vb.net

Posted 09 August 2012 - 06:55 AM

i'll take note to that sir..thank you again..
Was This Post Helpful? 0
  • +
  • -

#14 ranonymus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-March 13

Re: display database in textbox in 2fields combobox selection vb.net

Posted 20 March 2013 - 10:47 AM

hey im having the same insue can you help me??
how do you declare the:
dr = cmd.ExecuteReader
Dim dr ....

and Myconn to pls ;)
Was This Post Helpful? 0
  • +
  • -

#15 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

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

Re: display database in textbox in 2fields combobox selection vb.net

Posted 20 March 2013 - 10:54 AM

probably should have started a new thread.
For access there is a tutorial called OleDB basics in Vb.net and for SQL there is one called SQL Basics in VB.net in the tutorials section. I would link them but search is down and I didn't want to go searching through them myself. Besides, you might find other tutorials of interest as you scan through them. :)

I would start there and then come back with any more specific questions with some samples of your code and any errors that code might be displaying.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2