Results 1 to 7 of 7

Thread: [2005] DisplayDataReader to Form

  1. #1

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    [2005] DisplayDataReader to Form

    Please bear with me I'm working my way through the transition from VB 6. I think I'm going to change my name from rjbudz to FrustratedWithVB.NET.

    I have this DataReader, and I managed to get the data I need from my database.

    Now what? I want to show the results to the user. How do I do it?

    VB6 allows you to loop through a recordset (a DataReader in this case?) and display the data in a MSFlexGrid. How do I do the same thing in VB 2005?

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] DisplayDataReader to Form

    Display the results in a DataGridView. One way you can do this is load your reader into a DataTable and then use that DataTable as your DataGridView's DataSource.

    vb.net Code:
    1. ' Open the connection and execute the reader.
    2. conn.Open()
    3. Dim dr As OleDbDataReader = cmd.ExecuteReader()
    4. ' Create the DataAdapter and DataTable
    5. ' and load the DataTable.
    6. Dim dt As New DataTable()
    7. dt.Load(dr)
    8.  
    9. ' Populate the DataGridView.
    10. Me.BindingSource1.DataSource = dt
    11. Me.DataGridView1.DataSource = Me.BindingSource1
    12.  
    13. ' Clean up.
    14. dr.Close()
    15. conn.Close()

    Check out the MSDN documentation as well for lots more. Good luck.

  3. #3

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [2005] DisplayDataReader to Form

    Thanks. The code you wrote seems to fill add the correct number of rows to the DataGridView view, but there is no data displayed. That is, I know the SQL statement returns three records, the DataGridView adds three new rows, but the cells are empty. THAT doesn't seem right

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] DisplayDataReader to Form

    No, that doesn't seem right.

    Can you post all of the code you are using to fill the DataGridView?

  5. #5

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [2005] DisplayDataReader to Form

    Code:
        Public Function GetReportNumbers(ByVal SQLString As String) As String
    
            Dim conn As New OdbcConnection
            Dim comm As OdbcCommand
    
            conn = New OdbcConnection(My.Settings.DBConnection)
            conn.Open()
            comm = New OdbcCommand(SQLString, conn)
    
            Dim dr As OdbcDataReader = comm.ExecuteReader()
    
            ' Create the DataAdapter and DataTable
            ' and load the DataTable.
    
            Dim dt As New DataTable()
    
            dt.Load(dr)
    
            ' Populate the DataGridView.
            Me.BindingSource1.DataSource = dt
    
            Me.DataGridView1.DataSource = Me.BindingSource1
    
            ' Clean up.
            dr.Close()
    
            conn.Close()
    
        End Function

  6. #6
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] DisplayDataReader to Form

    I can't see what is wrong with that. I sometimes use something very similar to fill the DataGridView when I need to display records that I don't need to update.
    vb.net Code:
    1. Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Nwind.mdb;")
    2. Dim SQLString As String = _
    3.     "SELECT CustomerID, ContactName " & _
    4.     "FROM Customers " & _
    5.     "WHERE CustomerID LIKE @CustID " & _
    6.     "AND ContactName LIKE @ContName"
    7.  
    8. Dim cmd As New OleDbCommand(SQLString, conn)
    9. cmd.Parameters.AddWithValue("@CustID", "%" & Me.TextBox1.Text & "%")
    10. cmd.Parameters.AddWithValue("@ContName", "%" & Me.TextBox2.Text & "%")
    11.  
    12. conn.Open()
    13. Dim dr As OleDbDataReader = cmd.ExecuteReader()
    14. Dim dt As New DataTable()
    15. dt.Load(dr)
    16.  
    17. Me.DataGridView1.DataSource = dt
    18.  
    19. dr.Close()
    20. conn.Close()

    So this populates the DataGridView with the correct amount of rows, but doesn't actually put any values in the cells?

  7. #7

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: [2005] DisplayDataReader to Form

    Yep. I checked it with a couple SQL statements. One has three rows, one has a whole bunch (10+). The Grid seems to respond with the right number of rows, but no data in the cells.

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