CR_Office
asked on
How do I fix this error-"An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll"?
I hope someone can help me figure out this problem I have tried many things. I am using Visual Studio 2003 and have SQL 2000 loaded my local system. Below is the the sample code that I have been trying to get to work. I am new a programming so I will try not to ask to many stupid questions. Here is the code that I was testing to see if I could pull a dataset into a form. I am doing some sample so that I can understand the SQL connection for a project that I am working on.
' Imports 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=(loc al);Databa se=pubs;Us er ID=sa;Password=sa;Trusted_ Connection =True")
Dim myDataAdapter As New SqlDataAdapter
Dim myDataSet As DataSet = New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Set the SelectCommand properties...
myDataAdapter.SelectComman d = New SqlCommand
myDataAdapter.SelectComman d.Connecti on = myConnection
myDataAdapter.SelectComman d.CommandT ext = _
"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, aufname"
myDataAdapter.SelectComman d.CommandT ype = CommandType.Text
'Open the database connection...
myConnection.Open()
'Now execute the command...
'myDataAdapter.SelectComma nd.Execute NonQuery()
'Fill the DataSet object with data...
myDataAdapter.Fill(myDataS et, "authors") ' <<< Receive the error message here "An unhandled exception of type 'System.Data.SqlClient.Sql Exception'
' occurred in system.data.dll"?
'Close the database connection...
myConnection.Close()
'Set the DataGrid properties to bind it to our data...
grdAuthorTitles.DataSource = myDataSet
grdAuthorTitles.DataMember = "authors"
End Sub
End Class
' Imports 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=(loc
Dim myDataAdapter As New SqlDataAdapter
Dim myDataSet As DataSet = New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Set the SelectCommand properties...
myDataAdapter.SelectComman
myDataAdapter.SelectComman
myDataAdapter.SelectComman
"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, aufname"
myDataAdapter.SelectComman
'Open the database connection...
myConnection.Open()
'Now execute the command...
'myDataAdapter.SelectComma
'Fill the DataSet object with data...
myDataAdapter.Fill(myDataS
' occurred in system.data.dll"?
'Close the database connection...
myConnection.Close()
'Set the DataGrid properties to bind it to our data...
grdAuthorTitles.DataSource
grdAuthorTitles.DataMember
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
and in fill
myDataAdapter.Fill(myDataS et)
myDataAdapter.Fill(myDataS
myDataAdapter = New sqldatadapter(strcommandte xt, myConnection )
didnt you forget INNER before your join?
I might be mistaken and have no SQL server here to test it, but I believe you can only INNER JOIN, LEFT OUTER JOIN and RIGHT OUTER JOIN in SQL Server.
I might be mistaken and have no SQL server here to test it, but I believe you can only INNER JOIN, LEFT OUTER JOIN and RIGHT OUTER JOIN in SQL Server.
the sql is fine, that worked in a test :)
Dim sqlConn As New SqlClient.SqlConnection(". ....")
sqlConn.Open()
Dim strSQL As String = "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, aufname"
Dim cmd As New SqlCommand(strSQL, sqlConn)
Dim ta As New SqlClient.SqlDataAdapter(c md)
Dim ds As New DataSet
ta.Fill(ds)
sqlConn.Dispose()
That should work
sqlConn.Open()
Dim strSQL As String = "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, aufname"
Dim cmd As New SqlCommand(strSQL, sqlConn)
Dim ta As New SqlClient.SqlDataAdapter(c
Dim ds As New DataSet
ta.Fill(ds)
sqlConn.Dispose()
That should work
Ofcourse fill in your own connectionstring at the ... hehehe
I'm not sure whether this would produce the particular error message reported, but
"SELECT au_lname, au_fname ...
and
"ORDER BY au_lname, aufname"
are incompatible. Compare "au_fname" and "aufname".
Roger
"SELECT au_lname, au_fname ...
and
"ORDER BY au_lname, aufname"
are incompatible. Compare "au_fname" and "aufname".
Roger
ASKER
I would like to thanks everyone for comments about my problem. It ended up to be a connection problem and the link that bruintje referenced in his comment help to me resolve my problem so he is the one that got out of a bind.
Thanks
Thanks
Dim myConnection As SqlConnection = New _
SqlConnection("Server=(loc
Dim myDataAdapter As SqlDataAdapter
Dim myDataSet As datatable = New datatable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Set the SelectCommand properties...
dim strcommandtext as string= "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, aufname"
myDataAdapter = New myDataAdapter(strcommandte
'Fill the DataSet object with data...
myDataAdapter.Fill(myDataS
'Close the database connection...
myConnection.Close()
'Set the DataGrid properties to bind it to our data...
grdAuthorTitles.DataSource
grdAuthorTitles.DataMember
End Sub