none
Type: ConnectionStringSetting not yet defined RRS feed

  • Question

  • One the following CONNECTIONSTRINGSETTING gets underlined when a place a mouse over it I get "Type: ConnectionStringSetting not yet defined"  could somebody help me out.

    Public Class  frmXYZ

     Dim AccessConnectionEntry As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("SecSchool.My.MySettings.SecondaryDbConnectionString")
        Dim connstr As String = AccessConnectionEntry.ConnectionString

    Private Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click

     Dim cmd As New OleDb.OleDbCommand
            Dim conn As New OleDb.OleDbConnection
            Dim dr As OleDbDataReader
            'Object To Use As SQL Query
            cmd = New OleDbCommand("Select * From StudentNOK WHERE Student_ID = ?", conn)
            cmd.Parameters.AddWithValue("Student_ID", txtEnterStuID.Text)
            dr = cmd.ExecuteReader

            If dr.Read Then
                txtSurname.Text = dr.Item("SurName")
                txtFirstName.Text = dr.Item("FirstName")

    End sub

    End Class

    Thursday, November 23, 2017 10:23 AM

Answers

  • Hi alobi,

    If you want to use ConfigurationManager, you could need to add System.Configuration.dll in reference, then you need to import it in your project.

    Now, you could need to add connectionstring in app.config, like this:

    <connectionStrings>
        <add name="ConnStr1" connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb"
          providerName="System.Data.SqlClient" />
        <add name="ConnStr2" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C# and VB Support\Example(VB)\Sample\Data1.mdf;Integrated Security=True;"/>
      </connectionStrings>
    </configuration>

    In your project, you can read some information from appsettings or connectionstrings in app.config file, like this:

    Dim connectionString = ConfigurationManager.ConnectionStrings("ConnStr2").ConnectionString
            Dim queryString = "select * from Test13;"
            Using connection As New SqlConnection(connectionString)
                Dim com As New SqlCommand(queryString, connection)
                connection.Open()
                Dim dt As New DataTable
                Using adapter As New SqlDataAdapter(com)
                    adapter.Fill(dt)
                End Using
                DataGridView1.DataSource = dt
            End Using

    More detailed info, please refer to:

    https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

    If you already have added System.Configuration.dll, but you could not use system.configuration, please remove this dll and add again.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by alobi Friday, November 24, 2017 8:12 AM
    • Unmarked as answer by alobi Friday, November 24, 2017 8:15 AM
    • Marked as answer by alobi Friday, November 24, 2017 8:19 AM
    Friday, November 24, 2017 5:57 AM
    Moderator

All replies

  • Hello,

    Try the following.

    Imports System.Configuration
    Imports System.Data.OleDb
    
    Public Class Form1
        Dim AccessConnectionEntry As New OleDbConnection(
            ConfigurationManager.ConnectionStrings(
            My.Settings.SecondaryDbConnectionString).ConnectionString)
    
    End Class

    Or

    Imports System.Configuration
    Imports System.Data.OleDb
    
    Public Class Form1
        Private someConnectionString As String = ConfigurationManager.ConnectionStrings(
            My.Settings.SecondaryDbConnectionString).ConnectionString
        Private Sub DoSomething()
            Using AccessConnectionEntry As New OleDbConnection(someConnectionString)
                '
            End Using
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Thursday, November 23, 2017 10:43 AM
    Moderator
  • If you used my example from the other thread there is an Imports statement you need to add for System.Configuration (also see Karen's example above).

    Without the Imports statement you would need to fully qualify the Class references for the System.Configuration library:

            Dim AccessConnectionEntry As System.Configuration.ConnectionStringSettings = System.Configuration.ConfigurationManager.ConnectionStrings("SecSchool.My.MySettings.SecondaryDbConnectionString")


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, November 23, 2017 2:28 PM
  • In both examples I am getting this error message:

    "configurationManager, is not declared it may be inaccessible due to its protection level"

    Thursday, November 23, 2017 2:32 PM
  • Did you add a reference to your project for System.Configuration and use the Import statement as I did?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, November 23, 2017 2:50 PM
    Moderator
  • In both examples I am getting this error message:

    "configurationManager, is not declared it may be inaccessible due to its protection level"

    Here is the link to your other thread. Please follow all of the instructions in my answer:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/976e1893-6a64-495d-bc46-af7d8d72c475/using-appconfig?forum=vbgeneral


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, November 23, 2017 3:02 PM
  • I am now getting "ConfigurationManager is not declared, It may be inaccessible due to protection level" the code as it appears on the form

    Imports System.Data.OleDb
    Imports System.Configuration
    Public Class frmStudentNOK
         Dim AccessConnectionEntry As New OleDbConnection(
           ConfigurationManager.ConnectionStrings(
           My.Settings.SecondaryDbConnectionString).ConnectionString)
    

    Here is what the App.config file looks like

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> </configSections> <connectionStrings> <add name="SecSchool.My.MySettings.SecondaryDbConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="C:\VB.Net Secondary\SecondaryDb.accdb"" providerName="System.Data.OleDb" /> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>


    Thursday, November 23, 2017 4:29 PM
  • Try the following project which has the code below.

    Imports System.Configuration
    Imports System.Data.OleDb
    
    Public Class Form1
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            Dim Builder As New OleDbConnectionStringBuilder With
                {
                    .ConnectionString = ConfigurationManager _
                        .ConnectionStrings("MyConnectionString") _
                        .ConnectionString
                }
    
            Label1.Text = Builder.ConnectionString
            Label2.Text = Builder.DataSource
            Label3.Text = $"Exists: {IO.File.Exists(Builder.DataSource)}"
            If IO.File.Exists(Builder.DataSource) Then
                Try
                    Using cn As New OleDbConnection With {.ConnectionString = Builder.ConnectionString}
                        cn.Open()
                        MessageBox.Show("Open")
                    End Using
                Catch ex As Exception
                    MessageBox.Show($"Failed to open database{Environment.NewLine}{ex.Message}")
                End Try
            Else
                MessageBox.Show("Not found")
            End If
    
        End Sub
    End Class
    

    App config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
      </startup>
      <connectionStrings>
        <add name="MyConnectionString" 
             connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dotnet_Development\CustomersDatabase.accdb"/>
      </connectionStrings>
    </configuration>
    We failed to find the database because in this case it does not exists (I set the data source to a non-existing database)

    If it was located the database would had been opened.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, November 23, 2017 5:49 PM
    Moderator
  • Hi alobi,

    If you want to use ConfigurationManager, you could need to add System.Configuration.dll in reference, then you need to import it in your project.

    Now, you could need to add connectionstring in app.config, like this:

    <connectionStrings>
        <add name="ConnStr1" connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb"
          providerName="System.Data.SqlClient" />
        <add name="ConnStr2" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C# and VB Support\Example(VB)\Sample\Data1.mdf;Integrated Security=True;"/>
      </connectionStrings>
    </configuration>

    In your project, you can read some information from appsettings or connectionstrings in app.config file, like this:

    Dim connectionString = ConfigurationManager.ConnectionStrings("ConnStr2").ConnectionString
            Dim queryString = "select * from Test13;"
            Using connection As New SqlConnection(connectionString)
                Dim com As New SqlCommand(queryString, connection)
                connection.Open()
                Dim dt As New DataTable
                Using adapter As New SqlDataAdapter(com)
                    adapter.Fill(dt)
                End Using
                DataGridView1.DataSource = dt
            End Using

    More detailed info, please refer to:

    https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

    If you already have added System.Configuration.dll, but you could not use system.configuration, please remove this dll and add again.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by alobi Friday, November 24, 2017 8:12 AM
    • Unmarked as answer by alobi Friday, November 24, 2017 8:15 AM
    • Marked as answer by alobi Friday, November 24, 2017 8:19 AM
    Friday, November 24, 2017 5:57 AM
    Moderator
  • I want to thank everybody who has provided some information. But adding reference to system configuration to the project per Cherry did the trick. thanks
    Friday, November 24, 2017 8:19 AM
  • I want to thank everybody who has provided some information. But adding reference to system configuration to the project per Cherry did the trick. thanks

    Both Karen (in this post) and I (in your other post) had mentioned that. :-)

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, November 24, 2017 1:40 PM
  • Yes you both did, what happened when I see add reference I was importing. "Import system.configuration" My bad.
    Saturday, November 25, 2017 4:53 PM