Data not saving in local database on re run c# - c#

I've created a basic windows form application using the Service-based Database option so that when I deploy it on another pc it will not require to install sql server there.
I've added a LINQ-to-SQL class in the project and here is my full code
And here is the app.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<connectionStrings>
<add name="_16Sep18_databaseAppWithSetup_.Properties.Settings.WrestlersConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Wrestlers.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
The problem is when every time I run my program the previously stored data is no longer in the database but when I input data and perform the insert,delete,update etc operations it works and the data is shown in the datagridview also but once I close the app all those data are gone.
Why is this happening and how do I fix it?

This problem occurs because the mdf file is saving in DEBUG folder also when you try to run the program...
Just go to app.config file,,
it seems like you have added the directory like
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Data\Database1.mdf;Integrated Security=True;User Instance=True
change the |DataDirectory| to full data directory address like
"AttachDbFileName=c:\Project\Data\Database1.mdf"
it will work

I resolved this problem by changing "copy always" => "copy if newer":
It worked for me. Hope this helps you.

The MDF file is copied to the debug folder on each run, and that is the file your code manipulates, not the one in your source folder.

Related

C# Connection string Source according to link in text file

I'm trying to build a program in C# (visual studio) that has a Database, but it has to go to specific folder that the user picks when running the program for the first time. The link to the folder is stored in a text file. This is all done using a windows form.
I want the app.config file to read the text file so it knows where the database is located.
My app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="MyProgram.Properties.Settings.DataBaseTestingConnectionString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\MyProgram\DataBaseTesting.accdb"
providerName="System.Data.OleDb" />
<add name="cn" connectionString="data source=.;initial catalog=NORTHWIND;user id=sa"
providerName="System.Data.OleDb" />
</connectionStrings>
</configuration>
How can i make source be first line with link in text file (location.txt) located in Desktop? instead of E:\MyProgram\DataBaseTesting.accdb
----SOLUTION-17/06/2022---------------------------------------------------------------------------------
Finally figured it out. First had to change App.Config file and change Data Source. Instead of:Data Source=E:\MyProgram\DataBaseTesting.accdb" i had to change it to: Data Source=|DataDirectory|\DataBaseTesting.accdb" Seconddly in c# file i placed this line: AppDomain.CurrentDomain.SetData("DataDirectory", Global.path); Global.path is my global variable that stored the folder location of my database.

Configuration file - encrypting connection string

I'm trying to develop a C# Winform application, which connects to SQL database.
So far I was able to move the most sensitive data from my XML configuration file to an external XML configuration file, but that's it.
The last thing I have to do is to encrypt that file, as many people will have access to a directory in which application is located.
My main [APP] configuration file looks as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings configSource="conn_string.config"/>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
And there is my [conn_string] external configuration file in which I'm trying to hide a connection string:
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
<add name="myConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=ServerName;Initial
Catalog=InitialDatabaseName;User=UserName;Password=MyPassword;Application Name=MyAppName" />
</connectionStrings>
Now when it comes to encryption I have read that asp-netregiis.exe is looking only for file named "web"
so I temporarily renamed my "conn_string" file to "web"
And tried the encryption(via developer command line VS):
aspnet_regiis -pef "connectionStrings" "path_to_my_conn_string_file"
The result is: ~My translation
The web.config file doesn't contain a configuration tag
So I added one like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="myConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=ServerName;Initial
Catalog=InitialDatabaseName;User=UserName;Password=MyPassword;Application Name=MyAppName" />
</connectionStrings>
</configuration>
Now it complains about: ~Again my translation
File format configSource must be an element conatining section name
The steps you are taking that use aspnet_Regiis are really intended for web applications hosted in Internet Information Server (IIS). The file it is looking for is really "web.config." You mentioned that the app being constructed is a winforms application, which isn't a web application. Regular winforms applications are generally configured via a file called "app.config." Visual Studio may have created a base app.config for you depending on the version you're using.
You can "trick" aspnet_Regiis into encrypting your configuration file by temporarily renaming app.config to web.config, and then invoking aspnet_regiis with a flag that points to the exact path of our "phony" web.config:
For simplicity, let's say your initial app.config resides in c:\MyPrograms\MyApp.
Rename app.config to web.config.
From an administrative command prompt, set your current directory to c:\windows\micrsoft.net\framework\v4.0.30319
Invoke aspnet_regiis, using the "-pef" switch to instruct the tool to encrypt a particular section of your web.config:
aspnet_regiis -pef "connectionStrings" c:\MyPrograms\MyApp
If you see a "Succeeded" message, rename your web.config back to app.config, and run your application. .NET should decrypt your connection string automatically at runtime on that machine.
If you need to put this application on other machines, you may need to consider setting up a common encryption key that can be installed on other machines as well as define a provider in web.config that leverages that key. But for now, let's get the basic process working locally, and then worry about the other components once we know this part is working.
Hope this helps!

Where Does Visual Studio Store the Connection String

The problem is that the table adapter keeps referencing a connection string that I have not set up for it. When I go to each data table in the DataSet Designer, the connect says "MyConnectionString(settings)". When I search for the incorrect connection string, VS can't find it.
The project that is reused over multiple solutions. I have three configurations: Debug, Staging and Release. Each configuration has it's own connection string. My app.config looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings configSource="connect.config"/>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
</configuration>
Each configuration file looks something like this:
<connectionStrings>
<clear/>
<add name="Properties.Settings.MyConnectionString" connectionString="Data Source=CorrectDataSourceforthisConfig\SQL;Initial Catalog=MyDB;Trusted_Connection=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
In my dataset, I have this XML:
<Connections>
<Connection AppSettingsObjectName="Settings" AppSettingsPropertyName="MyConnectionString" ConnectionStringObject="" IsAppSettingsProperty="true" Modifier="Assembly" Name="MyConnectionString (Settings)" ParameterPrefix="#" PropertyReference="ApplicationSettings.MyMenu.Properties.Settings.GlobalReference.Default.MyConnectionString" Provider="System.Data.SqlClient" />
</Connections>
In my settings.designer.cs, I have this:
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=CorrectDataSourceForDebug\SQL;Initial Catalog=MyDB;Integrated Security=True")]
public string RMSConnectionString {
get {
return ((string)(this["MyConnectionString"]));
}
}
Where is this rogue connection string coming from? Any help, ideas, advice and opinions would be greatly appreciated.
The connection string is stored in your app.config file as well as in your sometimes in your dataset and sometimes in your code. In my case, I was able to fix this problem by going into Explorer and deleting all the files that I had accidentally created (i.e. Form1) and by searching my solution and making sure tha there were no reference to the incorrect connection string. Then I deleted all instances of the .DLL that I had used when I included this project in different solutions and re-referenced and rebuilt all the projects.
There is also machine.config which is the master configuration file on your system. This may be where your hidden connection string is stored.
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/connection-strings-and-configuration-files
The machine.config file also contains a connectionStrings section, which contains connection strings used by Visual Studio. When retrieving connection strings by provider name from the app.config file in a Windows application, the connection strings in machine.config get loaded first, and then the entries from app.config. Adding clear immediately after the connectionStrings element removes all inherited references from the data structure in memory, so that only the connection strings defined in the local app.config file are considered.

app.config file C# winforms preserve the connection string

I have an app.config file in Winforms application that holds a connection string. This is to go out to multiple tenant (clients) as a separate file. These clients have different database sources. This config file also holds other version information such as EF, Telerik reporting etc...
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
and
<section name="Telerik.Reporting"
type="Telerik.Reporting.Configuration.ReportingConfigurationSection, Telerik.Reporting, Version=8.1.14.804, Culture=neutral, PublicKeyToken=a9d7983dfcc261be"
allowLocation="true" allowDefinition="Everywhere" />
The problem I have is when we have an updated version of EF or Telerik reporting with our application and we deploy (auto-deploy) this we need to overwrite the app.config file in the client directory to update the versions in the client config file. They then lose their connection setting and I do not want the client to have to go and re-enter it.
My question:
Is there a best practice to overcome this issue? Should I hold the connection string somewhere else?
Yep, the best thing to do is to move your connection strings section to an another config file and reference that file within your app.config.
For example create a new file called connectionStrings.config:
<connectionStrings>
<add name="Default" connectionString="[client_connection_string] "/>
</connectionStrings>
And in your app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings configSource="connectionStrings.config" />
</configuration>
A full example can be found here.
Use an external configuration file that is referenced from the application config file. E.g. include this section in your config file.
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
The external config file is described http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx
Note that storing connection settings in plaintext on a workstation is still a bad idea.
Using Windows registry for stuff like this is a definite no-no these days.
you can try to hold all connection data that you need in separate xml file so it dont get overwrite when you preform a deploy of updated version.

Connection to database not created after setup created

I had developed C# application. and used external tool for themes.
Before creating setup for this application it works fine. and no problem in database connection.
But after creating setup for this application and installing it, connection to database not created.
I have used app.config file for getting connection string. Am I missing something when creating setup or the problem due to external theme?
My app.config code
<?xml version="1.0" encoding="utf-8" ?>
<configuration> <connectionStrings>
<add name="connectionStrings" connectionString="Data Source=SUMEET-PC\SQLSERVERNEW; Initial Catalog=ClothShop;User Id=sa;Password=123;" />
</connectionStrings>
</configuration>

Categories