Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2008 > BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 30th, 2008, 06:52 PM
Registered User
 
Join Date: Jul 2008
Location: Brooklyn, New York, USA.
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!
Scott

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
        objConnection.Open()

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

        'Close the database connection
        objConnection.Close()

        '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
Reply With Quote
  #2 (permalink)  
Old September 10th, 2008, 08:35 AM
Registered User
 
Join Date: Sep 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 08:17 AM
Pubs Database pro-logic ADO.NET 0 July 10th, 2003 11:50 PM



All times are GMT -4. The time now is 05:49 AM.


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