Auto search through textbox and dynamically populate datagrid-VBForums
Results 1 to 7 of 7

Thread: Auto search through textbox and dynamically populate datagrid

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    4

    Auto search through textbox and dynamically populate datagrid

    hello everyone
    can anyone help on this.
    i have a table with column's (itemcode, itemname, price) on the vb form i have one search field (textbox1) and datagrid
    i have data as
    itemcode itemname price
    1234 apple 10
    1235 orange 12
    1345 grapes 15
    1356 banana 15
    1357 apricot 25
    2345 strawberry 20
    2346 guava 10
    now what i am trying is if i type 1 then grid show all data's starting with 1 and as i type 12 then it should filter only remaining data with itemcode starting with 12... and so on.\
    i hope i am able to get my point through..
    simple search i am able to i.e. when whole string match's with itemcode it shows the details in gridview.
    code i m using on change event of textbox is
    Code:
     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    'i have opened connection above         
            cn.Open()
            Dim a As New OleDbDataAdapter("Select * from Item where ItemName = ' " & TextBox1.Text & "'", cn)
            Dim b As New DataSet
            a.Fill(b, "Item")
            DataGridView2.DataSource = b.Tables(0)
            cn.Close()
    
        End Sub
    thanx in advance

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,941

    Re: Auto search through textbox and dynamically populate datagrid

    If you want to make partial matches then you have to use the LIKE operator rather than =, and you must couple that with one or more wildcards. It appears that you want to perform a "starts with" search, so that means using a wildcard at the end of the input value that will match zero or more characters. In this and EVERY other case, you should also be using parameters, which will, among other things, avoid the error that you have made by putting a spurious space in your value.
    vb.net Code:
    1. Dim adapter As New OleDbDataAdapter("SELECT * FROM Item WHERE ItemName LIKE @ItemName", connection)
    2.  
    3. adapter.Parameters.AddWithValue("@ItemName", TextBox1.Text & "%")
    By the way, if all you're using is a single DataTable then using a DataSet is pointless. Just create a DataTable.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    4

    Re: Auto search through textbox and dynamically populate datagrid

    sorry, i have actually just started with vb.net and dont know use of parameter's.
    anyway i m getting following error if i use your code.
    Name:  error.jpg
Views: 1198
Size:  212.7 KB
    could you please explain code without parameter's

    Thanx


  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,941

    Re: Auto search through textbox and dynamically populate datagrid

    Soryy, that should be:
    Code:
    adapter.SelectCommand.Parameters

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    4

    Re: Auto search through textbox and dynamically populate datagrid

    Sorry it doesn't work, gives following error

    Name:  error.jpg
Views: 1123
Size:  58.3 KB

    and just a gentle reminder that values in textbox1 are numeric, if that's causing any problem..

    thanx

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,941

    Re: Auto search through textbox and dynamically populate datagrid

    Um, you've left off the AddWithValue. I meant that it should have been 'adapter.SelectCommand.Parameters' rather than just 'adapter.Parameters'. Parameters is a property of each command and each command is a property of the data adapter. AddWithValue is a method of the parameter collection that allows you to add a parameter with a value to the parameter collection of the adapter's SELECT command.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    4

    Re: Auto search through textbox and dynamically populate datagrid

    Thanx dear

    Finally i got it, i happen to do one tutorial with parameters. that's how got hold of it..

    Cheers


Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.