Exception has been thrown by the target of an invocation mscorlib

G

Guest

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?
 
M

Marina

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.
 
G

Guest

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
 
M

Marina

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top