Solved

Displaying selected value in TextBox in Vb.Net

Posted on 2012-03-28
5
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

696 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