Results 1 to 5 of 5

Thread: Use Parameter in query

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    5

    Use Parameter in query

    I wrote this code and i can populate result of query to combobox1.but i want to use parameter in my query and i want to range next combobox(such as combobox2) in attention to the selected item in combobox1.My sample databese name is test1 and has 3 field:categoryID,ParentID and Title.Please Help me
    here is my code

    Dim DataAdapter As OleDbDataAdapter = New OleDbDataAdapter()
    Dim myConn As OleDbConnection = New OleDbConnection
    Dim sqlStr As String
    'Dim conStr As String
    Dim dviewmanager As DataViewManager
    myConn.ConnectionString = "Provider=microsoft.jet.oledb.4.0;data source = C:\Documents and Settings\sce\Desktop\test1.mdb"
    'myConn = New OleDbConnection(constr)
    myConn.Open()
    'TextBox1.Text = "wsaaAS"
    sqlStr = "Select Title From Table1"
    DataAdapter = New OleDbDataAdapter(sqlStr, myConn)
    'Test1DataSet = New DataSet()
    DataAdapter.TableMappings.Add("testmapping", "Table1")
    DataAdapter.Fill(Test1DataSet)
    dviewmanager = Test1DataSet.DefaultViewManager
    ComboBox1.DataSource = dviewmanager
    ComboBox1.DisplayMember = "Table1.Title"
    myConn.Close()

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Use Parameter in query

    Follow the Blog link in my signature and check out my post on ADO.NET parameters.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    5

    Re: Use Parameter in query

    thank you very much.
    Can you give me the name of the afterupdate equivalent event in vb please.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Use Parameter in query

    I have no idea what "afterupdate" event you mean. If you want to know what events the ComboBox class has and what they are for then open the MSDN documentation for the ComboBox, navigate to the event list and read. They're all listed with a description and a link to their own topic, which will often contain a code example.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    5

    Re: Use Parameter in query

    I wrote a code.but when i select a value from combobox1 i want to show items in combobox2 that are related to the combobox1.Now by my code when i select item in combobox1,combobox2 values are all of the rows in in second field.Please help me what can i do.Please

    Imports System
    Imports System.Data
    Imports System.Data.OleDb
    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'Test1DataSet.Table1' table. You can move, or remove it, as needed.
    Me.Table1TableAdapter1.Fill(Me.Test1DataSet.Table1)


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim DataAdapter As OleDbDataAdapter = New OleDbDataAdapter()
    Dim myConn As OleDbConnection = New OleDbConnection
    Dim sqlStr As String
    'Dim conStr As String
    Dim dviewmanager As DataViewManager
    myConn.ConnectionString = "Provider=microsoft.jet.oledb.4.0;data source = C:\Documents and Settings\sce\Desktop\test1.mdb"
    'myConn = New OleDbConnection(constr)
    myConn.Open()
    'TextBox1.Text = "wsaaAS"
    sqlStr = "Select Title From Table1"
    DataAdapter = New OleDbDataAdapter(sqlStr, myConn)
    'Test1DataSet = New DataSet()
    DataAdapter.TableMappings.Add("testmapping", "Table1")
    DataAdapter.Fill(Test1DataSet)
    dviewmanager = Test1DataSet.DefaultViewManager
    ComboBox1.DataSource = dviewmanager
    ComboBox1.DisplayMember = "Table1.Title"
    myConn.Close()
    End Sub

    Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
    Dim DataAdapter As OleDbDataAdapter = New OleDbDataAdapter()
    Dim myConn As OleDbConnection = New OleDbConnection
    'Dim sqlStr As String
    'Dim conStr As String
    Dim sql As String
    Dim dviewmanager2 As DataViewManager
    myConn.ConnectionString = "Provider=microsoft.jet.oledb.4.0;data source = C:\Documents and Settings\sce\Desktop\test1.mdb"
    'myConn = New OleDbConnection(constr)
    myConn.Open()
    'sqlStr = "Select Title From Table1"
    sql = "SELECT ParentID FROM Table1 WHERE Title = '" & Me.ComboBox1.SelectedText & "'"
    DataAdapter = New OleDbDataAdapter(sql, myConn)
    'Test1DataSet = New DataSet()
    DataAdapter.TableMappings.Add("testmapping", "Table1")
    DataAdapter.Fill(Test1DataSet)
    dviewmanager2 = Test1DataSet.DefaultViewManager
    ComboBox2.ResetText()
    ComboBox2.DataSource = dviewmanager2
    ComboBox2.DisplayMember = "Table1.ParentID"
    myConn.Close()
    End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width