Strato
Strato

Reputation: 53

Updating JSON file instead of adding

I am trying to update the information that is deserialised from my JSON file. The list is populated in an editable asp.net form and I would like to edit the item and then update the JSON file. When I make the changes it Adds a new file to the list instead of updating the original. How can I make it update instead of adding.

Thanks in advance!

public partial class EditBook : System.Web.UI.Page
 {
Catalogue catalogueInstance;

//Filepath for json file
const string FILENAME = 
 @"C:\Users\tstra\Desktop\19456932_CSE2ICX_Assessment_3\Bin\Books.json";


protected void Page_Load(object sender, EventArgs e)
{
    // reading data contained in the json filepath
    string jsonText = File.ReadAllText(FILENAME);

    //convert objects in json file to lists
    catalogueInstance = JsonConvert.DeserializeObject<Catalogue>(jsonText);

    ddlEdit.DataSource = catalogueInstance.books;
    ddlEdit.DataTextField = "title";
    ddlEdit.DataValueField = "id";

    //binding the data to Drop Down List
    ddlEdit.DataBind();
}

    protected void btnSubmit_editBook(object sender, EventArgs e)
  {
    int id = Int32.Parse(txtID.Text);
    string title = txtTitle.Text;
    int year = Int32.Parse(txtYear.Text);        
    string author = txtAuthor.Text;
    string publisher = txtPublisher.Text;
    string isbn = txtISBN.Text;

    catalogueInstance.books.Add(new Book(id, title, author, year, publisher, 
    isbn));

    string jsonText = JsonConvert.SerializeObject(catalogueInstance);
    File.WriteAllText(FILENAME, jsonText);

    txtSummary.Text = "Book ID of " + id + " Has Been Updated in the 
    Catalogue" + Environment.NewLine;
    }

    protected void ddlEdit_SelectedIndexChanged(object sender, EventArgs e)
    {
        Book b = catalogueInstance.books[ddlEdit.SelectedIndex];
        txtID.Text = b.id.ToString();
        txtTitle.Text = b.title;
        txtAuthor.Text = b.author;
        txtYear.Text = b.year.ToString();
        txtPublisher.Text = b.publisher;
        txtISBN.Text = b.isbn;
    }
}

Upvotes: 0

Views: 261

Answers (3)

Bhawna Jain
Bhawna Jain

Reputation: 759

Hey Try replacing below line

File.WriteAllText(FILENAME, jsonText);

with

 if (!File.Exists(FILENAME))
                {
                    var myFile = File.Create(FILENAME);
                    myFile.Close();
                }
                 File.WriteAllText(FILENAME, jsonText);

Then, only one file will be written over and over again.

Upvotes: 0

Aria
Aria

Reputation: 3844

It is clear you are adding item in btnSubmit_editBook so it would be like:

protected void btnSubmit_editBook(object sender, EventArgs e)
 {
    int id = Int32.Parse(txtID.Text); 
    Book book = catalogueInstance.books.SingleOrDefault(x=> x.id == id);
    if(book != null)
    {
       book.title = txtTitle.Text;
       book.year = Int32.Parse(txtYear.Text);        
       book.author = txtAuthor.Text;
       book.publisher = txtPublisher.Text;
       book.isbn = txtISBN.Text;
       txtSummary.Text = "Book ID of " + id + " Has Been Updated in the 
          Catalogue" + Environment.NewLine;
    } 
    string jsonText = JsonConvert.SerializeObject(catalogueInstance);
    File.WriteAllText(FILENAME, jsonText);

}

Upvotes: 0

Malcor
Malcor

Reputation: 2721

It looks like you're adding a new book every time. With this line

catalogueInstance.books.Add(new Book(id, title, author, year, publisher,isbn));

Look at grabbing the instance of the book you want to edit.

var book = catalogueInstance.books.firstOrDefault(b => b.id == id);

Then update your book.

Upvotes: 1

Related Questions