Click here to Skip to main content
16,018,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have read the text file i need to insert a new data in the text file
this is the method i have read the text file

C#
try

            {

                var path = @"text file\\GetAllEmp.txt";
                

                string rawJson = File.ReadAllText(path, Encoding.UTF8);

                ObservableCollection<EmployeeItem> Employee = new ObservableCollection<EmployeeItem>();
                var jsonData = JsonConvert.SerializeObject(rawJson);

                List<EmployeeItem> emp = JsonConvert.DeserializeObject<List<EmployeeItem>>(rawJson);

                listitem.ItemsSource = emp;


What I have tried:

i just new to add new data in the text file
how to add new data

My data base contains
C#
public static void AddEmployee(EmployeeItem Employee)
{
    string dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "chillDatabase.db");
    using (SqliteConnection db =
      new SqliteConnection($"Filename={dbpath}"))
    {
        db.Open();

        SqliteCommand insertCommand = new SqliteCommand();
        insertCommand.Connection = db;

        insertCommand.CommandText = "INSERT or REPLACE INTO SiteInfoTable VALUES (@EMP_SITEID, @EMP_GroupID, @EMP_ID,)";

        insertCommand.Parameters.AddWithValue("@EMP_SITEID ", Employee.SiteID);
        insertCommand.Parameters.AddWithValue("@EMP_GroupID ", Employee.GroupID);
        insertCommand.Parameters.AddWithValue("@EMP_ID ", Employee.Id);



        try
        {
            insertCommand.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
Posted
Updated 28-Nov-22 4:54am
v3
Comments
pkfox 28-Nov-22 9:46am    
Now you have your data in a database you can do what you like with it - what is the problem ?
Richard Deeming 29-Nov-22 4:20am    
catch (Exception ex)
{
    throw new Exception(ex.Message);
}

Seriously, don't do that! You've just thrown away any information you could possibly use to diagnose the exception.

If you need to throw a different exception type, then pass the exception you caught as the inner exception, and use a more specific type than the base Exception class:
catch (Exception ex)
{
    throw new InvalidOperationException(ex.Message, ex);
}

If you want to re-throw the original exception, then use throw; instead of throw ex;:
catch (Exception ex)
{
    throw;
}

But since you're not actually doing anything with the exception, then there's no point in catching it in the first place. Just remove the try..catch block, and let the exception propagate normally.

1 solution

THe only practical way to add data to a JSON file is to add it to the objects(s) you read from the file into your application, then write a new file that includes the old data.

JSON is not a database, it's a data transfer format - and it isn't organised in a way that makes "adding rows" or "adding columns" simple. If you regularly want to change JSON, you are probably better off using a database instead.

Quote:
there is no way to insert a new object? is there is othere way to insert a data


JSON files are text, just that: they aren't structured data until you read them with software the "understands" JSON.

And text files don't insert *anything* the only way to insert even a single character into a text file is to read all the characters up to the insertion point while writing them to a second file, write the new character to the second file, then copy all the other characters over.
When you have finished doing that, you close both the files, delete the original, and rename the new file to the old name.

That's a PITA with text, but with JSON data in a file it's worse, because it's structured in a way that JSON apps can understand and humans (with training) can read but not in a way that means it's easy to do as there is no way to tell where any object in the data starts or ends unless you read and process the JSON data as you go to find the end of the object to want to add something after! Which means ... your app needs to understand JSON extremely well in code you have written to do that as well as how to insert strings into text files!

If you want to insert (or update- or delete-) objects into existing data regularly, don't use JSON: use a proper database where the file format and the library code to handle it is designed to make that both easy and reliable.
 
Share this answer
 
v2
Comments
Ailiseu Brigitta 28-Nov-22 2:20am    
there is no way to insert a new object? is there is othere way to insert a data
OriginalGriff 28-Nov-22 2:53am    
Answer updated.
Ailiseu Brigitta 28-Nov-22 2:21am    
can i insert in a loop
pkfox 28-Nov-22 3:25am    
OG made it very clear, if you need to modify your data USE A DATABASE
Ailiseu Brigitta 28-Nov-22 4:55am    
i have updated my question

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