0

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;
    }
}
1
  • What is the name of newly added file? Is it different? Commented May 15, 2018 at 8:37

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

}

5 Comments

Thanks for the reply. My thought process was that I had to add the book back to the list once it is updated?
No it doesn't need to add it again, because of calling by reference, it is enough to modify the properties of it, like book.title = txtTitle.Text,book.year = Int32.Parse(txtYear.Text); and etc...
Thanks, but it does not seem to be working. No changes are being made in the file?
@Strato, I have tested it and works properly with no problem, there may something went wrong.
Thanks mate, its probably me. Ill keep working at it.
0

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.

1 Comment

Hi thanks for the reply. Unfortunately it just added another item to the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.