Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / September 2005

Tip: Looking for answers? Try searching our database.

Exception has been thrown by the target of an invocation mscorlib

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robson Carvalho Machado - 30 Sep 2005 16:26 GMT
Dear Friends,

I'm trying to create a .NET class named TESTE using VB.NET that can be
called by an ASP page but I got an error that I could not fix.

TEST CLASS code:
==========

Imports System.Web.Mail
Public Class update
   Public update As String
   Public server As String
   Public database As String

   Dim strConnect As String
   Dim SQLStmt As String
   Dim dbConn, dbComm, dbRead
   Dim name

   Public Function envia()
       strConnect = "Provider=SQLOLEDB;Server=" + server + ";Database=" +
database + ";Trusted_Connection=yes"
       dbConn = New System.Data.OleDb.OleDbConnection(strConnect)
       dbConn.Open()

       SQLStmt = "          select                        "
       SQLStmt = SQLStmt & " name                "
       SQLStmt = SQLStmt & "from                          "
       SQLStmt = SQLStmt & " table        "
       SQLStmt = SQLStmt & "where                         "
       SQLStmt = SQLStmt & " status = 's'                 "
       dbComm = New System.Data.OleDb.OleDbCommand(SQLStmt, dbConn)
       dbRead = dbComm.ExecuteReader()
       If dbRead.read() Then
           name = dbRead("name")
       End If
       Return name
       '----------------------------------------------
   End Function
End Class

To compyle this DLL I'm setting PROPERTIES/CONFIGURATION PROPERTIES/BUILD as
Register for COM Interop so VS compilation creates a .DLL and a .TLB file.

After that I put this 2 files (dll and tlb) into my server and register it
using the following command:

REGASM teste.dll /tlb teste.tlb /codebase

TESTE.ASP Code that calls my .NET Class:
========================

<%
Dim obj
set obj = Server.CreateObject("teste.update")
obj.update = "resultado"
obj.server = "servername"
obj.database = "databasename"
response.write obj.envia()
%>

When executed, this page returns the following error message:

mscorlib error '80131604'
Exception has been thrown by the target of an invocation.
/teste.asp, line 7

I could see that it only occurs when I call

When I clear all code leaving just the below code it works fine without any
error message, but if I include dbConn.Open() the error returns to apear.

   Public Function envia()
       strConnect = "Provider=SQLOLEDB;Server=" + server + ";Database=" +
database + ";Trusted_Connection=yes"
       dbConn = New System.Data.OleDb.OleDbConnection(strConnect)
       Return strConnect
       '----------------------------------------------
   End Function
End Class

Can anyone help me?
Marina - 30 Sep 2005 16:51 GMT
The problem is opening the connection. Presumably, whatever process is
running the ASP, is not configured to be a user in the database.

Either make that process a user on the database (which may be hard if it is
a local account, and the server is on a different machine), or pass in a
username/password to connect.

Code blocks like that should be in a Try/Catch anyway, so that your function
can deal with connection issues, as well as query execution issues.

Additionally, I recommend you use Option Strict On, and force your function
to have a return type. It just makes things more readable, and often catches
coding errors at compile time.

And last (but certainly not least), you are never closing  the connection or
the reader at the end of your function. This is a big no-no. You will end up
with a connection leak.

> Dear Friends,
>
[quoted text clipped - 80 lines]
>
> Can anyone help me?
Robson Carvalho Machado - 30 Sep 2005 17:05 GMT
Dear Marina,

Thanks for you comments:

I've tryed to change connection string to

       strConnect = "Provider=SQLOLEDB;Server" + server + ";Database=" +
database + ";uid=myuser;pwd=password"

even after this  the problem shows itself the same way.

Best regards.
Robson Machado

> The problem is opening the connection. Presumably, whatever process is
> running the ASP, is not configured to be a user in the database.
[quoted text clipped - 98 lines]
> >
> > Can anyone help me?
Marina - 30 Sep 2005 17:27 GMT
There is still some problem connecting.

Try running your code from a .NET app, see what happens.

Also, put your code in a Try/Catch, and return the error message of the
exception, so that your ASP page can display it.

> Dear Marina,
>
[quoted text clipped - 128 lines]
>> >
>> > Can anyone help me?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.