Help for ConfigurationManager in VB.Net 2005

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TimHop12
    New Member
    • Nov 2006
    • 24

    Help for ConfigurationManager in VB.Net 2005

    After creating the ConnectionStrin g node in Web.Config file (ASP.NET 2005) even after import System.Configur ation

    I am not able ot see the ConfigurationMa nager as a class, so that I can read the node for creating a DB connection. Any advice is appreciated. Thanks.
  • TimHop12
    New Member
    • Nov 2006
    • 24

    #2
    By mistake I have posted the Question to this section of .Net forum. Can somebody move it for me to .Net Questions Forum please? Thanks.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I've moved your thread as requested.
      You should refrain from double posting your questions.


      Are you retrieving your connection string using:
      [code=vbnet]
      Dim myConString As String = ConfigurationMa nager.Connectio nStrings("MyDat abaseConnection StringIdentifie r").ConnectionS tring
      [/code]

      Where are you attempting to access the ConfigurationMa nager (please note that in certain places it is unavailable...l ike some functions in your Global.asax file)
      Could you please provide more details about your problem.

      -Frinny

      Comment

      • TimHop12
        New Member
        • Nov 2006
        • 24

        #4
        Thanks Frinny for following it up.

        For making DB connection, I have placed my connectionStrin g in web.config as:

        Code:
        <connectionStrings>
               <add name="MyDBConnection" connectionString="server=Server1; database=Req1; uid=sa; pwd=;" />
        </connectionStrings>
        Now I have created a seperate project "DBConnect" and in the Class file DBConnect.vb I was writing this function for connecting to DB as:

        Code:
        Public Shared Function getConnectionObj() As SqlConnection
           Dim objSqlConn as SqlConnection
           Dim strConn as string
        
           strConn = ConfigurationManager.ConnectionStrings("MyDBConnection").ConnectionString
           
          objSqlConn = New SqlConnection(strConn)
                
        End Function
        But in the function code above I am NOT able to get the "ConfigurationM anager" to read the node from Web.Config file. Please Help.

        Also I have imported following libraries:
        Code:
        Imports System.Data.SqlClient
        Imports System.Configuration
        Imports System.Web

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Originally posted by TimHop12
          Now I have created a seperate project "DBConnect" and in the Class file DBConnect.vb I was writing this function for connecting to DB as:
          You are in a seperate project? Values contained in a web.config apply only to the project it's contained in.
          (Unless I'm mistaken)

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by TimHop12
            Thanks Frinny for following it up.

            For making DB connection, I have placed my connectionStrin g in web.config as:

            Code:
            <connectionStrings>
                   <add name="MyDBConnection" connectionString="server=Server1; database=Req1; uid=sa; pwd=;" />
            </connectionStrings>
            Now I have created a seperate project "DBConnect" and in the Class file DBConnect.vb I was writing this function for connecting to DB as:

            Code:
            Public Shared Function getConnectionObj() As SqlConnection
               Dim objSqlConn as SqlConnection
               Dim strConn as string
            
               strConn = ConfigurationManager.ConnectionStrings("MyDBConnection").ConnectionString
               
              objSqlConn = New SqlConnection(strConn)
                    
            End Function
            But in the function code above I am NOT able to get the "ConfigurationM anager" to read the node from Web.Config file. Please Help.

            Also I have imported following libraries:
            Code:
            Imports System.Data.SqlClient
            Imports System.Configuration
            Imports System.Web
            Wow this sound familiar.
            I did something very similar just a few months ago.
            I wrote a class library in a project that managed all of my database manipulation. It was used within an asp.net project...and for security reasons I wanted to store the connection string in the web.config file.

            What I ended up doing was passing the connection string to the class library functions from my asp.net project.

            I don't think there is a way for both applications to access the connection strings node in the a web.config file.

            (think about it ...would you want to allow any application access to this information? it could be malicious and destroy your database!)

            I'll help you research the topic but I'm pretty sure that you can't do this.

            Comment

            • TimHop12
              New Member
              • Nov 2006
              • 24

              #7
              Trying to do something like this in the DBManager Class:

              Code:
                          Dim Path As String = "C:\DBInfo.xml"
                          Dim doc As System.Xml.XmlDocument
                          Dim nodeList As System.Xml.XmlNodeList
              
                          Dim strConn As String
                          Dim objSqlConn As SqlConnection
              
                          doc = New XmlDocument()
                          doc.Load(Path)
                          node = doc.SelectSingleNode("Database/DB")
                          strConn = node.FirstChild.FirstChild.Value
              This seems fine. But again I am hardcoding the File Path here in the vb file, which I don't want. I want to store this File Path info somewhere in a Configuration. Please advice. Thanks.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                in the constructor for your dbmanager class (or just some public property) have it set the file path.
                Then you can pass it in at runtime.

                Comment

                • TimHop12
                  New Member
                  • Nov 2006
                  • 24

                  #9
                  Thanks Plater. I shall try this out.

                  Comment

                  Working...