Solved

Displaying selected value in TextBox in Vb.Net

Posted on 2012-03-28
5
340 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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