Error trying to connect to a sql db

diverdan

Active member
Joined
Jan 2, 2006
Messages
34
Location
London UK
Programming Experience
Beginner
Hi there,

Can someone please help me out with a problem.
I am very new to vb.net and having some problems trying to connect to a db using a datagrid.


This is my code.

VB.NET:
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim objConnection As SqlConnection = New _
    SqlConnection("server=HPNX6130;database=pubs;Trusted_Connection=True;")

    Dim objDataAdapter As New SqlDataAdapter
    Dim objDataSet As DataSet = New DataSet

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents grdAuthorTitles As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.grdAuthorTitles = New System.Windows.Forms.DataGrid
        CType(Me.grdAuthorTitles, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'grdAuthorTitles
        '
        Me.grdAuthorTitles.DataMember = ""
        Me.grdAuthorTitles.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.grdAuthorTitles.Location = New System.Drawing.Point(48, 32)
        Me.grdAuthorTitles.Name = "grdAuthorTitles"
        Me.grdAuthorTitles.Size = New System.Drawing.Size(344, 224)
        Me.grdAuthorTitles.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(448, 366)
        Me.Controls.Add(Me.grdAuthorTitles)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.grdAuthorTitles, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        objDataAdapter.SelectCommand = New SqlCommand
        objDataAdapter.SelectCommand.Connection = objConnection
        objDataAdapter.SelectCommand.CommandText = _
        "SELECT au_lname, au_fname, title, price " & _
        "FROM authors " & _
        "JOIN titleauthor ON author.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

        'objConnection.Open()

        objDataAdapter.Fill(objDataSet, "authors")

        'objConnection.Close()

        grdAuthorTitles.DataSource = objDataSet
        grdAuthorTitles.DataMember = "authors"

        objDataAdapter = Nothing
        objConnection = Nothing
    End Sub

    Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles grdAuthorTitles.Navigate

    End Sub

    Private Sub SqlConnection1_InfoMessage(ByVal sender As System.Object, ByVal e As System.Data.SqlClient.SqlInfoMessageEventArgs)

    End Sub
End Class

When I try run the code I get an error saying
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

Additional information: System error.

Can anyone suggest what I have done wrong?

Thanks for your help!!
 
VB.NET:
Try
    objDataAdapter.Fill(objDataSet, "authors")
Catch sqlEx As System.Data.SqlClient.SqlException
    MessageBox.Show sqlEx.ToString
End Try

That will give you more details as to what is wrong.

-tg
 
Back
Top