Using Combobox selected value as parametar in SQL Command

to simplify. I have created 2 Windows forms:
First form (named Hierarchy_load) contain Combobox control loaded with values from SQL Server database.
Second form (named Form1) represent treeview populated from SQLSerevr database.  
I need to forward selected value in combobox from Hierarchy_load into Form1_load sub and according to forwarded parameter treeview will display data from database. For unknown reason my code forwards nothing to Form1_load. Please help...attached my code (forwarded value as parameter in command should be defined in "Hierarchy_load.ComboBox1.Text".
Please help how to solve this problem
Thank you for your time and effort....
Kind regards...
Imports System.Data.SqlClient

Public Class Form1

    Private con As SqlConnection
    Private WithEvents cmdSelectHID As New SqlCommand()
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim DSNWind As DataSet
        Dim CNnwind As New SqlClient.SqlConnection("Data Source=S_A-PC\SQLEXPRESS1;Initial Catalog=AdventureWorks;Integrated Security=True")
Dim DACustomers As New SqlClient.SqlDataAdapter
        DACustomers.SelectCommand = cmdSelectHID
        DACustomers.SelectCommand.Connection = CNnwind
        DACustomers.SelectCommand.CommandText = "SELECT * FROM CostC1 WHERE HID = @param"
        DACustomers.SelectCommand.Parameters.Add("@param", SqlDbType.NVarChar)
        DACustomers.SelectCommand.Parameters("@param").Value = Hierarchy_load.ComboBox1.Text

        DSNWind = New DataSet()
        DACustomers.Fill(DSNWind, "dtCosts1")

        DSNWind.Relations.Add("Parenttochild", DSNWind.Tables("dtCosts1").Columns("ID"), DSNWind.Tables("dtCosts1").Columns("Reports"), False)


    End Sub

Open in new window

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

CodeCruiserConnect With a Mentor Commented:
You have this line in your code above

Public Class Form1

Add following below the above line

Dim frm as Hierarchy_load
Public Sub New(f as Hierarchy_load)
frm = f

Then change this line
DACustomers.SelectCommand.Parameters("@param").Value = Hierarchy_load.ComboBox1.Text

DACustomers.SelectCommand.Parameters("@param").Value = frm.ComboBox1.Text
You need to pass a reference to the Form1 of the form containing combobox. Declare a public variable and assign it when creating the form. Or use a custom constructor on form1

Public Class Form1
     Inherits Windows.Form
Dim Otherform as Hierarchy_Load

Public Sub New(frm As Hierarchy_load)
OtherForm = frm

Then use this OtherForm to get the combo text
alsamAuthor Commented:
Thanks for answer...
I lost mysef in try to understand and how to introduce this part in my code...
Could you just help me and use my attached code to adjust the same with your proposed solution...
Thank you very much....appreciate your time and effort
alsamAuthor Commented:
Thank you.
Works perfectly.
Hope to have you as help in future.
Short, clean and now very understanable solution for me.
Thank you once more time.
Glad to help now and in future :-)
All Courses

From novice to tech pro — start learning today.