Click here to Skip to main content
15,890,982 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to save a variable value at button click and also need to retrieve that during application startup.I know this can be done easily by using database or using files.But i don't want to use those for saving just a variable value.Are there any way to do this?
C#
private void buttonSave_Click(object sender, RoutedEventArgs e)
{
    string birthday;
    if (autoCompleteBox1.Text == "")
    {
        MessageBox.Show("You don't give your birthday!");
    }
    else if (!validateInput(autoCompleteBox1.Text, @"^[0-9]\d{1}/[0-9]\d{1}/\d{4}$", "Birthday format isn't correct! Follow dd/mm/yyyy format.."))
        autoCompleteBox1.Focus();
    else
    {
        MessageBoxResult result= MessageBox.Show("Your Birthday Saved Successfully","Confirmation",MessageBoxButton.OK);

        birthday = autoCompleteBox1.Text;
        pickBirthday = birthday.Substring(0,5);//I need to save this value
    }
}

After clicking Save button the value of pickBirthday is saved.But when i re-run my program i need to show this saved value.But currently at that time the value is null..how can i fix this without using database or file?
Posted
Comments
StianSandberg 16-Aug-12 9:42am    
you have to store that value somewhere if you need it again after a restart/recycle. Putting it in a file/database isn't that much work..
[no name] 16-Aug-12 9:46am    
So not in a database and not in a file. Where exactly do you propose to store this value?
ridoy 16-Aug-12 9:47am    
I know database is best solution but it increases my small application's size too much..for that i am searching for something alternate..
[no name] 16-Aug-12 10:14am    
What makes you think a database is the "best" solution? Save the data to a file and be done with it. Your elimination of the easiest (and probably the best) solution does not make any sense.

What is the problem saving this information in a file?

Another option would be store somewhere in registry. (Personally, I wouldn't do that and use a xml file as a datastore).
 
Share this answer
 
Comments
Prasad_Kulkarni 17-Aug-12 0:19am    
My 5!
Manas Bhardwaj 17-Aug-12 3:11am    
thx!
I personally don't work with silverlight, but in .Net we use setting when we want to keep some data without a full database to store it. I did a google search[^] on silverlight settings and found this article[^] which looks like it might help you out. Hope it helps.
 
Share this answer
 
Comments
StianSandberg 16-Aug-12 9:50am    
5'ed
Try This:-

In solution explorer go to the Properties folder under your Project Name.
Then Double click on
C#
Settings.settings
and new tab will open
In thatyou will find four Columns (Name, Type, Scope, value).
In name Column type any text for eg. Birthday.
In second Column select type you can select string as you want to save string data.
In third column select the scope as user only because if you select application then the changed value will not be saved.
In fourth column keep empty. and save file by Ctrl + S.

In save Button where you have written code to save value to variable
C#
birthday = autoCompleteBox1.Text;
pickBirthday = birthday.Substring(0,5);//I need to save this value

C#
Properties.Settings.Default.Birthday = pickbirthday //your variable
Properties.Settings.Default.Save();
wrtite the following code. This will save your value.

Then in your Form Load event write following code:-
C#
//Your Control for eg. textbox or label, etc. i am using Textbox.
TextBox1.Text = Properties.Settings.Default.Birthday;

This will load the value you saved last time.

Any query plz comment or reply.
 
Share this answer
 
Comments
ridoy 16-Aug-12 10:30am    
It seems your solution is good one.But I don't found Settings.settings in Properties folder,i found only AppManifest.xml and OutOfBrowserSettings.xml there.Do i need to create Seetings.settings or i make any error?
Ank_ush 16-Aug-12 11:24am    
You are working on windows or web project ????
ridoy 16-Aug-12 11:42am    
In Silverlight 3.0..
Ank_ush 16-Aug-12 11:46am    
No Idea about Silverlight Dear.
Ank_ush 17-Aug-12 2:09am    
Did this worked ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900