Solved

OO Question

Posted on 2004-08-20
8
255 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MailAddress in vb 4 36
Retrieve Active Directory Groups a User belongs to in VB.NET 3 41
Access Schema 6 45
VB.Net - BackgroundWorker running multiple 5 46
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 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