ComboBox with textbox in vb.net2003

hi..

I have one combobox and one textbox..
the combobox has a numbers, and the textbox has the name..
numbers and names are comming from one table that's being in the database..

each number has one name in the table..

when the user press on the combobox it will show the numbers that being in the table

what I want is how can I display the name automatically in the textbox when the user define the number in the combobox.

thanks
LVL 2
JassimiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PockyMasterCommented:
You can do like this:

create a class Containing number and  Name

public class MyData
 private _sName as string
private _iNumber as integer

public sub New (sName as string, iNumber as integer )
_sName =sName
_iNumber = iNumber
end sub

public Readonly Property Name as string
return _sName
end property

public readonly Property Number as Integer
return _iNumber
End Property

End Class

Use this class when you get your data

e.g.

Public Function MyDataFunction() as ArrayList

dim arData as new ArrayList

While Not ...  (psuedo code)
 arData.Add (New MyData (dr!Name, dr!Number))

End While

In your form:

cboData.DisplayMember = "Number"
cboData.DataSource = MyDataFunction()


Then add an event in the change of your combobox
and implement there:

dim myItem as MyData = DirectCast(cboData, MyData)
txtMyName.Text= myItem.Name

Get it??

Have fun!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JassimiAuthor Commented:
hi..

sorry for late..
but I was trying your method..
I'm sorry to tell u that I didn't get it..

can u explain more about how to do that

thanks
0
PockyMasterCommented:
Show me your data retrieval method, and I'll put it in
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

JassimiAuthor Commented:
the data coming from suppliers datatable source
number feild is (supplier_id)
name  feild id (supplier_name)

returning to the form:
the combobox is(supplier_id)
the textbox is (supplier_name)


so when the user select the supplier_id from combobox.. the supplier_name will display automatically in the (supplier_name) textbox
0
PockyMasterCommented:

(dtSuppliers will be filled by your query)

dim arResults as new ArrayList

dim data as MyData
For each dr as dataRow in dtSuppliers.Rows
  data = new MyData(dr!Supplier_Name,dr!Supplier_ID)
  arResults.Add(data)
Next

cboSuppliers.DisplayMember = "Number"
cboSuppliers.DataSource = arResults


 Private Sub cboSuppliers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSuppliers.SelectedIndexChanged
        If Not cboSuppliers.SelectedItem Is Nothing Then
            txtSupplierName.Text = DirectCast(cboSuppliers, Mydata).Name
        End If
    End Sub


You might have to change the names of the combobox and the textbox to your own, but I guess it should be more clear now
0
JassimiAuthor Commented:

it's showing some errors on the following statements:
1- name: in (Public ReadOnly Property Name() As String)
                error: 'ReadOnly' property must provide a 'Get'

2- numver: in (Public ReadOnly Property number() As String_
                 error: 'ReadOnly' property must provide a 'Get'

3- return_sName..  error(statement cannot apear withen a proberty body.End of propertry assumed)

4- end property ..  error(must be proceded by matching 'Property')

5- return_iNumber.. error(statement cannot apear withen a proberty body.End of propertry assumed)


0
PockyMasterCommented:
Yeah, I typed it just as an example :D
Didn't put it in my environment first :D
I assumed you could figure that one out yourself.

So yeah,

Public Readonly Property Name as string
Get
Return _sName
End Get

End Property
0
JassimiAuthor Commented:
ok..

now it's showing another error in for statement:
 For Each dr As DataRow In dtSuppliers.Rows

the error says: Object reference not set to an instance of an object

and the statement become with yellow color
0
PockyMasterCommented:
In my example I wrote:
dtSuppliers is a datatable you filled somewhere..
did you do that?
0
JassimiAuthor Commented:
which one u mean?

sorry but I didn't get it
0
PockyMasterCommented:
Your statement:

the data coming from suppliers datatable source
number feild is (supplier_id)
name  feild id (supplier_name)

returning to the form:
the combobox is(supplier_id)
the textbox is (supplier_name)


so when the user select the supplier_id from combobox.. the supplier_name will display automatically in the (supplier_name) textbox
---------------

So I stated : dtSuppliers is the DataTable obj, which is filled with the Supplier Data (which you need to write code for :D )

Which could be something like

Dim conn as new OleDb.OleDbConnection (YourconnectionString)
Dim dtSuppliers As New DataTable
Dim MyDataAdapter As New  OleDbDataAdapter
Dim MyCommand As New  OleDbCommand
MyCommand.CommandType = CommandType.Text
'I have to guess your table name
MyCommand.CommandText = "SELECT SUPPLIER_ID, SUPPLIER_NAME FROM SUPPLIERS ORDER BY SUPPLIER_NAME "
MyCommand.Connection = Conn
MyDataAdapter.SelectCommand = MyCommand
MyDataAdapter.Fill(dtSuppliers )

 MyCommand.Dispose()
    MyDataAdapter.Dispose()
   
0
JassimiAuthor Commented:
ok.. I did that and the error is gone..

but now it's showing another error on this statement:
txtSupplierName.Text = DirectCast(cboSuppliers, Mydata).Name

the error says: Specified cast is not valid.

and the statement become with yellow color also..

sorry for bothering u
0
PockyMasterCommented:
Oops, my mistake..

txtSupplierName.Text = DirectCast(cboSuppliers.SelectedItem, Mydata).Name
0
JassimiAuthor Commented:
ok..

it's working now..

thank u
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.