Mojiz Quazilbash
Mojiz Quazilbash

Reputation: 11

Connecting and filtering Comboboxes in VB thru Microsoft SQL

I am very new to programming and need some guidance.

I have a MS SQL Database of items (product_cat, product_type, product_make, product_Model). I have a win form with 4 comboboxes related to these column. What I want is to be able to select product_cat and it filters product_type based on that. For example: if i select category "Transportation" it filters everything related to transportation into combobox2 (product_type) cars, buses, train... then when i select cars it filters and fills combobox3 (product_make). Right now my code is showing everything in all the comboboxes and wont filter anything. Thank you for any guidance.

Public Class Form1 
   Public sql As New sqlcontrol 
   Private DBcmd As SqlCommand 
   Public Sub loadcbxCategory()
      cbxCategory.Items.Clear()
      sql.ExeQuery("Select DISTINCT Product_Category From dbo.Product_Data$;") 
      If sql.HasExecption(True) Then Exit Sub 
      For Each r As DataRow In sql.DBDT.Rows
         cbxCategory.Items.Add(r("Product_Category").ToString)
      Next
   End Sub

   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
      loadcbxCategory() 
   ...

   Private Sub cbxCategory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxCategory.SelectedIndexChanged 
      Dim mysqlconn = New SqlConnection mysqlconn.ConnectionString = ("Server=mojiz-surface\sqlexpress;database=ASI_Inventory;trusted_connection=true;") 

Upvotes: 1

Views: 57

Answers (2)

Kejie Nandaru
Kejie Nandaru

Reputation: 39

Dim sample as new sqlcommand ("select*From *your datable in sqldatabase*", connection)
Dim adapter as new sqldataAdapter(sample)
Dim table as new DataTable()

adapter.fill(table)
combobox1.DataSource = Table
Combobox1.Valuemember = "No." (NOTE: *Select the column from your sql data table*)

hope it will help you..

Upvotes: 0

Umair Rasheed
Umair Rasheed

Reputation: 448

Try This You have to enter just combo box name and pass the query parameters

Class SurroundingClass
Private path = "Your connection string"
Private con As OleDbConnection = New OleDbConnection(path)

Public Sub ComboData(ByVal cmb As ComboBox, ByVal colname As String, ByVal dbname As String, ByVal wherecondition As String)
    con.open()
    cmb.Items.Clear()
    da = New OleDbDataAdapter("select " & colname & " from " & dbname & " where " & wherecondition, con)
    dt = New DataTable()
    da.Fill(dt)

    For Each row As DataRow In dt.Rows
        cmb.Items.Add(row(0).ToString())
    Next

    con.close()
End Sub

End Class

Upvotes: 1

Related Questions