Asp.net MVC 4, How making Master Detail in the same form - entity-framework-4.1

I use entity Framework 4.2 and MVC 4
I Got this model/Database structure
UserInformation
UserID(PK)
FirstName
LastName
Email
UserFavoriteColor
FavID(PK)
Color
Why
UserID(FK)
Is it possible in one Create Controller Action to fill the UserInformation table and then Fill the UserFavoriteColor.
I Know I could perform this in two steps by creating 2 separates sectiosn. But this is not what I want.

Typically, you would use jQuery to insert a new row. Since we don't know what your code looks like, it's hard to show you exactly how this should be done, but you can look at the examples here:
http://ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/
The trick is that you have to name them appropriately so that the model binder will add them to your collection when you click save. Then you have to write code in your post method to walk through the list of colors and add any records that don't exist already.
This is a relatively complex thing, so it's not something that can be easily covered in a single answer here.
Another option is to simply have an action for the add-new button, and this inserts a blank record into the data collection, which on postback will now get 3 records (one of them with null values). When you fill in the values, it will then postback to the main post method and udate the blank record.
This solution has the drawback that if the user adds a new record and doesn't save, the blank record stays in the database.

Related

Access combo box for DB mapping table 2-1 Table relation

I've run into a bit of a snag with a project I'm working on, and being new to Access I don't know if what I want to do is possible without VBA. I've looked around but all I can find are answers related to showing multiple columns, not controlling multiple fields in my DB.
To paint a picture I have a mapping system set up in my DB to help me distinguish the name and type of data is held in a table.
The setup is as follows:
-Data table is "LineItems" with an ID and the line data (think typical excel format)
-Mapping table is "LineItem_Mapper" with LineItem_ID, DataType, and Entity_ID
-A helper table "Data_Type" with ID and Name
-two "Entity" tables with differing properties both have ID and Name
The reason for the split is on data type is that the two types of data behave differently. One type has a parent child relation, and the other is a standalone row. I want to preserve this structure in my DB and feel I have done so with this mapping.
Now, on to the issue I'm running into. In my Access data entry form I want to use a combo box, as the options a user may chose for each line when entering are finite. However, this combo box is affecting the Mapping table above. I have been able to populate the box with my desired list with a custom query built from my 2 entity tables, but I don't know how to get Access to create or update the Mapping table using this box.
what I want to happen is when I chose something in the box, a line is created (or changed) in the mapping table with all 3 columns being populated. first the LineItem_ID for the line I am populating, and then the DataType and Entity ID to reflect the proper mapping.
Can Access do this on its own? Or do I need to do this with VBA?
As requested by the OP converting my commend as an answer (with a little bit more detail):
By far your best option is to use VBA. I doubt there is another way and even if there is it would be so convoluted it would be unworkable and unmanageable.
This should get you started:
In the combo box properties go tot the events tab and in After Update or On Change (look up the difference between the two events to see which behavior you prefer) click the down arrow and select [Event Procedure], then click on the … button. This will create a VBA module for you complete with the function that runs when the selected event is triggered.
You can use DoCmd.RunSQL "[Access SQL INSERT statement]" to add records to tables.
You can use Me.[MyComboBoxName] to get the current value of the combo box. Similarly the value of anything else in your active form.
You can use DLookup to get the value of any record in your tables.
Hopefully these will give you a relatively quick start.

Microsoft Access - Create Form to Post Data to Intersection Table

I am sincerely sorry if this question has been asked before, however with my limited knowledge of using Microsoft Access forms I am having an issue that is likely a very simple fix.
At the moment I am trying to create a form that will allow me to post data into an intersection table using combo boxes, the purpose is to create a relation between Clients and Project Numbers, these are intended on being a Many-to-Many relationship.
Relation Example
To begin with one issue I am running into is that when I use the intersection table to select a unique Client Name I get multiple entries from the drop down menu when there are multiple items in the intersecting table.
Form Example
The second part of my issue is that I am unsure how to post data to the intersecting table once the dropdown selections have been made.
Any help is greatly appreciated, and I thank everyone for their time.
The first part is to create a form in which you find, and edit data in the main client table.
This form will ONLY be based on the single main client table.
You perhaps ALREADY have a nice form to edit the client table anyway. I mean, how else will users find and edit a client anyway? So this issue and problem will LONG be dealt with before you do anything else of use in the application.
The form based on this single table might look like this:
Ok, now close the above, and create a second form to allow adding some choices of favorite colors (or in your case ClientContract). So the form will be based on this child table.
Again, like the first form, this form is based on ONE table.
Make this form continues, so click on ClientContract table, and then from ribbon choose multiple items form from the ribbon. It will look like this:
Of course we don’t want the user to have to manually type in the color (or project in your case), so we drop in a combo box from the ribbon and use the wizard. MAKE sure the first column of this combo is the PK of the ProjectNumber table, but FOR EASE of reading and selection, include ProjectNumber and Project Description columns in that comb box. NOTE in above how I also had added that combo box.
Assuming you closed and saved this form.
Now open our first main client form in design mode, and drag + drop in the continues second form we just created onto this form. The resulting form will look like this:
Access will automatic set up he customer_ID for us (because it is a sub form attached to the main form that only displays the one main record).
Once the above works, then again close the lot, and open up our continues form in design mode and remove all the extra junk.
(leaving ONLY the combo box).
The result is this:

How Do I Make a Subform Controlled by Report (Which is Controlled by a Query) Update After Records Are Added?

I recently took a duty position that requires me to track our administrative actions. Previously, the office had tracked them on an Excel spreadsheet, and the historical data was corrupted/missing. So I have built a database in Access 2010. I designed the database to display a main form that provides an overview of each action, but decided to require most of the data to be added or updated on specific data entry forms. My question relates to two subforms on the main form that I cannot get to update after data is entered into the tables. Here are specifics:
1) The main form is the case detail form, and each case has a unique case ID number that links most of the forms and tables.
2) The Case Detail Form has two subforms. One displays the names of the people involved in the case (this is the Subject Subform). The other displays the case history entries (this is the Case History Subform).
3) Both of the subforms are supposed to work the same way. Each is populated by a report which displays the information in its corresponding Subform. In turn, the reports underlying each Subform are based on a query that selects the records to display based in the case number.
4) New data for the subforms cannot be entered on the Case Details Form. Instead, the user can press a command button which launches a separate form that allows the user to enter either subject details or to enter a case history update. Once the user enters the data, he or she clicks A Save Record button, which saves the record and closes the form window. Everything seems to work fine up to this point.
5) However, I've now been working on this project for two weeks (as time permits), and I am still not able to make the two subforms update automatically. If it's a new case record, the user can make the Subforms update by using the navigation arrows to leave the main Case Details Form and then returning to it. If it's an established case record, a refresh button that I have added to the main Case Details form will cause the two subforms to update.
As I've tried to make this work, I've tried a number of approaches that I've found on various boards. Right now, I have:
A) The Subject data entry form has a Me.Requery statement in After Insert Event Procedure
B) The Subject data entry form has a SaveRecord and a CloseWindow command that are executed when the Save Record Button On Click event is triggered.
C) The main Case Detail Form has a executes a Me.Case_Subjects.Form.Requery when the GotFocus Event Procedure is executed.
I apologize for the lengthy question and explanation. I'm an Army officer and a little out of my depth with this. I would greatly appreciate any help anyone might be able to offer.
Best regards!
I'm not sure having understood everything but I try to give you some suggestions about what to try.
If I'm not wrong both form and subforms have the case ID as field so when you created the report you should have put them in "link" by the wizard (you can set it manually after but it's a bit more tricky).
Did you do this? This makes the form / subforms update when you navigate them.
Please note that there must be a relation between the underlaying tables to guarantee that the reports / forms moves together!
This applies to the query too, just remember to include the ID field int the queries.
If you don't want to see the ID (in case you use an autonumbering id) you can set it Not Visible.
In case you don't want to link the two tables with a relation there is another solution but it's a bit more tricky and, if it's not your case, I don't want to make confusion.
Let me know if you solved.
Bye
Firstly, the line Me.Requery should be on the After Update event, not the After Insert event. Secondly, you will then need to change Me.Requery to Me.[insertsubformname].Requery.
As the user above said, you will need to ensure that you have created your database relationships properly, however if you used the Insert Subform, it should've asked you what you wish to use as your link between the forms.

Editing table row data populated with with classic asp

I'm hoping this will be a rather simple question to answer, as I'm not looking for any specific code. I have a table on a classic asp page populated from an sql server. I've just set the table up so that each row is clickable and takes you to a page to edit the data in the row. My question is this: Would I be better off trying to use the recordset that populated the table or should I reconnect to the db and pull just the record I want edited.
As always; It Depends. It depends on what you need to edit about the record. It depends on How far apart your DB and site are from each other. It depends on which machine, if the DB and site are on separate machines, is more powerful.
That being said, you should make a new call for that specific record. The reason mainly being because of a specification you made in your question:
...and takes you to a page to edit the data in the row
You should not try to pass a record set between pages. There are a few reasons for this
Only collect what you need
Make sure data is fresh
Consider how your program will scale
On point 1 there are two ways to look at this. One is that you are trying to pass the entire record set across a page when you only need 1 record. There are few situations where another DB call would cost more than this. The other is you are only passing one record which would make me question your design. Why does this record set have every item related to a record. You are selecting way too much for just a result list. Or if the record is that small then Why do you need the new page. Why can you not just reveal an edit template for the item if it is that minimal.
On point 2 consider the following scenario. You are discussing with a coworker how you need to change a customer's record. You pull up this result set in an application but then nature calls and you step away from you desk. The coworker gets called by the customer and asked why the record is not updated yet. To placate the customer your coworker makes the changes. Now you are using an old record set and may overwrite additional changes your coworker made while you were away. This all happens because you never update the record set, you always just pass the old one from page to page.
On point 3 we can look back a point 1 a bit. let us say that you are passing 5 fields now. You decide though that you need a comments field to attach to one of your existing fields. do you intend to pass 2000 characters of that comment field to the next page? How about if each of the 5 need a comment field? Do you intend to pass 10,000 characters for a properly paged record set of 10? do you not do record set paging and need to pass 10,000 characters for a full 126 records.
There are more reasons too. Will you be able to keep your records secure passing them this way? Will this effect your user's experience because they have a crummy computer and cannot build that quick of a post request quickly? Generally it is better to only keep what you need and in most situations your result set should not have everything you need to edit.

Access--I need a list of dynamic check boxes on a form

I am trying to create a form in Access which will have a dynamic list of check boxes. It starts with a table tblMASTER_ATTACHMENT_LIST which will have three columns: MASTER_ATTACHMENT_ID, CLASS, and ATTACHMENT_NAME. (Before I go any further, this question is not about file attachments. Think more along the lines of an attachment to a report.) tblMASTER_ATTACHMENTS will be "pre-populated" with all possible reports and their classification. There will be another table tblREPORT_ATTACHMENTS, which will have the following columns: ID, REPORT_ID, MASTER_ATTACHMENT_ID, and ATTACHED_BL (boolean). So, depending on the class of the report, the list of possible attachments may change. I would like the list of possible attachments to be populated on a form, with check boxes next to each one to show whether it was actually attached or not.
I was thinking about a workflow such as once a user selects the class of report, to delete any records in the tblREPORT_ATTACHMENTS with the REPORT_ID equal to the current RECORD_ID, and then fill the form control with the results of SELECTing the records from the master table where CLASS = selected class.
If this workflow makes sense, I could use some help implementing it. Specifically, how do I build the control on the form that will hold the check boxes? If this workflow is not a very smart way to do, can someone suggest a better way? And explain how to do it?
By the way, I did search for this, but I don't really know what this type of thing is called. Makes it pretty hard to search for. If this is a common thing, perhaps someone could just point me in the right direction. In that case, a full answer here would not be necessary.