Subject: An unhandled exception of type 'System.Data.SqlCli
Posted By: BB Post Date: 12/2/2003 2:17:37 PM
Can someone give me some ideas on how to solve this. I am working on the example on page 638 in the book. The only differences I made was I used one of my databases out on the server and a different query. I get this error right at myConnection.Open.

 See code below:

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim myConnection As SqlConnection = New _
SqlConnection("server=Servername; database=databasename; uid=UUU; pwd=PPP")


    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.SelectCommand = New SqlCommand()
        myDataAdapter.SelectCommand.Connection = myConnection
        myDataAdapter.SelectCommand.CommandText = _
            "Select col1, col2, col3, col4" & _
            "from table1 order by col1"
        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, "Test")

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

        'set the datagrid properties to bind it to our data...
        grdAuthorTitles.DataSource = mydataset
        grdAuthorTitles.DataMember = "Test"

    End Sub
End Class


Any thoughts? Thanks!


Reply By: jlick Reply Date: 12/2/2003 2:25:45 PM
What error? It was truncated in the subject.
What book?

I think I know what your issue is anyway:

myDataAdapter.SelectCommand.CommandText = _
            "Select col1, col2, col3, col4" & _
            "from table1 order by col1"

There is a space missing between col4 & from. Your command would be:
"Select col1, col2, col3, col4from table1 order by col1"

BTW:
It is faster in VB.NET to use a string builder rather than the &.


John R Lick
JohnRLick@hotmail.com
Reply By: BB Reply Date: 12/2/2003 2:37:15 PM
Here is the full error

"An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll
Additional information: System error."

I took away the space, but still get the error. I will look in to trying the string builder.

Thanks

Reply By: jlick Reply Date: 12/2/2003 4:34:58 PM
Do you mean that you added a space? I was telling you that there should have been a space where there wasn't one.

Another thing you may want to try, is having the code print out the SQL statement that you want to try. Copy & Paste the SQL statement into a Query program (SQL Query Analyzer for MS-SQL). And see what error you get. Resolve it there, then fix your VB to build the correct statement.

John R Lick
JohnRLick@hotmail.com
Reply By: risu Reply Date: 12/4/2003 4:19:14 PM
BB,

I'm not familiar with the example in the book but my connection string uses this for id and password parameters not uid or pwd

user id=XXXXX; password=XXXXX


Also enclose the conn.open() with a try block and have a printline or msgbox show you the ex.message or ex.stacktrace


Try
   SqlConn.Open()
   SqlDataAdapter.Fill(ds, "Data")
Catch ex As Exception
   MsgBox(ex.Message)
Finally
   SqlConn.Close()
End Try


Thanks,
Risu

Go to topic 7222

Return to index page 992
Return to index page 991
Return to index page 990
Return to index page 989
Return to index page 988
Return to index page 987
Return to index page 986
Return to index page 985
Return to index page 984
Return to index page 983