Results 1 to 13 of 13

Thread: How do i use vb.net variables in a OleDbCommand select statement?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Location
    Winston Salem
    Posts
    45

    Unhappy How do i use vb.net variables in a OleDbCommand select statement?

    My code is:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton1.Click
    2. Try
    3. Dim OleDbCommand1 As New OleDb.OleDbCommand("SELECT * FROM Table1 WHERE Field = '" & TextBox1.Text & "' ", OleDbConnection)
    4. Dim OleDbDataAdapter1 As New OleDb.OleDbDataAdapter(OleDbCommand1)
    5. OleDbDataAdapter1.Fill(DataSet1)
    6. Catch oledbex As OleDb.OleDbException
    7.             MessageBox.Show(oledbex.Message)
    8.         End Try
    9.  
    10.     End Sub

    A datagrid is being used to show the information. I want the grid to show the fields where the text in the textbox matches the text in a database field. So far when i click the button nothing happens. What am i doing wrong. please help me.

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: How do i use vb.net variables in a OleDbCommand select statement?

    What are you doing with the dataset after you populate it?

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2005
    Location
    Winston Salem
    Posts
    45

    Unhappy Re: How do i use vb.net variables in a OleDbCommand select statement?

    Im not doing anything with it. should i dispose of it? By the way, this is the first time ive ever had to use a datagrid or a sql statement that involved variables. i have no clue what to do.

  4. #4
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: How do i use vb.net variables in a OleDbCommand select statement?

    the dataset is a data object, it's an intermediary between your database and the datagrid control.

    Set the datagrid's datasource proeprty to the dataset's table you want to bind to.

    If this is a webform datagrid, check out the link in my signature for some really good info.

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2005
    Location
    Winston Salem
    Posts
    45

    Re: How do i use vb.net variables in a OleDbCommand select statement?

    The dataset is bound to the datagrid. The datasource is set to the dataset and the datamember is set to the table. This is where i am getting confused. to me it looks like the code should work.

  6. #6
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: How do i use vb.net variables in a OleDbCommand select statement?

    In the example you gave, none of that is happening. You're filling the dataset with the adapter and then the try/catch/end try is done. Can you give more codewhere you're setting all these properties?

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2005
    Location
    Winston Salem
    Posts
    45

    Re: How do i use vb.net variables in a OleDbCommand select statement?

    the dataset is created in design mode. I have edited the properties of the datagrids datasource and datamember in design mode as well. should i be doing it in code?

  8. #8
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: How do i use vb.net variables in a OleDbCommand select statement?

    IMO - I do, it's easier to follow instead of digging through controls, but that's up to you.

    Try just YourDataGridName.DataBind() right after you fill the dataset. Since everything is already looking the right direction, you might just need to tell it to go.

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2005
    Location
    Winston Salem
    Posts
    45

    Re: How do i use vb.net variables in a OleDbCommand select statement?

    How do i call the databind method. its not coming up in intellisense

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2005
    Location
    Winston Salem
    Posts
    45

    Re: How do i use vb.net variables in a OleDbCommand select statement?

    I have tried using the update method. it doesnt work either

  11. #11
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: How do i use vb.net variables in a OleDbCommand select statement?

    Webforms datagrids will allow databind, winforms are different; sorry.

    You can try Me.Refresh(). If everything is lined up right (query, datasources, ect), it should be simple as that.

    If not, I would try hardcoding it in code, just for gits and shiggles to see if it might be a design-view configuration issue.

    VB Code:
    1. Dim dataset1 As New DataSet
    2.         Me.DataGrid1.DataSource = dataset1.Tables("SourceTableName")

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How do i use vb.net variables in a OleDbCommand select statement?

    Before you refill your dataset.... clear it first DataSet.Clear ... that will clear out the existing data (if you have multiple datatables, you can just .Clear the one table you are dealing with). Then you can fill your dataset, and rebind it back to your grid.

    About half way down this article I describe this phenominon (sp?)
    http://www.developerkb.com/modules/w...p?articleid=58

    When I was doing this, it looked like it wasn't doing anything either, but what I found was that it kept adding to my datatable.... and if I sorted, I could see the dupes.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    Member
    Join Date
    Nov 2005
    Location
    Winston Salem
    Posts
    45

    Re: How do i use vb.net variables in a OleDbCommand select statement?

    ok. i got it working. thanks a lot

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