none
Error reading connection string from app.config in vsto 2003 application

    Question

  • hi,

    I am using vsto 2003.

    My application can't read connection string from app.config. I get an error message "the Connectionstring proprerty has not been initialized". But the same thing works in a windows application. Can anybody help me?

     

    Wednesday, April 09, 2008 11:33 AM

All replies

  • Hi Jaya,

     

    I wrote the following codes in my VSTO application. It worked fine on my computer. We need to manually import System.Configuration assembly and use ConfigurationManager class to get the ConnectionStrings.

    Code Snippet

            private void ThisAddIn_Startup(object sender, System.EventArgs e)

            {

                string connectionString = ConfigurationManager.

                    ConnectionStrings["WordAddIn.Properties.Settings.NorthwindConnectionString"].ToString();

                MessageBox.Show(connectionString);

                SqlConnection con = new SqlConnection(connectionString);

                try

                {

                    con.Open();

                    MessageBox.Show("Open successfully!");

                }

                catch (Exception ex)

                {

                    MessageBox.Show("Cannot open data base with the connection string");

                }

                con.Close();

            }

     

     

    My app.config looks like:

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

        <configSections>

        </< SPAN>configSections>

        <connectionStrings>

            <add name="WordAddIn.Properties.Settings.NorthwindConnectionString"

                connectionString="Data Source=JiZhou\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"

                providerName="System.Data.SqlClient" />

        </< SPAN>connectionStrings>

    </< SPAN>configuration>

    If you still cannot get it work, please feel free to unmark this thread. And could you please post the codes you are using and tell us how it does not work when you say it did not.

     

     

    Thanks,

    Ji

     

    Tuesday, April 15, 2008 5:45 AM
    Moderator
  • Hi, thank you very much for your response. I am a newbee to  vsto. This is my code.

     

    the following method works.

    protected void makeConnection()

    {

    cn = new SqlConnection();

    cn.ConnectionString="workstation id=PMXEW0035;packet size=4096;user id=sa;password =xxxxx;data source=PMSQL01;persist security info=true;initial catalog=BPT_TEST" ;

    cn.Open();

    }

     

    But when i put it in the configfile and try to access it gives me "connectionstring not initialized.

    This following code doesn't work

         

    protected void makeConnection()

    {

    cn = new SqlConnection();

    cn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

    catalog=BPT_TEST" ;

    cn.Open();

    }


     
    My config fie is

    <configuration>

    <appSettings>

    <!-- User application and configured property settings go here.-->

    <!-- Example: <add key="settingName" value="settingValue"/> -->

    <add key="ConnectionString" value="workstation id=PMISGEXEW0035;packet size=4096;user id=sa;password =xxxxxx;data source=PMISGSINSQL01;persist security info=true;initial catalog=BPT_TEST" />

    </appSettings>

    </configuration>

     

    can i you please tell me where i have gone wrong?

     

     

    Thursday, April 17, 2008 10:02 AM
  • Hi,

     

    As my previous codes indicate, we need to use ConfigurationManager, not ConfigurationSettings in your codes.

    From MSDN(http://msdn2.microsoft.com/en-us/library/system.configuration.configurationsettings.getconfig(VS.80).aspx ) , you will see:

    “The ConfigurationSettings class provides backward compatibility only. You should use the ConfigurationManager class or WebConfigurationManager class instead.”

    If you debug your program, I guess you will find that you get null string from System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]. So, cn.Open() throws the exception.

     

     

    Thanks,

    Ji

     

    Friday, April 18, 2008 7:55 AM
    Moderator
  • Follow up:

     

     

    System.Configuration.ConfigurationSettings exists in System.dll and System.Configuration. ConfigurationManager exists in System.Configuration.dll which you need to reference manually in your project.

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=297508&SiteID=1

     

     

    Thanks,

    Ji

     

    Friday, April 18, 2008 8:01 AM
    Moderator
  • Hi Ji ,

     Thank you for your response. Here is my code.

    when i write the connection string inside a method it works fine. But when i place the connection string inside the

    app.config,it gives errior "connection string not initialized".

     

     

    the following code works

    protected void makeConnection()

    {

    cn = new SqlConnection();

    cn.ConnectionString="workstation id=PMEXEW0035;packet size=4096;user id=sa;password =xxxxx;data source=PMSQL01;persist security info=true;initial catalog=BPT_TEST" ;

    cn.Open();

    }

     

    the following code doesn't work

    protected void makeConnection()

    {

    cn = new SqlConnection();

    cn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

    catalog=BPT_TEST" ;

    cn.Open();

    }

    My config file looks like this.

    My config fie is

    <configuration>

    <appSettings>

    <!-- User application and configured property settings go here.-->

    <!-- Example: <add key="settingName" value="settingValue"/> -->

    <add key="ConnectionString" value="workstation id=PMEXEW0035;packet size=4096;user id=sa;password =xxxxxx;data source=PMSQL01;persist security info=true;initial catalog=BPT_TEST" />

    </appSettings>

     

    can you please tell me where i have gone wrong?

    Friday, April 18, 2008 9:36 AM
  • Thank you for your reply.can i use configuration manager in vs 2003? It gives me error.

     

    Monday, April 21, 2008 7:12 AM