p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning VB.NET Databases (http://p2p.wrox.com/forumdisplay.php?f=147)
-   -   WROX's Beginning VB.Net Databases by Thearon (http://p2p.wrox.com/showthread.php?t=26848)

PeterRen March 15th, 2005 02:35 AM

WROX's Beginning VB.Net Databases by Thearon
 
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

Thearon March 15th, 2005 07:15 AM

Peter,

This sounds as if you are using Visual Studio 2002. The HasRows property was introduced in Visual Studio 2003 with version 1.1 of the .Net Framework.

Can you verify the version of Visual Studio that you are running and then let's see if we can't provide a work around for you.

Thearon

PeterRen March 16th, 2005 03:27 AM

Hi,
Thanks for the quick response. Yes you are correct I am using Visual Studio 2002.

Do you have a fix please?

WIll this continue to be a problem as I work through the book?

Many thanks
Peter

Peter

Thearon March 16th, 2005 06:39 AM

Peter,

This simplest solution for you is to comment out the line of code that contains this IF statement:

If objDataReader.HasRows Then

Don't forget the also comment out the corrseponding End If statement.

As far as your other error goes the line should have been coded as:

For intIndex As Integer = 0 To objDataReader.FieldCount - 1

The intIndex variable is declared inline as part of the For statement.

Thearon


All times are GMT -4. The time now is 07:15 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.