Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP & COM 2

Status
Not open for further replies.

RushiShroff

Programmer
Jan 23, 2002
216
IN
Here I want to do data access from COM.

I have a ready made code HowToProject3 from
Project Name : How2Project3
Class Name : ClassName

Code:
Public Function MethodName(ByVal strDbConnectionString As String, _
                           ByVal strSQL As String) As Variant

    '~~~~~~~~~~~~~ Set ADO objects ~~~~~~~~~~~~~~~~~~
    Dim oRs As New ADODB.Recordset
    Dim oCmd As New ADODB.Command
    Dim oConn As New ADODB.Connection
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    '~~~~~~ Open Database with method argument ~~~~~~
    oConn.Open strDbConnectionString
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    '~~~~~~~~~~ Assign values to ADO objects ~~~~~~~~~
    oCmd.CommandText = strSQL
    oCmd.CommandType = adCmdText
    Set oCmd.ActiveConnection = oConn
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    '~~~~~~~~~~~~~ Open the RecordSet ~~~~~~~~~~~~~~~
    oRs.Open oCmd
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    '~~~~~~~Assign RecordSet to Variant Array ~~~~~~~
    MethodName = oRs.GetRows
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    '~~~~~Close RecordSet/Connection ~~~~~~~~~~~~~~~~
    oRs.Close
    oConn.Close
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    '~~~~~~~~~~~~~ Set objects to nothing ~~~~~~~~~~~
    Set oRs = Nothing
    Set oCmd = Nothing
    Set oConn = Nothing
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
End Function

well they have given ready made compiled dll for this named
ProjectName3.dll

My question is while instantiating a component as an object, generally we write server.createObject("Project.ClassName")

So can we give any name to dll after compilation ?
I mean (after that will register it to the directory),name of dll matters or NOT ??.


In my ASP page
Code:
<%
	'-----~~~~~- Record Field Constants ~~~~~~~~~~~~~
    Const EXAMPLE_FIELD_ID = 0 	'<- ExampleFieldID
    Const EXAMPLE_FIELD_1  = 1	'<- ExampleField1
    Const EXAMPLE_FIELD_2  = 2	'<- ExampleField2
	'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
	'~~~~~~~~~~~~~~~~~~ Set Object  ~~~~~~~~~~~~~~~~~
	Set oClass = Server.CreateObject(&quot;How2Project3.ClassName&quot;)
	'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	'~~~~~~~~~~~~ Set method patameters ~~~~~~~~~~~~~
	strDbConnectionString = &quot;strDSN&quot;
	strSQL = &quot;Select ExampleFieldID, ExampleField1, ExampleField2 FROM ExampleTable&quot; 
	'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		
	'~~~~~~~~~~ Get RecordSet Variant Array ~~~~~~~~~
	vRecordArray = oClass.MethodName(strDbConnectionString, strSQL)
	'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	'~~~~~~~~~~~~~ Set Object to Nothing ~~~~~~~~~~~~
	Set oClass = Nothing
	'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%>
  

<HTML><HEAD><TITLE>How2 EXAMPLE</TITLE></HEAD><BODY>

Number of records returned is <%=UBound(vRecordArray, 2) + 1%><BR>

<TABLE BORDER=&quot;1&quot; CELLSPACING=&quot;3&quot;>
<%lngUBound = UBound(vRecordArray, 2) %>
<%For lngIndex = 0 to lngUBound %>
	<TR>
	<TD><%=vRecordArray(EXAMPLE_FIELD_ID, lngIndex)%></TD>
	<TD><%=vRecordArray(EXAMPLE_FIELD_1, lngIndex)%></TD>
	<TD><%=vRecordArray(EXAMPLE_FIELD_2, lngIndex)%></TD>
	</TR>
<%Next%>
</TABLE>

</BODY></HTML>

My next question is if array is found to be empty, how can I trap it ??
Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
No, the name of the .dll does not matter at all. You can call it Rushi.dll if you please. The project name and class name are the only things that matter.

Now, once you register the .dll on the machine where you intend to use it, then you cannot change it after that point, but up until then, it doesn't matter.

=====

Typically, you can handle errors in one of two ways in a component. First, you can raise a custom error:

err.raise errorNumber, source, message

This will raise an error and stop execution of the ASP page, much like the ones we always see -- except that you can customize what it says so that you can see exactly what happened. You can even return variable values, if you wish, for debugging purposes. Whatever you see fit.

Or, you can return an error code to the caller in the form of a normal return value, so that execution of your page does not stop, but you (as the developer) receive some message from the component that something bad has happened. For instance, you may want to set the return value to nothing if an error occurs (divide by 0 in this case):

ASP
dim theAnswer
theAnswer = myObject.divide(9,0)
if isNothing(theAnswer) then
'some error has occured
else
'no error occured
end if



so that your component function may have looked like this:

function divide(byref numerator as variant, byref denominator as variant) as long
dim output as long

on error resume next

output = numerator / denominator
if err.number <> 0 then
set output = nothing
err.clear
end if

on error goto 0

divide = output
end function

hope that helps! :)
paul
penny1.gif
penny1.gif
 
Dear Link9,
I pasted the DLL code here.
Can you please add lines for tracking if no data found.
So in my ASP page I can track it and show message
&quot;No data found&quot; Please !!! Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
You're a cheeky b*****r RushiShroff. Paul has, as always, taken time to give you a very good and detailed answer, now apply it yourself!

Considering on another thread (233-253353) you tell codestorm that his/her answer is &quot;not satisfactory&quot;, I would think most experts would be reluctant to help you in future.
 
Sorry for asking again but Here on the main server they dont allow us to do R & D.Mainly when it is related with Registery of windows.

Anyways I have done it on a first Go ! Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
Probably the first line of the asp you have to check for Ubound(Array) = 0 then Response.write &quot;No data&quot;
 
But it creates problem right at

vRecordArray = oClass.MethodName(strDbConnectionString, strSQL)

If array is found to be empty.
No chance to trap it.

Also in dll

if oRs.EOF then
MethodName = IsNothing
end if

It says IsNothing not supported in ASP page.
Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
Try to assign some values that will never exist in your DB to the methodname

methodname = (0,0,0,0,0)

oRs.Open oCmd
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

'~~~~~~~Assign RecordSet to Variant Array ~~~~~~~
MethodName = oRs.GetRows

And then check in your ASP for this situation
 
Also i want to ask if once i register one dll to the registry.
Then if I make changes to class module and recompile the dll then what should i do ?

option 1 :
should I give it a same name and register again(rather overwrite)

option 2 :
should I give it a different name and register it ?
Should I unregister the previous dll ?

What is the best practice because playing with registry is not allowed again and again .. Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
If you the code inside a function, or you add a new function you just have to compile it with the same name but use binary compatibility.

If you change signature (add more parameter or returning value) then you can compile it under the same name but you have to use no binary compatibility.

You have to be carefull. If you use no binary compatibility
then if you use other dll that are calling your dll that they will not work anymore, so you have to compile them again
 
from where I can get more information about Binary Compatibility and COM fundamentals ??

Any Web site or specific book ? Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
I don't know now. I got this info from books.
I will send some info to you this evening when I get home after the Toronto Raptors game :)
 
if you dont mind can i have your electronic address ?? Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
How about this ?

Code:
Project Name : How2Project3
Class Name  : ClassName


Public Function MethodName(ByVal strDbConnectionString As String, _
                           ByVal strSQL As String, byRef vntErrCode as
variant) As Variant
on Error goto MethodName_Errh:

    '~~~~~~~~~~~~~ Set ADO objects ~~~~~~~~~~~~~~~~~~
    Dim oRs As New ADODB.Recordset
    Dim oCmd As New ADODB.Command
    Dim oConn As New ADODB.Connection
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    '~~~~~~ Open Database with method argument ~~~~~~
    oConn.Open strDbConnectionString
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    '~~~~~~~~~~ Assign values to ADO objects ~~~~~~~~~
    oCmd.CommandText = strSQL
    oCmd.CommandType = adCmdText
    Set oCmd.ActiveConnection = oConn
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    '~~~~~~~~~~~~~ Open the RecordSet ~~~~~~~~~~~~~~~
    oRs.Open oCmd
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	'*********************************************************
	if (oRs.eof and oRs.bof) then
		vntErrCode=-1
		MethodName = nothing
	else

    		'~~~~~~~Assign RecordSet to Variant Array ~~~~~~~~~~~~~~~~~~
    		MethodName = oRs.GetRows
		'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    		'~~~~~Close RecordSet/Connection ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    		oRs.Close
    		oConn.Close
    		'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


	end if		
	'*********************************************************
    '~~~~~~~~~~~~~ Set objects to nothing ~~~~~~~~~~~
    Set oRs = Nothing
    Set oCmd = Nothing
    Set oConn = Nothing
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
exit function
MethodName_Errh:
	vntErrCode  = -2
    '~~~~~~~~~~~~~ Set objects to nothing ~~~~~~~~~~~
    Set oRs = Nothing
    Set oCmd = Nothing
    Set oConn = Nothing
	set MethodName = nothing
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	LogError err.Number, err.description, &quot;Method NAme&quot;
End Function

in ASP page

Code:
<%
    '-----~~~~~- Record Field Constants ~~~~~~~~~~~~~
    Const EXAMPLE_FIELD_ID = 0     '<- ExampleFieldID
    Const EXAMPLE_FIELD_1  = 1    '<- ExampleField1
    Const EXAMPLE_FIELD_2  = 2    '<- ExampleField2
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'************************
Dim strErrorString, vntErrorCode
	strErrorString =&quot;&quot;
    '~~~~~~~~~~~~~~~~~~ Set Object  ~~~~~~~~~~~~~~~~~
    Set oClass = Server.CreateObject(&quot;How2Project3.ClassName&quot;)
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    '~~~~~~~~~~~~ Set method patameters ~~~~~~~~~~~~~
    strDbConnectionString = &quot;strDSN&quot;
    strSQL = &quot;Select ExampleFieldID, ExampleField1, ExampleField2 FROM
ExampleTable&quot;
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    '~~~~~~~~~~ Get RecordSet Variant Array ~~~~~~~~~
    vRecordArray = oClass.MethodName(strDbConnectionString,
strSQL,vntErroCode)
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'********************************************************
'~~~~~~~~Check the status of the output~~~~~~~~~~~~~~~
	if (vRecordArray is nothing) then
	
		select case vnterrCode 
			case -1 
				strErrorString = &quot; No records available to
display.&quot;
			case -2
				strErrorString = &quot; Requested operation could
not be performed. Please try again, If problem persists then contact
administartor.&quot;
		end select
	end if
    '~~~~~~~~~~~~~ Set Object to Nothing ~~~~~~~~~~~~
    Set oClass = Nothing
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%>


<HTML><HEAD><TITLE>How2 EXAMPLE</TITLE></HEAD><BODY>

Number of records returned is <%=UBound(vRecordArray, 2) + 1%>


<TABLE BORDER=&quot;1&quot; CELLSPACING=&quot;3&quot;>
<% if len(strErrorString) < 2 then
	lngUBound = UBound(vRecordArray, 2) %>
	For lngIndex = 0 to lngUBound %>
	    <TR>
	    <TD><%=vRecordArray(EXAMPLE_FIELD_ID, lngIndex)%></TD>
	    <TD><%=vRecordArray(EXAMPLE_FIELD_1, lngIndex)%></TD>
	    <TD><%=vRecordArray(EXAMPLE_FIELD_2, lngIndex)%></TD>
	    </TR>
	<%Next
     Else 

	Response.write(&quot;<TR><TD><font color=red>&quot; & strErrorString &
&quot;</font></TD></TR>&quot;)	

%>
</TABLE>

But it gives function or sub not defined error.
What to do ? Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
On what line?

From before, a simple test for an array is IsArray(<variable>) - returns true/false and from memory works in VB ASP and VB. codestorm
Fire bad. Tree pretty. - Buffy
Hey, I'm operating on a limited mental budget here.
<insert witticism here>
 
Hi codestorm I am stuck here.Can you help me ?

While compiling it gives me anerror on
LogError err.Number, err.description, &quot;Method NAme&quot;


in class module.
If somebody solves this prblem of mine ? How to trap it in a standard anner in ASP ? Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
Debug the DLL and see what the Err.Desc and let us know
Maybe you should display the logerror function also.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top