My below code sample is working fine, but I would like to add my client credentials into the web.config file (i.e, Inside the connection string).
I tried but had no luck. Can anyone please help?
protected void Page_Load(object sender, EventArgs e)
{
// Organisation service URL
var organizationUri = new Uri(ConfigurationManager.ConnectionStrings["CrmConnectionStr"].ConnectionString);
//Client credentials
var credentials = new ClientCredentials();
credentials.UserName.UserName = #"domain\username";
credentials.UserName.Password = "password";
// Use the Microsoft Dynamics CRM Online connection string from the web.config file named "CrmConnectionStr".
using (OrganizationServiceProxy _service = new OrganizationServiceProxy(organizationUri, null, credentials, null))
{
Response.Write("Connected");
}
}
Web.config file
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="CrmConnectionStr" connectionString="https://test.domain.com/XRMServices/2011/Organization.svc" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
As RandomWebGuy pointed out you could be connecting to Microsoft Dynamics CRM in which case you could just change your connection string to include the username etc like this connectionString="https://test.domain.com/XRMServices/2011/Organization.svc; Username=Fred.Bloggs; Password=P#ssword;"
However, if you connecting to a webservice or want to store arbitrary values such as URIs, usernames, passwords etc then use the AppSettings section, not ConnectionString.
So, something like this in your config file:
<appSettings>
<add key="UserName" value="Fred.Bloggs" />
<add key="Password" value="P#ssword" />
<add key="ServiceUri" value="https://test.domain.com/XRMServices/2011/Organization.svc />
</appSettings>
And then in code:
var organizationUri = new Uri(ConfigurationManager.AppSettings["ServiceUri"]);
// ...
credentials.UserName.UserName = ConfigurationManager.AppSettings["UserName"];
credentials.UserName.Password = ConfigurationManager.AppSettings["Password"];
Write a custom configuration to store your specific data (and do not try to piggy back on the existing connection strings area, which has an exact format).
Here is an example:
How to include simple collections in ConfigurationSection
This is much better than having several disjointed AppSetting(s) values.
Everything germane to your values is encapsulated into a known area.
Related
Below is my connection string and I don't know where I am getting error.
<appSettings>
<!-- Default database to use... -->
<add key="Database_default" value="DB_Servername"/>
<!-- Datamart database connection to use -->
<add key="Database_datamart" value="MED_PROD"/>
<!-- Development Datamart Database Server -->
<add key="MED_PROD" value="Initial Catalog=REPORT_MED;Data Source=DB_Servername;UID=app_UID;PWD=******;"/>
<!-- Dev Server - Port Reference-->
<add key="va3fin01" value="Initial Catalog=acc;Data Source=DB_Servername;UID=med_UID;PWD=*******;"/>
<!-- MEDFIN_DEV -->
<add key="MED_DEV" value="Initial Catalog=MED_DEV;Data Source=DB_Servername;UID=med_UID;PWD=********;"/>
</appSettings>
I cannot change any back end code. They asked me to change server. After changing servers I am getting "The connectionstring property has not been initialized" on a working app. Please let me know if there is anything I could take care of only on web.config
Please help!
Thanks
Edited
Below is the method using for getting the connection string.
private void GetConnectionString()
{
string _settingname;
// if its empty, update it with "default"
if (this._database.Trim().Length == 0) this._database = "default";
// get the setting...
_settingname = ConfigurationSettings.AppSettings["Database_" + this._database];
if (_settingname == null)
{
throw new Exception("Unable to determine connection settings for specified database.");
}
else
{
// retrieve the connection string from the specified database...
this._ConnectionString = ConfigurationSettings.AppSettings[_settingname];
}
}
Normally, connection strings are inside connectionstring tag inside web.config.
For example,
<connectionStrings>
<add name="MED_PROD" connectionString="Initial Catalog=REPORT_MED;Data Source=DB_Servername;UID=app_UID;PWD=******;" providerName="System.Data.SqlClient"/>
<add name="va3fin01" connectionString="Initial Catalog=acc;Data Source=DB_Servername;UID=med_UID;PWD=*******;" providerName="System.Data.SqlClient"/>
<add name="MED_DEV" connectionString="Initial Catalog=MED_DEV;Data Source=DB_Servername;UID=med_UID;PWD=********;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<add key="DBconnection"
value="USER=harc, PASSWORD=imp, FOR_USER=ra005" />
I have this line of config in my web.config.
I am getting this value in my C# code as
string connect = System.Configuration.ConfigurationManager.AppSettings["DBconnection"];
How do I get the USER and PASSWORD values individually from this configuration?
Assuming this is a SQL Server connection string, pass it to a SqlConnectionStringBuilder:
var builder = new SqlConnectionStringBuilder(
ConfigurationManager.AppSettings["DBconnection"]
);
// use builder.UserID and builder.Password here
If it's a connection string for another database, you'll have to know the proper provider -- or store it as a <connectionString> so the provider is included.
Note that actually including a user ID and password as plain text in a configuration setting is not recommended. See Connection Strings and Configuration Files, especially the section "Encrypting Configuration File Sections Using Protected Configuration".
You can add key value pair in webconfig file
<add key="UserName" value="test" />
<add key="Password" value="test" />
and read it like System.Web.Configuration.WebConfigurationManager.AppSettings["UserName"]
We are using CRM 2015 on-premise, we are trying to build customer portal, for that we generated Early Bound class
It is successfully generated and added to VS 2012. Now the problem is when i build the project in VS it goes fine and when i run the project it throws error in the Auto generated code
The code is below
public XrmServiceContext()
{
}
Below is my web.config code
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="Microsoft.Xrm.Client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
</configSections>
<connectionStrings>
<add name="Xrm" connectionString="ServiceUri=http://Contoso/XRMServices/2011/OrganizationData.svc/; Domain=MyDomain; Username=vsaravanakumar; Password=Password#5"/>
</connectionStrings>
<Microsoft.Xrm.Client>
<contexts>
<add name="Xrm" type="Xrm.XrmServiceContext, WebAppWalkthrough"/>
</contexts>
</Microsoft.Xrm.Client>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<pages controlRenderingCompatibilityVersion="4.0" clientIDMode="AutoID">
<controls>
<add tagPrefix="crm" namespace="Microsoft.Xrm.Portal.Web.UI.WebControls" assembly="Microsoft.Xrm.Portal"/>
</controls>
</pages>
<authentication mode="None"/>
</system.web>
</configuration>
The exception im getting is "Unable to find connection string with name".
I got this error during debugging of my code
I followed each and every steps what MSDN website is mentioned in the website portal development, if i missed anything please help me to resolve this error
Below is my Web.config Code
You need to define the CRM connection string in your app.config/web.config files. If you don't specify a connection string the Client DLL defaults to using the Config file.
The CRMSvcUtil.exe is used to generate a set of classes that you can include in your project and then use to read and manipulate CRM data. But they don't "do" anything until you create a connection and then instantiate and use them. The simplified connection string method is covered here...
https://msdn.microsoft.com/en-us/library/gg695810.aspx
Essentially you put a conn string in web.config section like this...
<add connectionString="Url=http://crm.contoso.com/xrmContoso; Domain=CONTOSO; Username=jsmith; Password=passcode" name="Crm" />
Then somewhere before you use early or late bound objects, you do this...
//Use the Microsoft Dynamics CRM Online connection string from the web.config (or app.config) file named "CRM".
var connection = new CrmConnection("CRM");
var service = new OrganizationService(connection);
var context = new CrmOrganizationServiceContext(connection);
With early bound you then can do a LINQ query against the generated code objects or {entity}Sets of the context like this...
var contacts = (from c in context.ContactSet
where c.LastName == "Smith"
select c);
This will return a collection of records matching your criteria that you could enumerate through with a foreach loop, or bind to a control, or send as a jSON array, or whatever you'd like.
How to read a personal section in a web.config ?
<MyPersonalSection>
<add name="toto" enable="true" URL="http://localhost:43242" />
<add name="titi" enable="false" URL="http://localhost:98762" />
<MyPersonalSection/>
I'd like to get the enable value and/or URL value with the name value.
I also have this mistake : Unrecognized configuration section MyPersonalSection
I been trying
var config = ConfigurationManager.GetSection("MyPersonalSection");
Here is a cool example for that.
You don't need to write a custom configuration handler to get what you want. There are built-in configuration handlers that you can use if you simply want key-value entries. But, you'll have to use key instead of name and value instead of URL. For example:
<configuration>
<configSections>
<section name="MyPersonalSection" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<MyPersonalSection>
<add key="toto" value="http://localhost:43242" />
<add key="titi" value="http://localhost:98762" />
</MyPersonalSection>
</configuration>
And you can access them via code:
var myValues = ConfigurationSettings.GetConfig("MyPersonalSection") as NameValueCollection;
var url = myValues["toto"];
I would suggest naming your keys in a way that makes it clear what the value should be, like "totoUrl" and "titiUrl".
If you want something other than string value pairs, you'll have to write your own custom handler.
You can add appSettings section in your web.config with key that you will need. For example:
<configuration>
<appSettings>
<add key="FirstUrl" value="http://localhost:43242"/>
<add key="SecondUrl" value="http://localhost:98762" />
</appSettings>
...
</configuration>
So, since aspx.cs file, you can declare directive
using System.Configuration;
And later, you can retrieve FirstUrl value in this way:
var myUrl = ConfigurationManager.AppSettings["FirstUrl"];
I have a form which needs to get connected to SQL Server, and I have a drop down for selecting the list of databases and perform operations like primary key checking, etc.
But presently my connection string looks like this:
SqlConnection sConnection = new SqlConnection("Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp");
But apart from the given database, I need to take it variable, so that I can connect it to the database I select from the dropdown.
How can I do this?
Hmm you can declare your variables like this
<appSettings>
<add key="SmtpServerHost" value="********" />
<add key="SmtpServerPort" value="25" />
<add key="SmtpServerUserName" value="******" />
<add key="SmtpServerPassword" value="*****" />
</appSettings>
and read like
string smtpHost = ConfigurationManager.AppSettings["SmtpServerHost"];
int smtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpServerHost"]);
I think he wants a "semi constant":
Web.Config
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings>
<add name="YourName" providerName="System.Data.ProviderName" connectionString="Data Source={0}; Initial Catalog=myDataBase; User Id=myUsername; Password=myPassword;" />
</connectionStrings>
</configuration>
CS file
String Servername = "Test";
String ConnectionString = String.Format(ConfigurationManager.ConnectionStrings["YourName"].ConnectionString, ServerName);
you can use the connectionStrings tag in the app.config configuration. You can add as many as you want (giving them each a separate key) and then retrieve them
example app.config xml (set providerName to a valid provider, for example System.Data.SqlClient, and the appropriate connection string) :
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings>
<clear />
<add name="firstDb"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
<add name="secondDb"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
</configuration>
example on getting them and listing them (in your case, you would create the appropriate items in the dropdown and set the values) :
ConnectionStringSettingsCollection settings =
ConfigurationManager.ConnectionStrings;
if (settings != null)
{
foreach(ConnectionStringSettings cs in settings)
{
Console.WriteLine(cs.Name);
Console.WriteLine(cs.ProviderName);
Console.WriteLine(cs.ConnectionString);
}
}
You could use the AppSettings section. Read here for an example.