Solved

OO Question

Posted on 2004-08-20
8
250 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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