|
-
Aug 6th, 2007, 02:58 PM
#1
Thread Starter
Hyperactive Member
[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?
-
Aug 6th, 2007, 03:03 PM
#2
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:
' Open the connection and execute the reader.
conn.Open()
Dim dr As OleDbDataReader = cmd.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()
Check out the MSDN documentation as well for lots more. Good luck.
-
Aug 6th, 2007, 04:33 PM
#3
Thread Starter
Hyperactive Member
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
-
Aug 6th, 2007, 06:02 PM
#4
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?
-
Aug 6th, 2007, 06:23 PM
#5
Thread Starter
Hyperactive Member
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
-
Aug 6th, 2007, 06:47 PM
#6
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:
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Nwind.mdb;")
Dim SQLString As String = _
"SELECT CustomerID, ContactName " & _
"FROM Customers " & _
"WHERE CustomerID LIKE @CustID " & _
"AND ContactName LIKE @ContName"
Dim cmd As New OleDbCommand(SQLString, conn)
cmd.Parameters.AddWithValue("@CustID", "%" & Me.TextBox1.Text & "%")
cmd.Parameters.AddWithValue("@ContName", "%" & Me.TextBox2.Text & "%")
conn.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader()
Dim dt As New DataTable()
dt.Load(dr)
Me.DataGridView1.DataSource = dt
dr.Close()
conn.Close()
So this populates the DataGridView with the correct amount of rows, but doesn't actually put any values in the cells?
-
Aug 6th, 2007, 07:09 PM
#7
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|