Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

Last post 01-16-2013 11:54 PM by rajkumar.bathula. 12 replies.
Page 1 of 1 (13 items)
Sort Posts: Previous Next
  • 06-22-2008 02:54 AM

    • sweta
    • Not Ranked
    • Joined on 06-17-2008
    • Posts 15

    Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

     Is there a way to avoid storing the connection string while creating a SqlConnection object in the code(c#) and instead retrieve it from say a configuration file????
     


  • 06-23-2008 11:55 AM In reply to

    Re: Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

    Hi:

    You can create a setting to store the information, which will place it in an app.config file in your project.

    To add the setting, go to Project/ ProjectName Properties in VSTA. Select the settings tab. If you have no current settings configured, there will be link in the center of the page you can click to create the app.config file. Now you can give your setting a name, select a type (string for a connection string), decide if the scope is user or application (for a connection string, you'll probably want application), and then type your connection string into the value area. You won't need quotes around it, having already stated that it is a string.

    Now, in your code, when you create your new SqlConnection, you can do this:

    SqlConnection sqlConn = new SqlConnection(ProjectName.Properties.Settings.Default.SQLConnectionString);

    Your app.config file will be located in the folder with your C# project.

    Hilary Stoupa

    Filed under:
  • 06-24-2008 03:52 AM In reply to

    • sweta
    • Not Ranked
    • Joined on 06-17-2008
    • Posts 15

    Re: Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

    The following is the code for my app.config file where

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

    <configuration>

    <configSections>

    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

    <section name="ICOE_WeeklyReport.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

    </sectionGroup>

    </configSections>

    <applicationSettings>

    <ICOE_WeeklyReport.Properties.Settings>

    <setting name="ICOE_WeeklyReport_localhost_Service" serializeAs="String">

    <value>http://localhost:4571/WebSite10/Service.asmx</value>

    </setting>

    </ICOE_WeeklyReport.Properties.Settings></applicationSettings>

     

    <appSettings>

    <add key="DBConnStr"

    value="server=(local);Integrated Security=SSPI;database=northwind"/></appSettings>

     

    </configuration>

     

     

    and i included the following method in my code

    private string GetDBaseConnectionString()

    {

    return ConfigurationSettings.AppSettings["DBConnStr"];

    }

    SqlConnection MyConnection = new SqlConnection(GetDBaseConnectionString());

     

    but i get an error saying The ConnectionString Property has not been initailzed and it also says

    System.Configuration.ConfigurationSettings.AppSettings is obsolete ,it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings

    can somebody help me out on this.

  • 06-24-2008 07:26 AM In reply to

    Re: Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

    Hello:

    My .config looks like the first part of yours -- with my sql connection string stored in a setting element:

    <applicationSettings>

    <ProjectName.Properties.Settings>

    <setting name="SQLConnectionString" serializeAs="String">

    <value>server=servername;database=dbname;UID=userName;PWD=password;</value>

    </setting>

    </ProjectName.Properties.Settings>

    </applicationSettings>

    And I get the settings using:

    ProjectName.Properties.Settings.Default.SQLConnectionString

    Did you add the section:

    <appSettings>

    <add key="DBConnStr"

    value="server=(local);Integrated Security=SSPI;database=northwind"/>
    </appSettings>

     

    </configuration>

    manually? I ask because it is quite a bit different from the other setting in your .config file.

    Hilary Stoupa

  • 06-24-2008 11:45 PM In reply to

    • sweta
    • Not Ranked
    • Joined on 06-17-2008
    • Posts 15

    Re: Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

    Hi

    yeah i included that manually. I even tried what you suggested and modified the code as

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

    <configuration>

    <configSections>

    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

    <section name="ProjectName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

    </sectionGroup>

    </configSections>

    <applicationSettings>

    <ProjectName.Properties.Settings>

     

    <
    setting name="ProjectName_localhost_Service" serializeAs="String">

    <value>http://localhost:4571/WebSite10/Service.asmx</value>

    </setting>

     

    <
    setting name="SQLConnectionString" serializeAs="String">

    <value>

    server=servername;Integrated

    Security=True;database=NorthWind;

    </value>

    </setting>

    </ProjectName.Properties.Settings>

    </applicationSettings>

    </configuration>

    But it is not able to recognize SQLConnectionString name in the setting when i include ProjectName.Properties.Settings.Default.SQLConnectionString in the code  and i am getting many errors.

    could you tell me if i am doing something wrong.

  • 06-25-2008 07:17 AM In reply to

    Re: Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

    Hi --

    Silly question, but I maybe wasn't clear enough earlier -- I'm using ProjectName as a placeholder for the actual name of the project. When I look at the first .config you pasted in, I see that your actual project name is "ICOE_WeeklyReport" -- is that correct? And I am guessing that is still your project name. Did you change the .config to say "ProjectName" in the places where it used to say "ICOE_WeeklyReport" ? Or did you modify the xml you pasted into your reply so as not to display your actual project name?

    Hilary Stoupa

  • 06-25-2008 08:22 AM In reply to

    • sweta
    • Not Ranked
    • Joined on 06-17-2008
    • Posts 15

    Re: Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

    Yeah i have just modified my xml . My project name is still ICOE_WeeklyReport and i dint change it to ProjectName in my code.I just wanted the code to be general .

  • 06-25-2008 08:34 AM In reply to

    Re: Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

    Hmmm.... Okay. Have you run the project in debug to see if the setting is being pulled from the .config file? What are the errors you are getting?

    Hilary Stoupa

  • 06-26-2008 03:10 AM In reply to

    • sweta
    • Not Ranked
    • Joined on 06-17-2008
    • Posts 15

    Re: Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

    I get the following error:

    ICOE_WeeklyReport.Properties.Settings does not contain a definition for 'SQLConnectionString'.

  • 06-26-2008 08:38 AM In reply to

    Re: Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

    I can replicate this error by changing the name of the setting in my .config file while still requesting the old name in my code, however, my project won't build. Please verify that the name of your setting in your xml matches the name you ask for in your code:

    <setting name="SQLConnectionString" serializeAs="String">
         <
    value>server=WINDOWSXP;database=AdventureWorks;UID=username;PWD=password;</value>
    </
    setting>

    And then in your code:

    SqlConnection sqlConn = new SqlConnection(ProjectName.Properties.Settings.Default.SQLConnectionString);

    Hilary Stoupa

  • 08-25-2008 04:46 AM In reply to

    • mbhat
    • Not Ranked
    • Joined on 08-14-2008
    • Posts 3

    Re: Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

    I have a question on the settings in the .config file. I added the settings through ProjectName.Settings and using the same (ProjectName.Properties.Settings.Default.SQLConnectionString) in the code behind. It works fine in the development server. However when I migrate the same InfoPath form to a production server, it does not work. I have copied only the app.config and the infopath form to the production server.

    I deployed the infopath form through network location. And from there I deployed it to the production server. After deploying I changed the connection string in the app.config.

    Will the ProjectName.Properties.Settings read from the app.config in case if we change the value? Do I need to add the app.config as a resource file? Is there any other way to do it?

    Any help is highly appreciated.

    Thanks

     

     

  • 12-05-2008 06:35 AM In reply to

    Re: Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

    Will storing the connection string like this in a settings file work when the form is a browser enabled form, hosted in forms services on the MOSS server? 

    Thanks,

    Jeff

  • 01-16-2013 11:54 PM In reply to

    Re: Avoid hardcoding of the Connection string in the code associated with infopath form template while making a data connection

    [[ Sorry, got the answer ]] 

    Hi Hilary

    is there a way in infopath 2010 code to access the form's hosted webapplication's web.config file elements? because there is not connectionManager class in .net 2.0 (where Infopath is considered to be). Is there any other workaround to access the config entries like connection string or any other. I dont want to keep those in separate config file like you stated in the solution above.

    thank u.

    Thank you
    Raj
Page 1 of 1 (13 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.