Link to home
Start Free TrialLog in
Avatar of alsam
alsam

asked on

Using Combobox selected value as parametar in SQL Command

Hi,
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()
        CNnwind.Open()
        DACustomers.Fill(DSNWind, "dtCosts1")
        CNnwind.Close()



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

        LoadTreeView(DSNWind)
        TreeView1.CollapseAll()

    End Sub

Open in new window

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of alsam
alsam

ASKER

Hi,
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
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alsam

ASKER

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 :-)