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):
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):
Code:
Public Class Form1
Private SQLCon As New SqlConnection("Data Source=zzz;Initial Catalog=yyy;Integrated Security=SSPI")
Private da1 As New SqlDataAdapter("SELECT * FROM Customers", SQLCon)
Private ds As New DataSet
Private cmb As New SqlCommandBuilder(da1)
Private MySQL_Cmd As New SqlCommand
Private sSQLQuery As String
Private MySQL_DA As New SqlDataAdapter
Private My_DS As New DataSet
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Try
da1.Fill(ds, "Customers")
DataGrid.DataSource = ds.Tables("Customers")
sSQLQuery = "SELECT DISTINCT C_Name FROM Customers"
MySQL_Cmd = New SqlCommand(sSQLQuery, SQLCon)
MySQL_DA = New SqlDataAdapter(MySQL_Cmd)
MySQL_DA.Fill(My_DS, "Tab1")
Cbx_Name.Items.Clear()
Cbx_Name.DisplayMember = "C_Name"
Cbx_Name.ValueMember = "C_Name"
Cbx_Name.DataSource = My_DS.Tables(0)
Cbx_Name.SelectedIndex = 0
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'+++++++++++++++
THE PART THAT DOES NOT WORK
'+++++++++++++++++++++++++++
Private Sub Cbx_Name_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles Cbx_Name.SelectedIndexChanged
Try
sSQLQuery = "SELECT * FROM Customers WHERE C_Name = '" & Cbx_Name.SelectedValue & "'"
MySQL_Cmd = New SqlCommand(sSQLQuery, SQLCon)
MySQL_DA = New SqlDataAdapter(MySQL_Cmd)
MySQL_DA.Fill(My_DS, "Tab2")
Dim dr As DataRow = My_DS.Tables(1).Rows(0)
Text_Adress.Text = dr("C_Adress").ToString
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class