Solved

Displaying selected value in TextBox in Vb.Net

Posted on 2012-03-28
5
338 Views
Last Modified: 2012-03-29
Hi,

I have a form which contains one combobox and two text boxes. Combobox is for the userId. and text box are for firstNAme and LAstNAme.  Well, If I selected USer ID  '5', then the firstname and the lastname of the person should appear in the text box.
How do i achieve it?
Thanks!!!
0
Comment
Question by:developer2012
5 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37778860
...and where do we get the associated firstNAme and LAstNAme from?
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37778885
Assuming that you know how to get firstname and lastname, here is a sample you should try to use:-

private void ComboBox1_SelectedIndexChanged(object sender, 
		System.EventArgs e)
	{

		ComboBox comboBox = (ComboBox) sender;

         	string selectedEmployee = (string) ComboBox1.SelectedItem;
                txtFirstName.Text = GetFirstName(selectedEmployee);
                txtLastName.Text = GetLastName(selectedEmployee);
        }

Open in new window

GetfirstName and GetLastName are the methods that will fetch first and last names of the id passed to them.....
0
 

Author Comment

by:developer2012
ID: 37779119
@ Idle-Mind: I get the firstName & LAstNAme from the database. IT should display the firstname and lastname associated with UID.
Thanks
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 37779490
There are two approaches.

1) If the list of names is not huge, you can load all the names in a DataTable on form load using following code

Dim dbadp As New SqlDataAdapter("Select UID, Firstname, Lastname From tablename", "connectionstring")
Dim dTable As New DataTable 'declare it at class level
dbadp.Fill(dTable)
dbadp.Dispose


Then in selectedindexchange event

dTable.DefaultView.RowFilter = "UID=" & combobox1.Text
If dTable.DefaultView.Count = 1 Then
   txtFirstName.Text = dTable.DefaultView.Item(0).Item(1)
   txtLastName.Text = dTable.DefaultView.Item(0).Item(2)
End if


2) Second option is to load the names when the selection changes

In selectedindexchanged event

Dim dbcon As New SqlConnection("connectionstring")
dbcon.Open()
Dim dbcmd As New SqlCommand(dbcon)
dbcmd.CommandText = "Select Firstname + ':' + LastName From table Where UID=" & combobox1.Text
Dim names as object = dbcmd.ExecuteScalar()
dbcmd.Dispose
dbcon.Dispose

if Not IsDbNull(names)
   txtFirstname.Text = names.ToString().Split(":")(0)
   txtLastname.Text = names.ToString().Split(":")(1)
End if
0
 

Author Closing Comment

by:developer2012
ID: 37783468
Thanks!!!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now