Saving Data On C# Wpf - c#

How can I save my added buttons when closing the program ? Each time I hit an add button I'm creating a new title which type is button.And each title have it's own specific words.So I must save the words in buttons at the same time.

Find out what data needs to be stored. > Define a Model
Define how the Data will be stored > e.g. XML-Serialisation, JSON or whatever
Define where Data will be stored, e.g. Database, File
Define when the Configuration is read and saved
eg: read on startup, save on shutdown
Handle your Configuration Data to generate your buttons
Its all up to you. :)
An easy solution might be using XmlSerializer, which is documented here.

Related

Set a permanent value of a variable without using a database

I don't know how to describe it thoroughly in the title, but I need to set a permanent value of a variable/flag once a process has return true and maybe set some flag in the program itself the value rather than saving it to database. And once that variable/flag has already have that value then the program won't run the process again and just use the value. Is it possible? I'm using VB.Net. I can't use the database because database can be overridden and change values by using query. Thanks in advance!
You can simply use binary/XML serialization in a file to save the state of that variable through your program. Every time you restart your app you can access the value from that file to get its current state.
You can look at this example - http://www.centerspace.net/examples/nmath/csharp/core/binary-serialization-example.php
Basically, you will not save the value in the database but in a file. Anyways you need to persist the value somewhere.
Some ways below
You did not specify if you are afraid that your application or another one could change the value
How I would do it
My ideas below
1)You could use an xml file for example and zip a copy of it with a strong password. Every time you update the first xml you will update also the encrypted zipped xml.You can use a FileSystemWatcher and capture any file change, so if something/someone has changed the file you just get a new copy from the zip
2)You can store the value in the DB and add a trigger to prevent delete/update
for example
-- delete trigger
CREATE TRIGGER Function_Value_Deleted
ON [dbo].[FunctionsValueTb]
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (
SELECT [Flag] FROM deleted
)
BEGIN
ROLLBACK;
RAISERROR ('Record deletion is not allowed...', 16, 1);
END
END
*You can use also use THROW rather than RAISERROR
**Do the same for the insert and update actions
***You can also store the value into a log table or send an email
I found myself in a situation quite similar to yours a couple of days ago.
In the end, I decided to use the settings functionaly provided by .NET: it is easy to use and maintain, and so far it has given me good results.
Yo can see here what I am talking about:
Best practice to save application settings in a Windows Forms Application
That thread refers to C# but is easily applicable for VB.NET: I just had to follow the same steps in order to add the Settings file:
Right click on the project in Solution Explorer, choose Properties.
Select the Settings tab, click on the hyperlink if settings doesn't
exist. Use the Settings tab to create application settings. Visual
Studio creates the files Settings.settings and
Settings.Designer.settings that contain the singleton class Settings
inherited from ApplicationSettingsBase
And then, from my code, I use the settings like this:
Dim lastExecDate As Date = My.Settings.LastSuccessfulExecution
lastExecDate = lastExecDate.AddDays(1)
// Perform my next execution and do other stuff
My.Settings.LastSuccessfulExecution = lastExecDate
My.Settings.Save()
Next time you retrieve the parameter LastSuccessfulExecution, it will have the updated value.
One more remark, as stated in the post that I linked above:
Note that you need to set the scope property of your settings. If you
select Application scope then Settings.Default.< your property > will
be read-only
Finally, I see that you are using this to store the expiration date of a product, so you don't want the user messing around with it. According to this post, the actual values of the parameters are stored in an Application Data user folder. It is somehow obfuscated since it is not that easy to find and besides it contains a hash on its name... I don't know if that is well hidden enough for you.
If you want the value only to exist in memory when the application is running then you can use the main thread of the application and use:
int slotData = randomGenerator.Next(1, 200);
//to set the data
Thread.SetData(Thread.GetNamedDataSlot("SomeDataKey"), slotData);
//to get the data
int newSlotData = (int)Thread.GetData(Thread.GetNamedDataSlot("SomeDataKey"));
Or you can use the Windows Registry if your app only runs on Windows, if not then you would have to write the value/object to a file and read it from there.

Multiple values for a key in App.config

I want to write a key and multiple values in app.config, so that depending on the value a method is run.
for example:
let the key be "syncMode" and values to be kept are "syncAll"/"syncYest"
If the value is set as syncAll, complete data sync should happen in the main program and if syncYest is set, then only yesterday's data should be sync.
How can i write this in App.config?
Based on your requirement this looks like a simple case of single name and single value.If you want "Complete data sync in the main program" set value of key syncMode = "syncAll".Otherwise if you want "yesterdays data to sync" use syncMode = "syncYest"
You can create a custom configuration section for this. There is already a SO Post related to this. Additionally, you can also read the official MSDN page.

Saving contents of a listBox to program settings C#

So what I am trying to do is save the contents from a listBox to the application Properties.Settings.Default
I have no idea where to start, nor do I know if it is even possible. Thanks in advance.
any settings you have setup in Properties-> settings tab should show up like
[your namespace].Properties.Settings.Default.yoursetting = "change";
after you edit your properties always call
[your namespace].Properties.Settings.Default.Save();
the save part got me at first.
for list types use :
it will help if your objects can be converted to and from strings anything more not really sure for anything more complex but I hope this gets you started.
foreach(string s in listbox.Items){[settingscode].add(s);}
something like that anyways.

Storing data on Windows phone

I'm working on an app that is going to store some favourites information that the user has added. I want to store two pieces of information for each favourite - a number and a piece of text. So it will be something like this:
123 Some text
1234 Some more text
1233 More text
And so on.
The number will be unique so it should be fine to use as a key and I will need to have this number stored separately anyway in order to use it to query some data.
What is the best way to store this data on Windows Phone? I've been looking at IsolatedStorage and specifically, ApplicationSettings however I think it only stores one piece of information at a time? At least when I added some favourites information, the original value got overwritten by the new value.
Do I need to use some sort of database to store this information in IsolatedStorage? I can't imagine the amount of data will be huge. I would expect the users may only add a handful of favourites at most.
What's the best way to go about storing some data that takes the form of a key and a value on Windows Phone? Once the user has added their favourites information, it will need to be stored and loaded automatically when the app is loaded.
A very simple solution is to use the IsolatedStorageSettings. The settings is a dictionary of values. You access settings like such
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
bool useLocation;
if (settings.TryGetValue("UseLocation", out useLocation) == false)
{
// provide a default value if the key does not exist
useLocation = true;
}
You then can save settings like such
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["UseLocation"] = value;
settings.Save();
Even better is to create a nice Settings class to take care of all of that for you. Here is a nice blog post detailing how to do that.

Loading a Null String and Silverlight 4

I maintain a Silverlight 4 application. While I was out of the office, the database structure was changed and a table was dropped and its fields combined into another existing table. Now, I’m receiving the following error after I create a new item and proceed to its "summary" screen:
“Value cannot be null. Parameter name: Text
At System.Windows.Controls.TextBox.set_Text(String value)”
This only happens with newly created entries, not older entries where the information on the next screen is complete (data was converted from an Excel spreadsheet and loaded into the database). So, I’ve narrowed it down this: the child window that is used to create a new record doesn’t have all the fields that were added to the table because some of the information isn’t available when the record is created. A Google search turned up that null strings can’t be passed in Silverlight.
The Summary screen is loaded via ddsSummaryLoadedData domain service. If I don’t include the “new” fields, then the values aren’t loaded for existing entries, but new entries don’t cause an error. If I do include them, older entries load correctly but new ones give the above error.
Is there a workaround to create the empty fields until they’re needed, but still load data if it exists (for older entries)? Or does the child window need to be redesigned? I’m new to Silverlight and still have so much to learn!
It doesn't look like you're using Bindings to render your view otherwise null values will be handled gracefully, so if you are setting the Text property manually in code, use the cascading operator to verify you are not submitting a null value.
myTextBox.Text = myModelValue.FirstName ?? string.Empty;

Categories

Resources