question

donbradman-4481 avatar image
0 Votes"
donbradman-4481 asked donbradman-4481 commented

View not updating and other issues after CRUD operations in WPF app

I have a WPF window with a tabcontrol with 2 tabitems, first one only displays some date filtered data from a SQLite database and the second tabitem also has a tabitem which shows filtered data where data is being filtered by the selecteditem of a combobox and then further filtered by a text box data with a button click.

I'm trying to implement CRUD operations using the selected row of the datagrid in the second tabitem and when the CRUD operations are done then the view in both the datagrid's (in tabitem 1 & 2) along with the combo box items should get updated.

But currently I'm having these issues and errors:

1) When I update a record then the updated record is displayed immediately as it should in the datagrid of the 2nd tabitem but the datagrid of the 1st tabitem does not update the view.
219150-view.gif



2) When I add a new record or delete a record the view (both datagrid's in tabitem1 & 2) does not update and If a new record has a new Party name then that should also be added to the combo box items but it doesn't either.
219263-addnew.gif

219259-del.gif


3) And lastly when adding a new record if I keep the PaidOn column blank then the next time I run the app I get an error:

219260-image.png


How do I fix all these errors?

The onedrive link for my project is here


dotnet-csharpwindows-wpfdotnet-sqlite
view.gif (1.3 MiB)
addnew.gif (771.9 KiB)
del.gif (706.9 KiB)
image.png (102.1 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

In regards to the last image, great time to set a breakpoint, inspect variable, see if any are DBNull than once know use assertion to remedy the problem. Unsure what to do? search for the error text and there are tons of post with common solutions such as this one.


0 Votes 0 ·

@karenpayneoregon To be honest I actually tried the common solution suggested by your link before the I posted this question (you can see the commented portion in the last image) but I forgot that there were two places where I needed to implement that and I did it in only one place so when I again got the error I thought that solution is not working, so my bad its working now.

Can you help on the issues showed in first two images ? Thanks.

0 Votes 0 ·

Can anyone help me with this please, I'm stuck at the errors shown in first two images. :(

0 Votes 0 ·

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered donbradman-4481 commented

Hi,@donbradman-4481.
1. The PendingBills and AllBills of the ICollectionView bound to the DataGrid in your two TabItems are m.PBills and m.SBills respectively. The data sources are different, so the views will not change synchronously.

When the data source of PendingBills and AllBills are the same, the view will change synchronously.

 _penBills = m.PBills;
    
  PendingBills = new ListCollectionView(_penBills)
  {
  Filter = o => ((Bills)o).PaidOn==""
  };
    
 _allBills = m.SBills;
  AllBills = new ListCollectionView(_allBills)
  {
  Filter = o => ((Bills)o).Party == SelectedCBItem
  };

2.Data CRUD works normally, and reloading program data updates normally. The current problem is that the view is not updated after modification.

There are three code blocks in the project that load data from the database, all of which are modified as follows. Even if the PaidOn of the newly added Item is empty, no error will be reported.

 public void LoadAllBillsFroBillsbase()
  {
  ...
    
  foreach (DataRow row in dt.Rows)
  {
    
  **var p = (row["PaidOn"] == DBNull.Value) ? String.Empty : (string)(row["PaidOn"]);**
    
  _sBills.Add(new Bills()
  {
  Id = Convert.ToInt32(row["Id"]),
  Party = (string)row["Party"],
  BillNo = (string)row["BillNo"],
  BillDt = (string)(row["BillDt"]),
  Amt = (string)row["Amt"],
  DueDt = (string)(row["DueDt"]),
  //PaidOn = (string)(row["PaidOn"])
  **PaidOn = p**
  });
    
  }
  m_dbConnection.Close();
  }

I'm trying to find a solution to update the view in time and will come back if there are any updates.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @HuiLiu-MSFT , Thanks for your reply.

As your rightly mentioned in your point 1

The PendingBills and AllBills of the ICollectionView bound to the DataGrid in your two TabItems are m.PBills and m.SBills respectively. The data sources are different, so the views will not change synchronously.

I've updated PendingBills and DoSave as under:

         PendingBills = new ListCollectionView(_allBills)
         {
             Filter = o => ((Bills)o).PaidOn=="" &&  ( DateTime.ParseExact(((Bills)o).DueDt, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture) >= DateTime.Today.AddDays(-50) && 
                              DateTime.ParseExact(((Bills)o).DueDt, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture) < DateTime.Today.AddDays(100))
         };
         void DoSave(object param)
         {
         ....
    
             SelectedInv = null;
    
             PendingBills.Refresh();
         }

So the view in both the datagrid updates when I edit an existing record and hit Save, but when I Delete or Add new item and Save, the view does not update. Also, the ComboItems need to update when I insert a new record that has a completely new Party.

0 Votes 0 ·

Hi @HuiLiu-MSFT , any updates ?

0 Votes 0 ·