Results 1 to 3 of 3

Thread: Connection string available thru class for multiple user controls

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2003
    Posts
    93

    Connection string available thru class for multiple user controls

    I'm having a problem with my connection string remaining open on the load of a main form that contains user controls. I can test through and see that my connection string is correct & opened from my main module but when the user control hits InitializeComponent my connection string closes. I was told that I should create a class that contains a shared property that holds the connection string:

    Public Class CApplication

    Private Shared m_sConnectString As String

    Public Shared Property Connectstring() As String
    Get
    Return m_sConnectString
    End Get
    Set(ByVal Value As String)
    m_sConnectString = Value
    End Set
    End Property

    End Class

    and then access the property directly in the class:

    Dim cnnConnect as SqlConnection
    cnnConnect.ConnectionString = CApplication.ConnectString
    cnnConnect.Open

    I'm totally confused with what I'm doing. Where do I access the property directly in the class? Does this go in the form load of the user control? How does this work since the connection was originally made in a config file?

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    Ah yes, I had the same problem initially. I created a class called DbConn and added it to my project.

    In DbConn, I have declared

    Public Shared MyConn as string = "MyConnection String"

    and a function called:

    Public Shared Function CurrCon()
    Return MyConn
    End Function

    In this way, in my main code I can create a new connection and use the string returned from the DbConn.CurrCon, to supply the connection information

    as in dim cn1 as new sqlconnection(DbConn.CurrCon)

    Hope that helps
    Last edited by RichardAtherton; Oct 27th, 2003 at 03:50 PM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2003
    Posts
    93
    I'm sorry but it's still not sinking in.

    When I try to reference the class name it says that "Name CApplication is not declared.

    You posted "Ah yes, I had the same problem initially. I created a class called DbConn and added it to my project" I created a class in my prohect. Do I have to add it also?............ I'm way too confused on this one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width