locked
Editing/Adding/Reading Data Problems in C# RRS feed

  • Question

  • Hello, My name is Filiph Sandström. I am working on a Windows Store App in C# called Costs Keeper. Costs Keeper is basically a simple but good looking app to save transactions and other minor thinks. However this is the second App im creating and i have not had the need to save large amounts of data before, so I well... created This:

    public static class Variables
    	{
    		public static int costsInDB;
    		public static double totalSum;
    		public static string latestCosts;
    	}
    	
    	public class costsDB
    	{
            public static void cleanAll()
    		{
    			if(Variables.costsInDB == 0)
    			{
    				return;
    			}
    			
    			Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
    			Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    			
    			int temp = Variables.costsInDB;
    			
    			for(int x = 0; x <= temp; x++)
    			{
                    string temp23 = x.ToString();
    				localSettings.Containers["Costs"].Values[temp23] = 0;
    				localSettings.Containers["Sum"].Values[temp23] = 0;
                    localSettings.Containers["Date"].Values[temp23] = 0;
    			}
    		}
    		
    		public static int getCostsInDB()
    		{
    			
    			Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
    			Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    
    			/*if (localSettings.Containers.ContainsKey("Costs"))
    			{
    			   
    			}
    			else
    			{
    				localSettings.CreateContainer("Costs", Windows.Storage.ApplicationDataCreateDisposition.Always);
    				return 0;
    			}*/
    			
    			int temp = 0;
    			for(bool z = true; z == true; temp++)
    			{
                    string temp2 = Convert.ToString(temp);
    				if(localSettings.Containers["Costs"].Values.ContainsKey(temp2.ToString()))
    				{
    					z = true;
    				}
    				else
    				{
    					z = false;
    				}
    			}
    
    			return temp;
    		}
    		
    		public static void addCostsInDB(string name, double sum, string desc)
    		{
    			Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
    			Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    			
    			int temp = costsDB.getCostsInDB() + 1;
    			
    			localSettings.Containers["Costs"].Values[temp.ToString()] = name;
    			localSettings.Containers["Sum"].Values[temp.ToString()] = sum;
    			localSettings.Containers["Date"].Values[temp.ToString()] = System.DateTime.Now.Day.ToString() + "/" + System.DateTime.Now.Month.ToString() + "/" + System.DateTime.Now.Year.ToString();
    			updateVariables();
    		}
    		
    		public static double getTotalSum()
    		{
    			Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
    			Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    			
    
    			if (localSettings.Containers.ContainsKey("Sum"))
    			{
    			   
    			}
    			else
    			{
    				localSettings.CreateContainer("Sum", Windows.Storage.ApplicationDataCreateDisposition.Always);
    				return -1;
    			}
    			
    			double sum = 0;
    			
    			for(int x = 0; x <= Variables.costsInDB; x++)
    			{
                    if (localSettings.Containers["Sum"].Values[x.ToString()] == null)
                        return 0.0;
                    string temp = localSettings.Containers["Sum"].Values[x.ToString()].ToString();
    				sum = sum + Convert.ToDouble(temp);
    			}
    			
    			return sum;
    			
    		}
    		
    		public static string getLatestCost()
    		{
    			Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
    			Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    			
    			if (localSettings.Containers.ContainsKey("Date"))
    			{
    			   
    			}
    			else
    			{
    				localSettings.CreateContainer("Date", Windows.Storage.ApplicationDataCreateDisposition.Always);
    				return System.DateTime.Now.Day.ToString() + "/" + System.DateTime.Now.Month.ToString() + "/" + System.DateTime.Now.Year.ToString();
    			}
    
                string temp = "";
                string temp2 = Variables.costsInDB.ToString();
                if (localSettings.Containers["Date"].Values[temp2.ToString()] == null)
                    return temp;
    			temp = localSettings.Containers["Date"].Values[temp2.ToString()].ToString();
    			
    			return temp;
    		}
    		
    		private string getMostCg(int place)
    		{
    			Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
    			Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    
                int temp = Variables.costsInDB;
    
    			for(int x = 0; x <= temp; x++)
    			{
    				//Variables.costsInDB;
    			}
    
                return "Error";
    		}
    
            public static string getInfoFromCostsDB(int number, int type)
            {
                updateVariables();
                Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
                Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    
                string temp = "";
    
                if (number > Variables.costsInDB)
                {
                    return "Error: Too High Int";
                }
                if (localSettings.Containers["Costs"].Values[number.ToString()] == null)
                {
                    return "Error: DataBase_" + number.ToString() + " is null";
                }
    
                if (type == 1)      // Name
                {
                    temp = localSettings.Containers["Costs"].Values[number.ToString()].ToString();
                }
                else if (type == 2) //Sum
                {
                    temp = localSettings.Containers["Sum"].Values[number.ToString()].ToString();
                }
                else if (type == 3) // Date
                {
                    temp = localSettings.Containers["Date"].Values[number.ToString()].ToString();
                }
    
                return temp;
            }
    		
    		public static void updateVariables()
    		{
                int temp = getCostsInDB();
    			if(!(temp == -1))
    			{
    				Variables.costsInDB = temp;
    			}
    			else
    			{
    				Variables.costsInDB = 0;
    			}
    			
    			double temp2 = getTotalSum();
    			if(!(temp2 == -1))
    			{
    				Variables.totalSum = temp2;
    			}
    			else
    			{
    				Variables.totalSum = 0;
    			}
    			
    			Variables.latestCosts = getLatestCost();
    		}
    	}

    To Edit, Create and save data. the problem is that its doesn't work. It would be really helpful if someone could help me. Thanks :D

    Friday, December 20, 2013 7:37 PM

Answers

  • Hi,

    You can access AppData like local app data , Romaing app data and Temporary app data. You should read the links in the page:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh465099.aspx

    And If you want to access and edit a file which contain in DocumentLibrary,PictureLibrary and MusciLibrary you should describe the capability in appManifest.Or you can use OpenPicker which can access and edit a file in everywhere:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh758319.aspx

    There are same sample you should see:

    Application data sample

    File access sample

    File picker sample

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, December 26, 2013 2:39 AM

All replies

  • Hi,filfat

    Please upload your full code in SkyDrive so that I can test it. And you should clarify
    which error exist in your app. Please describe your problem in detail!



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.



    • Edited by Anne Jing Monday, December 23, 2013 9:22 AM edit
    Monday, December 23, 2013 9:21 AM
  • Hi,

    I see and test your project. There are some errors in your project:

    First: You should create the container before use it. You can refer to the link:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdatacontainer.aspx

    And see the Application data sample

    Second: You do not declare your file access in DocumentLibrary, see the link below:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/jj150593.aspx

    I recommend you should read this link and edit your code.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, December 25, 2013 6:42 AM
  • So, by reading http://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx I understand that i can access the AppData, however can i write to it? and if it is possible how? all im trying to do is to save, edit and remove some integers and strings from a file, and that was much easier to do in Normal desktop applications.

    P.S. a code example wouldn't hurt ;)

    Wednesday, December 25, 2013 9:21 PM
  • Hi,

    You can access AppData like local app data , Romaing app data and Temporary app data. You should read the links in the page:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh465099.aspx

    And If you want to access and edit a file which contain in DocumentLibrary,PictureLibrary and MusciLibrary you should describe the capability in appManifest.Or you can use OpenPicker which can access and edit a file in everywhere:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh758319.aspx

    There are same sample you should see:

    Application data sample

    File access sample

    File picker sample

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, December 26, 2013 2:39 AM