p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7 (http://p2p.wrox.com/forumdisplay.php?f=403)
-   -   Unable to connect to pubs database... help! (http://p2p.wrox.com/showthread.php?t=69763)

scottkhughes July 30th, 2008 06:52 PM

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

bvdijk September 10th, 2008 08:35 AM

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)

All times are GMT -4. The time now is 04:00 AM.

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