Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Unhandled exception - FIGURED IT OUT

 

Hello one and all,

 

I just began to work with VB.NET Standard edition connecting to my trial SQL copy on my local workstation.

 

I am working with some sample code from a book and here is the code:

' Import Data and SqlClient namespaces...
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
   Inherits System.Windows.Forms.Form

   Dim myConnection As SqlConnection = New _
       SqlConnection("Server=(local);database=pubs;uid=sa;pwd=sa;")

   Dim myDataAdapter As New SqlDataAdapter
   Dim myDataSet 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.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                   Or System.Windows.Forms.AnchorStyles.Left) _
                   Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
       Me.grdAuthorTitles.DataMember = ""
       Me.grdAuthorTitles.HeaderForeColor = System.Drawing.SystemColors.ControlText
       Me.grdAuthorTitles.Location = New System.Drawing.Point(5, 5)
       Me.grdAuthorTitles.Name = "grdAuthorTitles"
       Me.grdAuthorTitles.Size = New System.Drawing.Size(584, 192)
       Me.grdAuthorTitles.TabIndex = 0
       '
       'Form1
       '
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(592, 196)
       Me.Controls.Add(Me.grdAuthorTitles)
       Me.Name = "Form1"
       Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
       Me.Text = "Bound DataSet"
       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
       ' Set the SelectCommand properties...
       myDataAdapter.SelectCommand = New SqlCommand
       myDataAdapter.SelectCommand.Connection = myConnection
       myDataAdapter.SelectCommand.CommandText = _
           "SELECT au_lname, au_fname, title, price " & _
           "FROM authors " & _
           "JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
           "JOIN titles ON titleauothr.title_id = titles.title_id " & _
           "ORDER BY au_lname, au_fname"
       myDataAdapter.SelectCommand.CommandType = CommandType.Text

       ' Open the database connection...
       myConnection.Open()
       ' Now execute the command...
       myDataAdapter.SelectCommand.ExecuteNonQuery()

       ' Fill the DataSet object with data...
       myDataAdapter.Fill(myDataSet, "authors")

       ' Close the database connection...
       myConnection.Close()

       ' Set the DataGrind properties to bind it to our data...
       grdAuthorTitles.DataSource = myDataSet
       grdAuthorTitles.DataMember = "authors"

   End Sub
End Class

 

The error I am getting is An Unhandled Exception of type 'System.Data.SqlClient.SqlException occurred in System.data.dll'

 

The error is occurring at the line myDataAdapter.SelectCommand.ExecuteNonQuery()

 

Anyone have a clue as to what is going on or what the book might have wrong or what I might be doing wrong on this?

Thanks

:)

 

Sorry everyone, just figured out what the problem was. Amazing how a typo can mess things up. For those of you that want to know, the second JOIN line above has author spelled incorrectly and that was the problem. I found it after I did this post and things seem to be working just fine now. :p

Edited by Derek Stone

Ira Richard Smith

IraRichardSmith.Net

  • *Experts*
Posted
You should be using ExecuteQuery, not ExecuteNonQuery, as well. ExecuteNonQuery is for INSERT, UPDATE and DELETE statements, which do not return results.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...