Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 26th, 2007, 12:03 PM
Registered User
 
Join Date: Apr 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default vb conection to SQL db

I'm trying a sample program to list a field in an MS SQL db. The connection works but I get a message "A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Invalid column name 'DOCS_ID'." when the objDataAdapter.Fill(objDataTable) instruction is executed (as I step through).

I've done a similar example with MS Access and it works fine. This is a variation on the "Visual Basic 2005 Databases" book Chapter 2 Exercise 1.

The code:
        Dim strConnectionString As String = _
            "Provider=SQLOLEDB;" & _
            "Data Source=ABC_Server;" & _
            "Database=ABC_database;" & _
            "User ID=user_id;Password=user_password"
        Dim objConnection As New OleDbConnection(strConnectionString)
        Dim strSQL As String = "SELECT DOCS_ID"
        Dim objCommand As New OleDbCommand(strSQL, objConnection)
        Dim objDataAdapter As New OleDbDataAdapter(objCommand)
        Dim objDataTable As New Data.DataTable("DOCUMENT_STORE")
        Dim objDataRow As DataRow

        Try
            'open db connection
            objConnection.Open()

            'fill the Datatable object
            objDataAdapter.Fill(objDataTable)

            'load the list box on the form
            For Each objDataRow In objDataTable.Rows
                ListBox1.Items.Add(objDataRow.Item("DOCS_ID"))
            Next

        Catch oledbexceptionerr As Exception
            Debug.WriteLine(oledbexceptionerr.Message)

            'catch invalidoperationexceptionerr as InvalidOperationException)
            ' Debug.WriteLine(InvalidOperationException.Message)
        End Try

Jim
 
Old April 26th, 2007, 03:08 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by josmith2
         Dim strSQL As String = "SELECT DOCS_ID"
You are missing the rest of the query. I assume you want:

Dim strSQL As String = "SELECT DOCS_ID FROM DOCUMENT_STORE"


-Peter
 
Old April 26th, 2007, 04:13 PM
Registered User
 
Join Date: Apr 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's not it.

I get "A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Invalid object name 'dbo_THG_DOCS_DOCUMENT_STORE'."

In the example from the book, the "FROM" statement isn't there. I ASSUME that it gets is from this:

Dim objDataTable As New Data.DataTable("dbo_THG_DOCS_DOCUMENT_STORE")

But I don't know...

Jim
 
Old April 27th, 2007, 06:48 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

No. Dim objDataTable as New DataTable() is a memory resident object and, for all intents and purposes, it can be used as a container object to store a result set returned from a SQL query and do any manipulation that you may need.

That is what you do here:
objDataAdapter.Fill(objDataTable)

You are filling the datatable with the result set of your query.

The error you are getting is being generated by your RDBMS, not your code so, as Peter pointed out, I believe it has something to do with your Query OR in your query string you are not defining the correct catalog that the Table you are trying to select data from belongs to.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old April 27th, 2007, 08:01 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

As Doug pointed out...

   Dim objDataTable As New Data.DataTable("DOCUMENT_STORE")

this line is meaningless to the database. You could just as well do this:

   Dim objDataTable As New Data.DataTable("bologna")

and this line

   objDataAdapter.Fill(objDataTable)

would still load the data. The name you provide when you instantiate the DataTable class is used strictly for reference within the dataset (when it's used in a dataset). If you are only creating a DataTable, you don't even need to provide it a name.

All this said, in the end you still need to provide a query that returns data. This: "SELECT DOCS_ID" doesn't return data. The database engine is going to try looking for an object name "DOCS_ID" which it can't find so it's giving you the error.

-Peter
 
Old April 27th, 2007, 08:21 AM
Registered User
 
Join Date: Apr 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks to all. I'm a newbie at this and you've help clarify the problem for me. I'm still looking into it. I have an MS Access ODBC connection to the SQL db and the query I use there works fine. Hmmmm.

Jim
 
Old April 27th, 2007, 08:39 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Are you saying you are using a Microsoft Access driver to connect to Microsof SQL Server?

Also, at least in MS Sql Server and MySQL the only time a select statement will work without a reference to a table is if you do something like this:

SELECT 'foo'

Executing this command will return the string literal value of foo as your result set in all other cases you need to specify where you are selecting data from so, for the sake of argument, humor me.

Change your SQL statement from:
SELECT DOCS_ID

to
SELECT DOCS_ID FROM DOCUMENT_STORE (assuming that Document_Store is the name of a table in the database you have supplied in your connection string.)

What does that do to your code?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a SQL Server DB through VB code tknnguyen VB Databases Basics 2 July 2nd, 2008 11:28 AM
Webpage with DB conection make SQL OVERFLOW rtr1900 Classic ASP Databases 3 March 28th, 2008 07:30 AM
Read SQL Server DB using .ASP-VB leafboy75 VB How-To 1 May 11th, 2007 12:08 AM
conection between Express Server and VB ashishprem Pro VB Databases 0 January 23rd, 2007 07:41 AM
Need to connect to SQL DB in VB app codehappy VB How-To 3 April 21st, 2005 12:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.