?
Solved

Displaying selected value in TextBox in Vb.Net

Posted on 2012-03-28
5
Medium Priority
?
344 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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 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