Refresh Detail Data Grid in WPF - wpf

I have a master and detail page. For Master table i have 4 text box displayed in detail layout and for detail table i have data grid. There is trigger on master table which insert some records in detail table. i want to refresh the detail data grid from database. How i can refresh detail data grid with database changes. Here is image. Data is inserted in upper form and there is trigger on upper form which insert data in detail table. I want to load data inserted by triggers in below grid. I m using Entity Framework and using context.SaveChanges(); to save data in database.
Thanks.

Usually Master details information displayed on same page; If there is your case, it is easy. when you are going to insert something in your master table, You can get updated records from detail tables based on PK. One downsize of this approach is, you need to be 100% sure how much trigger will take time.(I am not sure ADO.Net return status of ExecuteNonquery after insert row in specific table or completion of triggers).
In your case when you saveChanges(), make a request to get details against user. Hopefully it is the easiest way to achieve goal.
But if Master and details pages are separate, or information can be changed from other mean. Then you can use SqlDependency. Please Note that SqlDependency only works with SQL server. It monitor DB changes in specific table, and notify ADO.Net. You can handle SqlDependency.OnChange.

Related

MS Access <> SQL Server : save current record which creates GUID in SQL and go back to this record

The title is maybe not the best. My current setting: MS Access frontend with a SQL Server backend.
The SQL Server backend will create the PK with a GUID as soon the row is saved.
I need to save my record and go back to that record to use the created PK GUID.
When I do a Me.Refresh or Me.Requery, it will loose the connection to the record showing #Name? on all fields.
I need to get to GUID because I want to upload a file which is done in a FILESTREAM table and the link to the content table is done via the GUID of the content table.
So I need the content table GUID but as the record is not yet saved, I don't have one.
How to save the record, get its GUID and "stay" on that record?
I tried following which is not working:
DoCmd.RunCommand acCmdSaveRecord 'Here it will show '#Name? on all fields
Me.Refresh
Use Me.Requery instead of Me.Refresh and hope you stay on the same record. You can, of course, navigate to the last record, which opens a small door for race conditions (someone could in theory insert a record between you saving yours and you requerying).
Access does strange things to add records and then fetch the default data, as can be read here. It doesn't play well with GUID columns (or, really, anything that isn't a numeric identity primary key column without triggers). By forcing a requery you refetch all data.
Alternatively, you can bind the form to an ADO recordset and work with that, but that approach has some problems on its own, such as trouble with the built-in refresh/filter/sort options.

Inserting data in Many-to-Many relationship (intermediate table) via MS Access linked to SQL server database

I have a contact database, created on SQL server. An example of its many to many relationships is below:
1- Contact table (ContactID, Fname, Lname, ...)
2-Contact_Skill table (ContactID, SkillID, Score)
3-Skill table (SkillID, Skill_name, Skill_type)
For easier interaction, I linked this databse with MS access file (which will be the interface, used by users). Users mainly use it for insertion, and I have somewhere else a SQL report server to retrieve some data and do some reports.
My issue now is that, I'm trying to find an easier way (without programming) to insert data in the intermediate table (Contact_Skill), other than getting the IDs manually. A thing like a drop down menu, that enables the user to choose the skill and it turns into its ID. Any easier way would be so welcomed as well. Thanks
Ok, you likely don't need code, or at the very least VERY little code, and you certainly don't have to write code to insert the records.
So, I would create the form (bound directly to the linked contact table.
So, you can use ctrl-f, or whatever to search, find, jump to your record you want to edit. (so built in search features will suffice for now).
In that form, you will drop in a sub form. (a continues form - multiple items would work nice here.
So, in that form you have this:
In this example, I have
customers
Invoices
Invoice details
So, really, I have near example the same kind of setup. One master table (customers)
Then a child table of invoices.
And then for each invoice, we have details of the invoice.
So, your main form will have that sub form dropped in that shows the contact skills.
In my case, I have this:
So, for this, you not really written any code.
Now, do note that for each invoice (or your case of contact skills).
Note how I have a edit button in that continues form.
This allows our "drill" down.
The code behind this button is:
if me.Dirty = true, then me.Dirty = False
docmd.OpenForm "frmInvoice",,,"ID = " & me!id
So, clicking on edit will launch a new "main" form based on invoice (or in your case skills) the ONE record.
Now for that skills record, it not clear if you have another drill down (child table), but lets assume you do!
so, In that form, you simply drop in another sub form (again continues items), and you thus are now free to add more skills to that one contact skill.
In my case, it is invoice details, so we have this:
The above screens are rather crude (they are test scenes), but it shows a design in which you dealing with 3 typical related tables.
So the whole system works with about 6 lines of code. Just keep in mind that when you drop in a sub form to relate child items? Access will setup the foreign key value that belongs to the parent table that has the one "main" record, and that sub-form of child records. All that is required is that the link master/child setting is setup correct. Once done, then Access will manage and set the correct values used to relate the tables to each other, and you don't have to write any SQL or even any code for that matter. (you are free to add a sub form record, and then hit Edit button for further details to edit, or as noted add more child records to that form you just launched.
So related tables, and adding of child records is VERY automatic in Access, and you don't write code to add the records - access will do all this work for you if you follow the above design pattern. About the only code you ever had to add is the the "edit" button to launch the next form in the sequence.

Handling Onload events to add server side logic in ADF

I want to know how do we handle onload event so that we add some business logic once a view is loaded.
In brief this is what i want to achieve -
I have a entity object which i am showing as editable ADF table. Now once the page is loaded and I want the table to show at least 4 rows no matter how many rows exist in Database table. For example, if database table has 2 entries then ADF table should show those 2 rows and additional blank 2 rows in ADF table. And if the database table has 6 rows it should show 6 rows in ADF table.
How to programmatically handle this functionality. What functions and methods i need to override or add.
Any help is appreciated.
Thanks.
There are multiple ways of doing this, including doing this in a method call in the taskflow before you reach the page.
Some options:
https://blogs.oracle.com/adf/entry/an_epic_question_how_to

How to make SQLDataSource insert into view instead of the underlying table?

We have replaced 20 tables with a consolidated table, that separates each set of data via a "set id" (all the records for table "A" have a set_id of 1, table "B" is 2, etc.).
We then built views on the table, and renamed each so they had the views had the same names as the original 20 tables, with a WHERE to add the set_id. Net result - inserts/updates/selects of the views still work
We did this so our web page, which uses a sqldatasource with "sql command builder", wouldn't have to change. We added an INSTEAD OF INSERT trigger on each view, so that when you insert into the view, it adds the set_id and inserts into the consolidated table. So far, so good.
It partially works: UPDATEs and DELETEs work, because they know the actual ID for the record.
However INSERTs don't - when the command actually executes, we see "exec sp_executesql insert into consolidatedtable" - rather than hitting the view, the data source control finds the underlying table, then inserts directly into it. If we try adding fields to the views, they then show up in the data source control, but the web page then shows a configurable field.
Is there a way to change things on the database side to force it to use the view? My only other option at this point is to replace the views with tables, add an AFTER INSERT, UPDATE, DELETE trigger so that the consolidated gets updated, and then a process to make sure they're in sync and there are no issues.
MANY thanks in advance.

inserting into a view in SQL server

I have a SQL Server as backend and use ms access as frontend.
I have two tables (persons and managers), manager is derived from persons (a 1:1 relation), thus i created a view managersFull which is basically a:
SELECT *
FROM `managers` `m`
INNER JOIN `persons` `p`
ON `m`.`id` = `p`.`id`
id in persons is autoincrementing and the primary key, id in managers is the primary key and a foreign key, referencing persons.id
now i want to be able to insert a new dataset with a form in ms access, but i can’t get it to work. no error message, no status line, nothing. the new rows aren’t inserted, and i have to press escape to cancel my changes to get back to design view in ms access.
i’m talking about a managers form and i want to be able to enter manager AND person information at the same time in a single form
my question is now: is it possible what i want to do here? if not, is there a “simple” workaround using after insert triggers or some lines of vba code?
thanks in advance
The problem is that your view is across several tables. If you access multiple tables you could update or insert in only one of them.
Please also check the MSDN for more detailed information on restrictions and on proper strategies for view updates
Assuming ODBC, some things to consider:
make sure you have a timestamp field in the person table, and that it is returned in your managers view. You also probably need the real PK of the person table in the manager view (I'm assuming your view takes the FK used for the self-join and aliases it as the ID field -- I wouldn't do that myself, as it is confusing. Instead, I'd use the real foreign key name in the managers view, and let the PK stand on its own with its real name).
try the Jet/ACE-specific DISTINCTROW predicate in your recordsource. With Jet/ACE back ends, this often makes it possible to insert into both tables when it's otherwise impossible. I don't know for certain if Jet will be smart enough to tell SQL Server to do the right thing, though.
if neither of those things works, change your form to use a recordsource based on your person table, and use a combo box based on the managers view as the control with which you edit the record to relate the person to a manager.
Ilya Kochetov pointed out that you can only update one table, but the work-around would be to apply the updates to the fields on one table and then the other. This solution assumes that the only access you have to these two tables is through this view and that you are not allowed to create a stored procedure to take care of this.
To model and maintain two related tables in access you don’t use a query or view that is a join of both tables. What you do is use a main form, and drop in a sub-form that is based on the child table. If the link master and child setting in the sub-form is set correctly, then you not need to write any code and access will insert the person’s id in the link field.
So, don’t use a joined table here. Simply use a form + sub-form setup and you be able to edit and maintain the data and the data in the related child table.
This means you base the form on the table, and not a view. And you base the sub-form on the child table. So, don't use a view here.

Resources