Solved

OO Question

Posted on 2004-08-20
8
252 Views
Last Modified: 2010-04-23
 I am relatively new to the app programming in .net. That being said I was wondering if someone could help me out with a OO related question.
 
Code in question. I have deleted out the non-relevant functions.

LOG.VB
Imports System.Data.OleDb
Imports System.IO

Public Class log

    Private sql As String
    Private command As OleDbCommand
    Private conn As OleDbConnection
    Private reader As OleDbDataReader
    Private pst_connection As String
    Private job_id As Integer
    Private job_instance As Integer

   Private Sub getConnection()
        conn = New OleDb.OleDbConnection(pst_connection)
        conn.Open()
    End Sub

    Function getnext()
        getConnection()

        '--- Clean-up ---
        sql = " DELETE pst_job_log WHERE job_instance_id IN (SELECT DISTINCT (job_instance_id)  FROM pst_job_log WHERE stamp < SYSDATE - 60)"
        command = New OleDbCommand(sql, conn)
        command.ExecuteNonQuery()

        '--- Get ID ---
        sql = " SELECT job_instance_id.NEXTVAL FROM DUAL"
        command = New OleDbCommand(sql, conn)
        reader = command.ExecuteReader(CommandBehavior.CloseConnection)
        reader.Read()
        getnext = reader.GetValue(0)
        reader.Close()
    End Function

    Function setup(ByVal file_name As String, ByVal jobid As Integer)
        Dim files As New file_functions()
        pst_connection = files.read_tf(file_name)
        job_id = jobid
        job_instance = getnext()
    End Function

    Function add(ByVal message As String)
        getConnection()

        '--- Add Message ---
        message = Replace(message, "'", "''")
        sql = " INSERT INTO PST.PST_JOB_LOG ( JOB_ID, STAMP, MESSAGE,  MESSAGE_ID, JOB_INSTANCE_ID, TIME) "
        sql = sql & " VALUES (" & job_id & ",sysdate,'" & message & "' ,msg_id.nextval," & job_instance & ",to_char(sysdate, 'hh:mi:ss'))"
        command = New OleDbCommand(sql, conn)
        command.ExecuteNonQuery()

        command.Dispose()
        conn.Close()
    End Function
End Class


FILE_FUNCTIONS.VB
Imports System.IO

Public Class file_functions
..........
    Function check(ByVal filename As String, ByVal minutes As Integer)
        Dim lf As New log()
       lf.add("File check...") <----- "Connection string not initialized."
    ................................
    End Function

End Class

I have this compiled into a .dll. When I call the check function I get Connection string not initialized. I am sure I am just missing a concept here and was wondering if someone could offer an explanation.
0
Comment
Question by:cwigley
  • 3
  • 2
8 Comments
 
LVL 8

Expert Comment

by:bramsquad
ID: 11855969
it relates to this statement

pst_connection = files.read_tf(file_name)

you dont have a valid connection string thats being passed to your connection.....from the looks of it (youre omitting the read_tf() function which would tell us what your connection string looks like......but im under the assumption you are just returning a filepath.

check out www.connectionstrings.com

this should lead you in the right place to initialize your connection.


0
 

Author Comment

by:cwigley
ID: 11856046
If I instantiate the log class iteself then the connection string initializes just fine.

Testing Example:
vbscript

This works just fine.
'----------------------
const job_id=15
set lf=createobject("script_tools_v2.log")
set file=createobject("script_tools_v2.file_functions")
lf.setup "c:\scripts\pst_connection.txt", job_id
lf.add "This is a test of the .net message logging dll."

If I then add the next line I get my connection string could not be initialized. I figure this is because the values that the function setup calls are not being referenced. This next line will work if I take the call the to add function out of the code. So it is blowing on trying to call the add function from within the file_functions class.

file.check("filename.txt")
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 11859803
what is the value of the variable pst_connection at the time you get the error message?

AW
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:cwigley
ID: 11869238
It is empty. How can I pass this value from the log class to the file_functions class? I am trying to keep from having to pass in this value everytime I want to write to the db from another function.
0
 

Author Comment

by:cwigley
ID: 11869665
 Ok. I don't how good a pratice this is but I created a module and declared the pst_connection as a public variable(Global). So when I call the setup function it gets populated and persists the value through the various classes. I will still award points if someone can show me a better way to handle this.
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 125 total points
ID: 11869792
try this approach:

1) modify you log class, to have the setup function test to see if the connection has already been read - and only read it if it has not already been read:

    Function setup(ByVal file_name As String, ByVal jobid As Integer)
        Dim files As New file_functions()
        if pst_connection = "" then
              pst_connection = files.read_tf(file_name)
              job_id = jobid
              job_instance = getnext()
       end if
    End Function



then, in your File_Fiunctions class:

FILE_FUNCTIONS.VB
Imports System.IO

Public Class file_functions
..........
    Function check(ByVal filename As String, ByVal minutes As Integer)
       Dim lf As New log()
       lf.setup() ' only create a new connection if one does not already exist
       lf.add("File check...") <----- "Connection string not initialized."
    ................................
    End Function

End Class

as it was, since you create a NEW log object, it does NOT have an assocaited connection, hence the error.

AW

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now