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
Related
Here is my problem: I have a simple C# console app and I open my connection with
SqlConnection conn = new SqlConnection("Data Source=server;" + "Initial Catalog=database;" + "User id=sa;" + "Password=pass;timeout=60;");
That works fine. I added the app.config with some keys in the AppSettings. That works fine too.
But if I add a new tag to the config, the previous code gives me exception...why?!
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="template" value="value"/>
<add key="saveas" value="value"/>
</appSettings>
<tag>
</tag>
</configuration>
You cannot just add a random tag to your config file. The config file is parsed by the runtime and the runtime has to understand it.
If you want to do something with your custom tag, it might be helpful to ask a question about that (make sure you search existing questions before you do), because there are different ways to reach what you want (for example custom configuration sections). But until then, the quickest solution to make your problem go away is to simply delete the tag tag from your configuration file.
I'm not sure how you plug your connection string into your data handler. If you are using Linq, your project should have updated with the connection settings in the app.config automatically. I update my app.config during publication.
Here is a sample of what my config looks like. perhaps it will give you what you need. You can see how I added my own "tag" called ConnectivityDatabase and SysDatabase.
Im new to C# .net programming and sorry if I ask stupid question.
I have setup my programs to load setting from database instead off app.config.
However, I want it to replace the setting from app.config if only the setting available in it.
For example, the setting that will load from database is
IP_address = 192.168.0.111
folder_path = /share
pc_name = pc_dev
username = developer
password = developer123
then in app.config I will insert this value
IP_address = 192.168.0.222
the program will then change the IP_address value that it load from database to the value that I insert in app.config
is there anyway to do this?
Thank you
Easiest way is to use your app.config's appSettings. Add a reference to System.Configuration.
Then your app config should look like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ipAddress" value="192.168.0.222"/>
</appSettings>
</configuration>
Then in your code, get the value from the config by using this:
string ipAddFromConfig = System.Configuration.ConfigurationManager.AppSettings["ipAddress"]; // get the value from the appsettings.
Then you can replace the value that you got from your DB.
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.
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"
I was hoping to be able to switch data providers to/from SQL Server and SQL Server Compact Edition via just a configuration change. But it doesnt work and looking at the EDMX file I think I can see why:
<edmx:StorageModels>
<Schema ... Provider="System.Data.SqlClient" ...
Is there any way to specify the provider in app.config or at runtime?
The Storage-Model is tied to a specific provider, which will cause the Entity Framework to reject any DbConnection implementations that are not compatible with the specified provider.
If you look at an Entity Framework connection-string, you can see that the StorageSchema, ModelSchema and Mapping are specified in three different files (which are generated from your .edmx and than embedded in the assembly). You could take your .edmx apart and embed the .ssdl, .csdl and .msl yourself and than create another .ssdl for SQL Server CE. This is basically just copy & paste and replacing the provider and some column-types.
I wrote about here: Comparison Entity Framework
For Unit Tests I change Schema this way (changing ssdl before main code execution).
In code:
var s = Assembly.GetExecutingAssembly().GetManifestResourceStream("Model1.ssdl");
var ssdlFilePath = "<some-dir>\file1.ssdl";
using (var file = File.Create(ssdlFilePath))
{
StreamUtil.Copy(s, file);
}
var str = File.ReadAllText(ssdlFilePath);
str = str.Replace("old provider token", "ProviderManifestToken=\"4.0\"");
str = str.Replace("old provider type"", "Provider=\"System.Data.SqlServerCe.4.0\"");
File.WriteAllText(ssdlFilePath, str);
In app.config:
<connectionStrings>
<add name="Database2Entities" connectionString="metadata=res://*/Model1.csdl|<some-dir>\file1.ssdl|res://*/Model1.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\Database1.sdf"" providerName="System.Data.EntityClient" />
</connectionStrings>
It works)