<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<Connectionstring>
<add key="questionpaper" value="server=localhost;database=Question_info;
UID=root;password=SATISH;"/>
</Connectionstring>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
this is my app.config but unable to connect to MySql server
so please tell me how to deal with this problem
How to write mysql connection string in app.config in c#
You need to pluralize the connectionStrings tag.
<connectionStrings>
<add name"ConnectionStringName" connectionString="Data Source=serverName;Initial Catalog=databaseName;Intergated Security=SSPI;Application Name=My.Application.Name" providerName="System.Data.SqlClient" />
</connectionStrings>
The add key/value tag is also the format to use for appSettings, not connectionStrings.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="questionpaper" connectionString="SERVER=localhost; DATABASE=Question_info; UID=root; PASSWORD=SATISH" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Related
Here is my AppConfig File
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<connectionStrings>
<add name="Connection" connectionString="(local)\SQLexpress"/>
<add name="MainPrinter" connectionString=""/>
</connectionStrings>
</configuration>
And I want to change MainPrinter's Name
Here is what i'm trying to do:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["MainPrinter"].Value = "Epson";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
But i dont take any result
Can't find what is wrong with my connection string:
I get this exception:
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized element. (D:\C#\learncsharp\Data access\AsyncSQL\AsyncSQL\bin\Debug\AsyncSQL.exe.Config line 2)
This is my code:
string connectionString = null;
string MovieDBContext = null;
try
{
MovieDBContext = ConfigurationManager.ConnectionStrings["MovieDBContext"].ConnectionString;
connectionString = ConfigurationManager.ConnectionStrings["ProgrammingInCSharpConnection"].ConnectionString;
}
catch (Exception e)
{
Console.WriteLine( e.ToString() );
}
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MovieDBContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="ProgrammingInCSharpConnection"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=ProgrammingInCSharp;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
What is wrong? And how to get details which element is wrong?
Make sure you have defined the section in the <configSections> element.Change your config as follows,
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections />
<connectionStrings>
<add name="MovieDBContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
<add name="ProgrammingInCSharpConnection"
providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=ProgrammingInCSharp;"
/>
</connectionStrings>
</configuration>
I have my config file with below configurations:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
</configSections>
<dataConfiguration defaultDatabase="myConnectionString" />
<connectionStrings>
<add name="myConnectionString" connectionString="Data Source=mydatasource;Max Pool Size=100;Pooling=true; Initial Catalog=MyDB;User ID=Myuser;Password=Password;Connection Timeout=60" providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
</configuration>
As you can see, I have set the default dataconfiguration to use myConnectionString as the default connection string, but in code behind (c#) how can I access this connection string without having to provide the name i.e. myConnectionString
So in below code:
string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
SqlConnection cnn = new SqlConnection(connectionString);
SqlBulkCopy sbc = new SqlBulkCopy(cnn);
I would like to skip hardcoding the name of the connection string.
var dataConfig = (Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings)System.Configuration.ConfigurationManager.GetSection(
"dataConfiguration");
string connectionString = ConfigurationManager.ConnectionStrings[dataConfig.DefaultDatabase].ConnectionString;
That's what I can tell from typical usage of custom config sections and DatabaseSettings class.
EDIT: I have tested it in LINQPad with your exact app.config, got result:
Data Source=mydatasource;Max Pool Size=100;Pooling=true; Initial Catalog=MyDB;User ID=Myuser;Password=Password;Connection Timeout=60
I'm trying to use input from a windows form app to enter into a localdb, however when I send the data to the database it returns "the config system failed to initialize".
try
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["database"].ConnectionString);
SqlCommand cmd = conn.CreateCommand();person newPerson = new person(FirstNameBox.Text, phoneBox.Text, emailBox.Text, LastNameBox.Text);
cmd.CommandText = #"INSERT INTO Person (FirstName, LastName, Email,Phone)
VALUES(#FirstName, #LastName, #Email, #Phone)";
cmd.Parameters.AddWithValue("#FirstName", newPerson.getFirstName());
cmd.Parameters.AddWithValue("#LastName", newPerson.getLastName());
cmd.Parameters.AddWithValue("#Email", newPerson.getEmail());
cmd.Parameters.AddWithValue("#Phone", newPerson.getPhone());
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
if (!ValidateForm())
return;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
and this is the app.config part
<?xml version="1.0" encoding="utf-8" ?>
<configSections>
<connectionStrings>
<add name="database"
connectionString=" Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Matthew\Documents\Visual Studio 2012\Projects\Midterm\Midterm\Database1.mdf;Integrated Security=True"></add>
</connectionStrings>
</configSections>
<connectionStrings>
<add name="Midterm.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
The most obvious issue I'm seeing here is that your app.config is specifying a <connectionString> section, where it should be <connectionStrings> (note the s).
You may want to provide your entire app.config as there may be other syntax/configuration issues that prevent ConfigurationManager from loading your app.config.
Edit for updated app.config:
Your app.config file is incorrectly formatted. <configSections> shouldn't be the root element of the file (it should be <configuration>).
The MSDN documentation for ConfigurationManager.ConnectionStrings shows an example of what a app.config file should be formatted like.
But, in short, the <connectionStrings> element (and the other elements such as <startup>) should be a child element of <configuration>.
What your app.config should be like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
*** ADD YOUR <add> LINES HERE ***
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
I receive the following error from a Windows Service application
System.Net.WebException = {"An exception occurred during a WebClient request."}
-2146233079
InnerException = {"Configuration system failed to initialize"}
The same code on a Console application works fine.
The Service file has not setup Security on "Local Service" (in my test environment)
Could you point me out what could cause the error and how to solve it?
Try
Using client As New Net.WebClient
Dim reqparm As New Specialized.NameValueCollection
reqparm.Add("DeviceId", deviceId)
reqparm.Add("StatusCode", statusCode)
reqparm.Add("Type", "printer")
reqparm.Add("Message", msg)
Dim responsebytes = client.UploadValues("http://xxx.xxx.com/api/notification", "POST", reqparm)
Dim responsebody = (New Text.UTF8Encoding).GetString(responsebytes)
End Using
Catch ex As Exception
'do nothing
End Try
my App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<configSections>
<section name="app" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<app>
<add key="version" value="0.0.1"/>
</app>
</configuration>
The problem was in the App.config file. This the correct version, only one instance of <configSections> and should be the first element after <configuration>.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="app" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<app>
<add key="version" value="0.0.1"/>
</app>
</configuration>