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.
Related
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.
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.
In my project (I am using azure storage) I have some data that I want to translate. I have the resource system in place for translations. I have a table in cloud which has name property. I want to translate it somehow.
One option is to create all the entries in database for each language which I don't prefer as it would create a lot entries along with the name.
Is there a smart way to use the resx mechanism I have in place?
So the table has multiple properties and one is name. Name could be anything like Mud, rock etc. Now I want to translate Mud into different language. Something like Texts.Mud would return me the correct value.
But lets say I get data like this
var data = some query;
string translatedName = Texts.data[0].name; // this won't work
You should instead add more columns in the database, each for a different language and select the column based on the user language.
Other solution is to have a transaltion mechanism (a custom class for example), where you pass the original database result (say data[0].name) to a query and it returns the translated value for you.
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.
I've got a C#/ASP.net application. When a user searches for data using a few standard dropdowns and text boxes, I run a SQL query to grab all of the users search preferences and then auto-fill the controls based on what's returned. The user can then search using those presets, or change any of the choices and search again.
The problem is, this requires a call to the DB every time that search page is loaded. I was hoping there might be a way to grab all the preferences once when the user logs in the first time and then store them somehow, to lighten the load on my SQL Server db. Has anyone ever come across this issue and discovered an efficient way to handle it?
What about using the old goodies - Cookies?
HttpCookie aCookie = new HttpCookie("SearchPreferences");
aCookie.Values = /* your collection of preferences */;
aCookie.Expires = /* DateTime of expiration */;
Response.Cookies.Add(aCookie);
... or sessions you would destroy when an user logs out?
if (Session["SearchPreferences"] != null) {
/* loading the preferences */
} else {
/* preferences are already loaded */
}
You can also set their expiration using Session.TimeOut.
Edit:
As it may sound from the discussion below, both of these methods have their pros and cons. For that I've thought that adding a few more should give your opportunity to choose what suits you best.
System.Web.Caching.Cache seems like the most modern and fastest way of doing this.
Cache c = System.Web.Caching.Cache();
c.Add(key, value, dependencies, absoluteExpiration,
slidingExpiration, priority, onRemoveCallback);
However, there's still an option of ApplicationState, which is an object that can hold your values while the application is running.
If you're looking for client-side persistence, Cookies and Sessions (which still depend on cookies on the client, unless cookieless) is one option (see previous answer). You can also look into newer client side persistence options (where Web Storage has most browser support). Hth...