View Single Post
  #1 (permalink)  
Old July 30th, 2008, 06:52 PM
scottkhughes scottkhughes is offline
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