Results 1 to 6 of 6

Thread: How to set a global connection string for all crystal reports in my project

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2020
    Posts
    17

    How to set a global connection string for all crystal reports in my project

    I have a VB.Net project with SQL Server DB. I want to add few Crystal reports to it. I'm using a connection string inside a module which i use every where in my project and its very useful since i can change the login details at a single point.

    Code:
    connString As String = "Data Source=192.168.1.11,1433;Network Library=DBMSSOCN;Initial Catalog=MY_DB;User Id=sa;Password = abc@1234.;" 'global connection string
    Same way, is it possible to have a common global connection string somewhere embedded inside (Ex. module) my project (Not a DSN) or is possible to use the above conn string.

    If it's possible, what is the correct method of doing it and i would be grateful, if someone can post a sample code.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to set a global connection string for all crystal reports in my project

    I don't use CR but I would expect that it would support a connection string stored in the config file, which is where you should generally store it so you can change the connection details even after compiling.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: How to set a global connection string for all crystal reports in my project

    Personally I use a class that I put any needed config file connection and reference them.
    I don't know if this a common practice but It sums the connections into a Class that can be easily referenced.
    Something like this:
    Code:
    #Region "connectionstrings"
        Public NotInheritable Class GlobalConnections
            Private Sub New()
            End Sub
            Shared Sub New()
                Try
                    strconVrExternal = ConfigurationManager.ConnectionStrings("DBConnectionString").ToString()
    
                Catch ex As Exception
                    ' This should be handled  if we are not getting a db connection and we see that the connection is set and called correctly.
                End Try
            End Sub
    
            Public Shared Property strconVrExternal() As String
                Get
                    Return m_strconVrExternal
                End Get
                Set(value As String)
                    m_strconVrExternal = value
                End Set
            End Property
            Private Shared m_strconVrExternal As String
    ' For manually force a connection outside of a connection string
            Public Shared Sub Setstrcon(newString As String)
                strconVrExternal = newString
            End Sub
    
        End Class
    #End Region
    on app.config:
    Code:
    <connectionStrings>   
        <add name="DBConnectionString" connectionString="Data Source=server;Initial Catalog=VW;User Id=sa; Password=password;" providerName="System.Data.SqlClient" />
      </connectionStrings>
    you call it like:
    Dim strConnString As String = GlobalConnections.strconVrExternal
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to set a global connection string for all crystal reports in my project

    Quote Originally Posted by sapator View Post
    Personally I use a class that I put any needed config file connection and reference them.
    I don't know if this a common practice but It sums the connections into a Class that can be easily referenced.
    Something like this:
    Code:
    #Region "connectionstrings"
        Public NotInheritable Class GlobalConnections
            Private Sub New()
            End Sub
            Shared Sub New()
                Try
                    strconVrExternal = ConfigurationManager.ConnectionStrings("DBConnectionString").ToString()
    
                Catch ex As Exception
                    ' This should be handled  if we are not getting a db connection and we see that the connection is set and called correctly.
                End Try
            End Sub
    
            Public Shared Property strconVrExternal() As String
                Get
                    Return m_strconVrExternal
                End Get
                Set(value As String)
                    m_strconVrExternal = value
                End Set
            End Property
            Private Shared m_strconVrExternal As String
    ' For manually force a connection outside of a connection string
            Public Shared Sub Setstrcon(newString As String)
                strconVrExternal = newString
            End Sub
    
        End Class
    #End Region
    on app.config:
    Code:
    <connectionStrings>   
        <add name="DBConnectionString" connectionString="Data Source=server;Initial Catalog=VW;User Id=sa; Password=password;" providerName="System.Data.SqlClient" />
      </connectionStrings>
    you call it like:
    Dim strConnString As String = GlobalConnections.strconVrExternal
    I don't really see the point in the class. Simply add the connection string on the Settings page of the project properties. It will be added to the config file automatically and you can access it via My.Settings in code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: How to set a global connection string for all crystal reports in my project

    No problem.
    As I've said I don't know if this a common practice .
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: How to set a global connection string for all crystal reports in my project

    Quote Originally Posted by dilhanmail View Post
    I have a VB.Net project with SQL Server DB. I want to add few Crystal reports to it. I'm using a connection string inside a module which i use every where in my project and its very useful since i can change the login details at a single point.

    Code:
    connString As String = "Data Source=192.168.1.11,1433;Network Library=DBMSSOCN;Initial Catalog=MY_DB;User Id=sa;Password = abc@1234.;" 'global connection string
    Same way, is it possible to have a common global connection string somewhere embedded inside (Ex. module) my project (Not a DSN) or is possible to use the above conn string.

    If it's possible, what is the correct method of doing it and i would be grateful, if someone can post a sample code.
    Many of our older reports have the connection built right into the report. You lose the flexibility of an ini doing that. The newer ones we use ODBC controlled in a config file. You mentioned you didn't want to go that route though.
    Please remember next time...elections matter!

Tags for this Thread

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