i use VS2010 + Framework3.5 + Sql Compact in Project. But when I use the SQL Compact. display the following warning:
The specified store provider cannot be found in the configuration, or is not valid.
for get my data from SQLCE:
EFConn conn = new EFConn();
dataGridView1.DataSource = conn.Students.ToList();
its ok. but, for send data:
EFConn con = new EFConn();
Student objstd = new Student();
objstd.Name = "Sheli";
objstd.Family = "Makro";
con.Students....
is no method Sutdents.AddObject
And there is always the following warning:
The specified store provider cannot be found in the configuration, or is not valid.
thanx for help me...
You need to add the connection string to your application xml.
If you change the platform to x86 instead of Any CPU ?
I had this problem and it seemed to come from x64 provider.
verify your string connection
here example
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="ConsoleApplication1.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=|DataDirectory|\YourDataBase.sdf"
providerName="Microsoft.SqlServerCe.Client.3.5" />
</connectionStrings>
</configuration>
verify your provider
providerName="Microsoft.SqlServerCe.Client.3.5"
Related
So I'm using a connection string from my app.config file to connect to my database. I also tried to make it more secure, so that my SqlCOnnectionStringBuilder retrieves its info from the app.config file. To give you a better idea of what I mean here's some code:
app.config:
<connectionStrings>
<clear/>
<add name="connection"
connectionString="Data Source=SERVERIP; Initial Catalog=DB;
Port=3306; User ID=USERNAME; Password=PASSWORD;"
providerName="System.Data.SqlClient" />
</connectionStrings>
Method for the connection builder:
private void BuildConnectionString()
{
ConnectionStringSettings settings =
ConfigurationManager.ConnectionStrings["connection"];
if (null != settings)
{
MySqlConnectionStringBuilder builder =
new MySqlConnectionStringBuilder(settings.ConnectionString);
conString = builder.ConnectionString;
}
}
Somehow the user id gets replaced with the name of my computer, which is very wierd since the IP, port and the initial catalog aren't being replaced.
Oh and I followed this handy article here on MSDN.
Can someone please tell me what I'm doing wrong here?
EDIT:
Here is the full error I'm getting:
An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
Additional information: Host 'nameofpc' is not allowed to connect to this MariaDB server
It seems to me that the problem is that the MariaDB server is only accepting incoming connections from a certain set of hosts. Most likely the server is defaulted to accept only localhost connections. I think that this link might be helpful: https://mariadb.com/kb/en/mariadb/configuring-mariadb-for-remote-client-access/.
I receiving the error and in the local window I am seeing for both conSettings and connectionString value of null. I am right to say ConfigurationManager is null and I need to create a new object. Maybe I am using Access and perhaps I have missed something in App.config file. Can someone help me on how to solve this problem, please. Thanks in advance.
App.config file...
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MyDBConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=E:\...\Database1.mdb"/>
</appSettings>
</configuration>
Form.cs file...
private void btnShow_Click(object sender, EventArgs e)
{
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
string connectionString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString; // error points here
try
{
con = new OleDbConnection(connectionString);
con.Open();
cmd = new OleDbCommand("SELECT * FROM Table1", con);
objReader = cmd.ExecuteReader();
while (objReader.Read())
{
txtID.Text = ds.Tables[0].Rows[rno][0].ToString();
CBAgeGroup.Text = ds.Tables[0].Rows[rno][1].ToString();
CBGender.Text = ds.Tables[0].Rows[rno][2].ToString();
CBCrimOffen.Text = ds.Tables[0].Rows[rno][3].ToString();
if (ds.Tables[0].Rows[rno][4] != System.DBNull.Value)
{
photo_aray = (byte[])ds.Tables[0].Rows[rno][4];
MemoryStream ms = new MemoryStream(photo_aray);
pictureBox1.Image = Image.FromStream(ms);
}
txtCV.Text = ds.Tables[0].Rows[rno][5].ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
I have been advised to use App.config.
VS 2010 C#
MS Access 2003
UPDATE 1
My App.config now looks like this...
<configuration>
<ConnectionString>
<add key="MyDBConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Raj\Education\C_Sharp\Test1\Database1.mdb"/>
</ConnectionString>
I am now receiving error of..."Configuration system failed to initialize". I am looking at it now on Google.
Update 2
Tried...
<configuration>
<connectionStrings>
<add name="MyDBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=E:\...\Database1.mdb"/>
</connectionStrings>
</configuration>
Receiving error of "Object reference not set to an instance of an object" Googling again
Update 3
<configuration>
<connectionStrings>
<clear />
<add name="MyDBConnectionString"
providerName="System.Data.OleDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Source=\Database1.mdb" />
</connectionStrings>
With the update 3 I am receiving error the same error. I have included the Add reference System. Configuration and I have referenced using System.Configuration;
Conclusion
Perhaps it maybe there is a technical gitch between VS 2010 and Access 2003. I shall not use App.config this time round. I know there will be no problem with SQL Server. So I will leave it that. Thanks Damith and Clint for your time.
you need to read AppSettings key as below ,
string connectionString =
ConfigurationSettings.AppSettings["MyDBConnectionString"];
still you receive empty value, try below steps
Select the App.Config file in the solution explorer
In the property window select Copy to Output Directory to Copy
Always.
Now Build the application and try again.
to access like below you need to add connectionStrings section in app config
string connectionString =
ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString; // error points here
sample app config
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="MyDBConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=E:\...\Database1.mdb"/>
</connectionStrings>
</configuration>
Check if you have put the app.config file under your startup project or not. In my case, I just had to put the app.config file under my startup project, and problem was solved.
This happened to me in class library with a console app set to run for debugging.
It is necessary to add the connection to the app.config in the console app, as it will not find it in your library if you're starting it from an outside process.
This:
string connectionString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
is your problem.
You're accessing ConfigurationManager.ConnectionStrings to get to your configuration item, but in the App.Config file you've put it under appSettings which is a different section of the config file than ConnectionStrings
You could either put your connection string in the relevant ConnectionStrings section of the app.config (accessible with ConfigurationManager.ConnectionStrings, or access it against the appSettings section.
See:
http://msdn.microsoft.com/en-us/library/ms254494(v=vs.80).aspx
For MSDN guidelines on storing connection strings.
I too faced the same problem even after multiple scrutinizing the app.config files and my code for error, but i didn't found any errors in my code. Eventually i found a silly mistake that my compiler had by default taken the application configuration file name as 'app1.config', and when i changed it to 'app.config' every thing just worked fine for me.
Hope it will help.
I bumped into this problem when I added app.config file by Project -> Add -> New Item... -> Application Configuration File to an old, tiny C# library that had been originaly written in .NET Framework 4.5 and later updated to 4.7.2. After many attempts I decided to load config file just as a XML file. If everything else failed you may consider using this workaround.
App.settings
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyDBConnectionString"
providerName="System.Data.OleDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Source=\Database1.mdb" />
</connectionStrings>
</configuration>
Code behind
using System;
using System.IO;
using System.Reflection;
using System.Xml;
XmlDocument doc = new XmlDocument();
string path = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath) + "\\YourProjectName.exe.config";
doc.Load(path);
XmlNode node = doc.SelectSingleNode("//connectionStrings");
XmlElement value = (XmlElement)node.SelectSingleNode("//add[#name='MyDBConnectionString']");
string connectionString = value.Attributes["connectionString"].Value;
The effect should be similar to:
string connectionString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ToString();
OR
string connectionString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
Hope that someone will find this useful.
When using WPF and entity-framework I have an APP.CONFIG that looks like the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="DatabaseEntities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=%APPDATA%\Folder\Database.sdf"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
When using this code it always throws the following error:
System.Data.EntityException: The underlying provider failed on Open. ---> System.Data.SqlServerCe.SqlCeException: The path is not valid. Check the directory for the database. [ Path = %APPDATA%\Folder\Database.sdf ]
When I run the path "%APPDATA%\Folder\Database.sdf" from the command prompt it works fine, and if I remove "%APPDATA% and hardcode the path it works fine - so it looks simply like the %APPDATA% is just not being substituted for the actual folder...
Thanks,
As you already reallized, %APPDATA% or any other environtment variables are not replaced with their respective value in connection strings. Environment varialbes are something related to the operating system shell. They work in command prompt because the command prompt explicitly parses the values entered and substitutes environment variables. That's not something that .NET Framwork usually performs.
To achive this, you have to manually provide the value for %APPDATA% (using Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) or Environment.GetEnvironmentVariable("APPDATA")). There are two options:
Change your connection string and use |DataDirectory|:
<connectionStrings>
<add name="DatabaseEntities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\Database.sdf"" providerName="System.Data.EntityClient" />
</connectionStrings>
(Notice the use of |DataDirectory| in the path to the database file.)
Then provide the value for |DataDirectory| in your application's Main method:
AppDomain.CurrentDomain.SetData("DataDirectory",
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
Refer to this MSDN page for more information.
Manually provide the connection string for your ObjectContext class. This way you can parse and change the connection string:
public static string GetConnectionString()
{
var conStr = System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseEntities"].ConnectionString;
return conStr.Replace("%APPDATA%",
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
}
And later:
var db = new DatabaseEntities(GetConnectionString());
Or subclass you ObjectContext class and always use the new connection string:
public class MyDatabaseEntities : DatabaseEntities
{
public MyDatabaseEntities()
: base(GetConnectionString())
{
}
public static string GetConnectionString()
{
var conStr = System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseEntities"].ConnectionString;
return conStr.Replace("%APPDATA%",
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
}
}
and use the new class anywhere.
I have another option. We don't need to replace anything. I am using below connection string without any replace and it's working fine.
<connectionStrings>
<add name="ProjectManagementDBEntities" connectionString="metadata=res://*/Models.ProjectManagementModels.csdl|res://*/Models.ProjectManagementModels.ssdl|res://*/Models.ProjectManagementModels.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\ProjectManagementDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>
</connectionStrings>
Main change is data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\ProjectManagementDB.mdf;integrated security=True;
I hope this will save someone.
You have to replace the %APPDATA% in the code with the relative path -
var connectionString = ConfigurationManager.ConnectionStrings["DatabaseEntities"]
.ConnectionString;
connectionString.Replace("%APPDATA%",
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
I have a problem. I am trying To extract a connection string through
sqlConnStr = new qlConnection(ConfigurationManager.ConnectionStrings["PlacementConnectionString"].ConnectionString);
but it keeps failing :
Object Ref not set to an instance of an object
I check with the debugger and this is the value of the connectionStr
{Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Placement.accdb}
I have imported my database through VS2012's DataSet wizard so what am I doing wrong?
PS: I have tested the connection numerous times.
<connectionStrings>
<add name="_201103578_P09.Properties.Settings.PlacementConnectionString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\Placement.accdb"
providerName="System.Data.OleDb" />
</connectionStrings>
Kind regards
Markus
[UPDATE]
I changed from
sqlAdapter = new SqlDataAdapter();
try
{
sqlConnStr = new SqlConnection(ConfigurationManager.ConnectionStrings["PlacementConnectionString"].ConnectionString);
}
to
sqlAdapter = new SqlDataAdapter();
string s = ConfigurationManager.ConnectionStrings[1].ConnectionString;
try
{
sqlConnStr = new SqlConnection(s);
}
I inspect s and the value is
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Placement.accdb
Now an error is thrown
System.ArgumentException: Keyword not supported: 'provider'
At wits' end;
=================================================================
TO EVERYONE - THANK YOU the problem was (I forgot) when using an Access database you have to use OleDbCommand and not SqlCommand. Thank you everything works fine now! – Markus just now edit
Going off the code you posted, the only explanation I can see is that the null reference you're getting is related to the configuration manager not getting anything by the string you're passing it.
if
ConfigurationManager.ConnectionStrings["PlacementConnectionString"]
doesn't return anything-- calling
.ConnectionString
will fail with your error. Can you verify that
"PlacementConnectionString"
is the correct name of the connection?
Your connectionstring name is incorrect
<connectionStrings>
<add name="_201103578_P09.Properties.Settings.PlacementConnectionString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\Placement.accdb"
providerName="System.Data.OleDb" />
Instead it should be
<connectionStrings>
<add name="PlacementConnectionString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\Placement.accdb"
providerName="System.Data.OleDb" />
Or in your code you should look for
ConfigurationManager.ConnectionStrings["_201103578_P09.Properties.Settings.PlacementConnectionString"].ConnectionString
Environment
.NET 3.5
Self Servicing
LLBL Pro 2.6
I know I might be doing something stupid here. I have following code
string connectionString = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
DbUtils.ActualConnectionString = connectionString;
PersonCollection ps = new PersonCollection();
ps.GetMulti(new PredicateExpression(Person.Lastname == "lastname" ));
Console.WriteLine(pt.Count);
Now when I generated the entities from LLBL Studio, I used a db named ForGeneratingLLBL but in app.config connection string is pointing to another db Master . My expection is that program will retrieve data from whatever is defined in DbUtils.ActualConnectionString (which in this case is defined in app.config) but for some reason its still retreiving data from ForGeneratingLLBL. Any idea what i am doing wrong here?
PS: I have posted this quetion on LLBL forum as well, trying here to see if anyone had similar issue before
If your DB is different than the one you generated the entities you need to put this in your config file:
<configuration>
<configSections>
<section name="sqlServerCatalogNameOverwrites" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
</configuration>
and this:
<sqlServerCatalogNameOverwrites>
<add key="OriginalDatabase" value="TargetDatabase" />
</sqlServerCatalogNameOverwrites>
In the documentation, under Catalog name overwriting