Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > BOOK: Beginning VB.NET Databases
|
BOOK: Beginning VB.NET Databases
This is the forum to discuss the Wrox book Beginning VB.NET Databases by Thearon Willis; ISBN: 9780764568008
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning VB.NET Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 15th, 2005, 02:35 AM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old March 15th, 2005, 07:15 AM
Thearon's Avatar
Wrox Author
 
Join Date: Dec 2003
Posts: 396
Thanks: 0
Thanked 8 Times in 8 Posts
Default

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
 
Old March 16th, 2005, 03:27 AM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old March 16th, 2005, 06:39 AM
Thearon's Avatar
Wrox Author
 
Join Date: Dec 2003
Posts: 396
Thanks: 0
Thanked 8 Times in 8 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
vb.net 2008 re: VB.NET databases book bigbearjeff VB.NET 0 June 2nd, 2008 01:22 PM
ISBN: 0-7645-6800-0 :Beginning VB.NET Databases Aiking BOOK: Beginning VB.NET Databases 1 July 11th, 2006 09:37 AM
Beginning ASP.Net Databases Using VB.Net ISBN 6195 tlamazares SQL Server ASP 1 December 15th, 2003 01:28 PM





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