Solved

Displaying selected value in TextBox in Vb.Net

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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