On page 104, of WROX's Beginning
VB.Net Databases by Thearon Willis ISBN: 0-7645-6800-0, they have the code:
Private Sub btnDataReader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDataReader.Click
'Validate connection state
If Not IsConnectionOpen() Then
Exit Sub
End If
'Declare and initialize a new instance of the OleDbCommand class
Dim objCommand As New OleDbCommand(txtSQL.Text, objConnection)
'Set the CommandType property
If optText.Checked Then
objCommand.CommandType = CommandType.Text
Else
objCommand.CommandType = CommandType.TableDirect
End If
'Declare a OleDbDataReader object
Dim objDataReader As OleDbDataReader
'Declare a String variable
Dim strData As String
Try
'Execute the SQL text
objDataReader = objCommand.ExecuteReader()
'Check to see if we have data
If objDataReader.HasRows Then
'Process all rows
While objDataReader.Read()
'Clear the variable
strData = String.Empty
'Get the data in each column
For intIndex As Integer = 0 To objDataReader.FieldCount - 1
strData &= objDataReader.Item(intIndex).ToString & ", "
Next
'Remove the last comma from the string
strData = strData.Remove(strData.Length - 2, 2)
'Write the data to the TextBox
txtSQL.Text &= ControlChars.CrLf & strData
End While
End If
'Close the reader
objDataReader.Close()
Catch OleDbExceptionErr As OleDbException
MessageBox.Show(OleDbExceptionErr.Message, "Access SQL")
End Try
'Cleanup
objCommand.Dispose()
objCommand = Nothing
objDataReader = Nothing
End Sub
Private Sub btnDataAdapter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDataAdapter.Click
'Validate connection state
If Not IsConnectionOpen() Then
Exit Sub
End If
'Declare and initialize a new instance of the OleDbCommand class
Dim objCommand As New OleDbCommand(txtSQL.Text, objConnection)
'Set the CommandType property
If optText.Checked Then
objCommand.CommandType = CommandType.Text
Else
objCommand.CommandType = CommandType.TableDirect
End If
'Declare a OleDbDataAdapter object
Dim objDataAdapter As New OleDbDataAdapter
'Declare a DataSet object
Dim objDataSet As New DataSet
'Set the SelectCommand for the OleDbDataAdapter
objDataAdapter.SelectCommand = objCommand
Try
'Populate the DataSet
objDataAdapter.Fill(objDataSet)
''Bind the DataSet to the DataGrid
'grdResults.DataSource = objDataSet
''Tell the DataGrid which table in the DataSet to use
'grdResults.DataMember = objDataSet.Tables(0).TableName
'Alternate data binding
grdResults.SetDataBinding(objDataSet, objDataSet.Tables(0).TableName)
'Set the AlternatingBackColor property
grdResults.AlternatingBackColor = Color.WhiteSmoke
'Set the GridLineStyle property
grdResults.GridLineStyle = DataGridLineStyle.None
'Set the SelectionBackColor and SelectionForeColor properties
grdResults.SelectionBackColor = Color.LightGray
grdResults.SelectionForeColor = Color.Black
Catch OleDbExceptionErr As OleDbException
MessageBox.Show(OleDbExceptionErr.Message, "Access SQL")
End Try
'Cleanup
objCommand.Dispose()
objCommand = Nothing
objDataAdapter.Dispose()
objDataAdapter = Nothing
objDataSet.Dispose()
objDataSet = Nothing
End Sub
When I try to compile this code I receive the following errors:
Code snippet
'Execute the SQL text
objDataReader = objCommand.ExecuteReader()
'Check to see if we have data
If objDataReader.HasRows Then
Error Message
âHasRows is not a member of âSystemData.OLEDB â Line 344
I also received the following error
Code Snippet
For intIndex = 0 To objDataReader.FieldCount - 1
strData &= objDataReader.Item(intIndex).ToString & ", "
Next
Error Message
Name IntIndex is not declared â line 350
Can you please provide me with a solution to the above problems as I am keen to continue the exercises?
Regards
Peter Renwick
Brisbane, Queensland, Australia
Peter