By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

Fill text box after selecting value in combobox

P: 1
Hi,
Beginning to learn Visual Basic 10.
Experienced user of VB 6 it now is time to learn the modern approach...

Case:
Table 'Customer' in SQL Server, Fields are 'Name' and 'Adress'

VB Program starts and loads all Customer Names in combo box and all details in a DataGrid.
This is already working.

Then I want to display the Adress when selecting a Name in the combo box.

And this I can't get to work.

Coding (The part that is working):

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private SQLCon As New SqlConnection("Data Source=zzz;Initial Catalog=yyy;Integrated Security=SSPI")
  3.     Private da1 As New SqlDataAdapter("SELECT * FROM Customers", SQLCon)
  4.     Private ds As New DataSet
  5.     Private cmb As New SqlCommandBuilder(da1)
  6.  
  7.     Private MySQL_Cmd As New SqlCommand
  8.     Private sSQLQuery As String
  9.     Private MySQL_DA As New SqlDataAdapter
  10.     Private My_DS As New DataSet
  11.  
  12. Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  13.    Try
  14.  
  15.    da1.Fill(ds, "Customers")
  16.    DataGrid.DataSource = ds.Tables("Customers")
  17.  
  18.  
  19.    sSQLQuery = "SELECT DISTINCT C_Name FROM Customers"
  20.    MySQL_Cmd = New SqlCommand(sSQLQuery, SQLCon)
  21.    MySQL_DA = New SqlDataAdapter(MySQL_Cmd)
  22.    MySQL_DA.Fill(My_DS, "Tab1")
  23.  
  24.    Cbx_Name.Items.Clear()
  25.    Cbx_Name.DisplayMember = "C_Name"
  26.    Cbx_Name.ValueMember = "C_Name"
  27.    Cbx_Name.DataSource = My_DS.Tables(0)
  28.    Cbx_Name.SelectedIndex = 0
  29.  
  30.    Catch ex As Exception
  31.             MessageBox.Show(ex.Message)
  32.    End Try
  33. End Sub
  34.  
  35. '+++++++++++++++
  36. THE PART THAT DOES NOT WORK
  37. '+++++++++++++++++++++++++++
  38.  
  39. Private Sub Cbx_Name_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles Cbx_Name.SelectedIndexChanged
  40.  
  41. Try
  42.   sSQLQuery = "SELECT * FROM Customers WHERE C_Name = '" & Cbx_Name.SelectedValue & "'"
  43.  
  44.   MySQL_Cmd = New SqlCommand(sSQLQuery, SQLCon)
  45.   MySQL_DA = New SqlDataAdapter(MySQL_Cmd)
  46.   MySQL_DA.Fill(My_DS, "Tab2")
  47.  
  48.  
  49.   Dim dr As DataRow = My_DS.Tables(1).Rows(0)
  50.  
  51.   Text_Adress.Text = dr("C_Adress").ToString
  52.  
  53.   Catch ex As Exception
  54.         MessageBox.Show(ex.Message)
  55.   End Try
  56.  
  57. End Sub
  58. End Class
Sep 17 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.