Wrox Programmer Forums
BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7
This is the forum to discuss the Wrox book Beginning Microsoft Visual Basic 2008 by Thearon Willis, Bryan Newsome; ISBN: 9780470191347
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7 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 July 30th, 2008, 06:52 PM
Registered User
Join Date: Jul 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to scottkhughes
Default Unable to connect to pubs database... help!

Greetings all,

I'm trying to connect to the pubs database (using Visual Studio 2008 and SQL Server Express), but I'm unable to for some reason, and I can't figure out why. It would help if I received some error messages, but I'm not receiving any. The program runs, but only returns a window with no data. The code below was taken from Chapter 17 of the Visual Basic 2008 book and the only code that deviates from what is found in the book is the SqlConnection constructor, so I'm inclined to believe the error is there.

I ran a different program to see if I could manually connect the database to a form, and was able to do it with no problem. Any suggestions as to what the issue could be would be greatly appreciated!!! The code is below with the SqlConnection line highlighted...

Thanks in advance for your help!

Code below:

'Import Data and SqlClient namespaces
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1

    Dim objConnection As New SqlConnection("server=HOME-PC;database=pubs;user id=sa;password=;")

    Dim objDataAdapter As New SqlDataAdapter()
    Dim objDataSet As New DataSet()

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Set the SelectCommand properties...
        objDataAdapter.SelectCommand = New SqlCommand()
        objDataAdapter.SelectCommand.Connection = objConnection
        objDataAdapter.SelectCommand.CommandText = _
        "SELECT au_lname, au_fname, title, price " & _
        "FROM authors " & _
        "JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
        "JOIN titles ON titleauthor.title_id = titles.title_id " & _
        "ORDER BY au_lname, au_fname"
        objDataAdapter.SelectCommand.CommandType = CommandType.Text

        'Open the database connection

        'Fill the DataSet object with data...
        objDataAdapter.Fill(objDataSet, "authors")

        'Close the database connection

        'Set the DataGridView properties to bind it to our data
        grdAuthorTitles.AutoGenerateColumns = True
        grdAuthorTitles.DataSource = objDataSet
        grdAuthorTitles.DataMember = "authors"

        'Clean up
        objDataAdapter = Nothing
        objConnection = Nothing

    End Sub

End Class
Old September 10th, 2008, 08:35 AM
Registered User
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Scott,

Since you don't get any error, your connection string must be fine, otherwise the program
would have stopped with an error at the line objConnection.Open

(Although you could leave out objConnection.Open and objConnection.Close, since the DataAdapter
will take care off opening and closing the connection).

The only thing I can think off is that the SQL statement just doesn't return any data, because
- The SQL statement has a typo
- There is no data in (one of) your tables

If checked and doublechecked the code, but I don't see any typo. Can you confirm that all the
three tables have data? (authors, titleauthor, titles)

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to connect to Oracle. rupen Classic ASP Databases 0 August 18th, 2008 02:50 AM
unable to connect sultan SQL Server 2005 1 June 6th, 2008 03:38 PM
Unable to connect to server suganya_db SQL Server 2000 1 January 3rd, 2005 09:17 AM
Pubs Database pro-logic ADO.NET 0 July 10th, 2003 11:50 PM

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