child and parent table errors - c#

I have two tables that one table is parent and the other is child.
First,in form load, I wanna add a row to Parent, after that use Parent ID in child Table.
Of course all of those are temporary, till user confirm and save finally.
But through my code, I received this error:
ForeignKeyConstraint
FK_Table385_ParentTable_Table380_ChildTable
requires the child key values (3) to
exist in the parent table.
I know what this error means, and I can update Parent Table after that add rows to my child Table. But I can not do that. because all of those works are temporary till user confirm operation.
So what can I do?

If you configure your Typed datasets correctly this is handled automatically.
I don't recall all details but it requires the use of AutoIncrement on the PK and Cascading updates in the Relation.
You ought to see (debugger) that the new records get temporary negative values.
So if that (3) is the key value, something ain't right.

Use Datasets: insert data offline in your application and commit to database when user confirms.

Related

Generate database table from Umbraco content nodes

I'm currently working on a project to generate forms from the Umbraco (V.10) backend (I know that there is Umbraco Forms, but this is not an option).
I have a parent Node called "Formular" this generates the <form> tag. The parent can have various children like "Input Form Field", "Date Time Form Field" etc. These create an <input> field.
Document types
What I want to do is to generate a database table from all child nodes of the "Formular" node. The database attribute names should be the node ID.
A child node has a radio list with types like "text", "textarea", "number" etc. to set the <input type="">. If the type is getting changed or a node is deleted, a new table should be generated with the new child nodes. It would be an option to update the table if a new node was added and to set the previous entries to null in this column.
Example table
I thought that I could create a custom section where all child nodes are shown with their datatype and a button that is activated if any changes were made in the nodes. If the button is getting clicked, it generates the new table (maybe the table name could be set in the section too).
If this helps somehow, I can send my form with ajax this is how it looks:
Ajax POST
I tried to create a table with the id and data types which I get from my ajax post, but I realized, that it is dumb to always check if the id and datatype are the same in the table and to create a new one if this is false. So I thought about creating the table directly from the Umbraco nodes.
My problem is, that I don't know how to generate the table if I don't have a model with attributes like "public int ...". And since there is no way to generate these attributes with a loop i have no idea how to create the table.
Any thoughts on this problem?
Thanks
I have been working on that for quite a long time, but I could figure out a better way than creating the database table from the content nodes.
I just generated a table "FormSubmission" that saves a nodeId each time the form has been submitted. Then I created a second table named "FormContext". This one is related to the ID of the submission, and each input field gets saved as one table entry in the "FormContext" table with the submission ID as foreign key.
UML
This way I'm able to select all entries of a specific form and I can select all content a person has entered into the form.
Since I have all types an input field can have in the table, I don't have to update the table when the form changes or one form gets deleted.
I then created the tables like the umbraco documentation tells and just had to insert the data into the tables.
Umbraco creating a custom database table
I know this is not the most beautiful way but it works.

How do I get the ID from a selected row in a datagridview?

I have a c# program build in Visual Studio 2015 with a datagridview connected to a MySQL database. Everything is working fine. But, when I click on a row it gets selected, now I want to get the ID that row has in my MySQL database to perform another query with it. How do I do this?
I guess you should rather fetch the corresponding database key value(s) of the current row than the ID in the grid.
I had a similar problem some time ago. The solution was simply adding the key columns to the data source of the grid and - if required - hide them from view. Afterwards you can use these key values to perform all other kinds of actions (in my case: fetch child information from another table).
Using the ID from within your grid will be of no use.

How to display information from joined tables in a DataGridView

I would like to write a software which shows database contents in a Forms application. To make it easier to explain, I'll use a shopping list as an example.
I have a number of lists in one table tLists
I have a number of tProductPackages in another table
A list can consist of many productPackages and one productPackage can be in many lists. This m:n relationship is reflected in a separate table named tListProdPacks.
A productPackage contains a product. A product can have several productPackages. In every tProductPackage table entry, there's a foreign key FK_ProductID which references an entry in a table tProducts.
My forms application should navigate through all the lists using a BindingNavigator. In some bound controls, it should display the details of the selected list. This is working fine. (Just bound controls.)
All the list contents (entries in tListProdPacks for the selected tLists element) should be shown in a DataGridView. This is also working fine. Also the details of the productPackages (tProductPackages items i.e. one hop more to the next table) are displayed correctly. In this table he product value is available as ID (FK_ProductID) (foreign key to tProducts).
Is there a way to show the product name (contained only in tProducts) in the DataGridView whose DataSource is tListProdPacks?
(It's easy to display tProductPackages members, as the primary key from tProductPackages is referenced it tListProdPacks and using this ID I can show all other details from tProductPackages by just keep using the same valueMember but use different DisplayMember values for the single DataGridView columns [using dropdown controls in the DGV].)
But when jumping one table further (i.e. when jumping to the tProducts table), this does not work anymore.
I know that I could just create a custom query (join, view) to get exactly what I want, but then I cannot update the database from the dataset anymore.
Does anyone konw how to accomplish that?
(I guess that there's some easy way provided by the framework and I just don't see it, right?)
I'm not sure if I was able to describe what I would like to accomplish good enough. If not, please help me what to provide in addition.
Best regards,
Tom
u can relate the tables using joins if they contain primary key foreign key relation . using joins u can retrieve a single result set containing all the fields which u might need.

Binding gridview from sql table but this binding not for particular table it keeps on changing depending on user input

I have a table called TableExplorer which contains other table's names and their respective column names.
For example: table Customer may have 5 columns but in TableExplorer I may mention only 2 column names out of 5.
User send me the table name in query string, my job is to find that table name whether it is present or not in TableExplorer which I mentioned initially. If the user mentioned table is present then I should bind that table to a gridview and that gridview should have functionality like edit, delete, update. Finally any operation like edit, delete or update made by user should reflect in the respective table as mentioned by the user.
Database used : SQL Server 2008 R2
Programming language : C#, ASP.NET
Thanks in advance for helping.
Are you using WebForms? I'm far from being a WebForms expert but this is what I'd do:
You create different pages with GridView control, one for each table that you might want to bind, eg. Customer table. You create the CRUD operation for this GridView, Insert, Update, Delete. You create parameters for all the columns that the user might need, some of them could be invisible.
The user goes to his selection page, he choses a table and a list of columns and press Submit his request. Whenever you receive the query string you do 2 things:
Identify what table your user is asking and load the right page, if available. Maybe you can load only the specific GridView control in a specific using Ajax. Or maybe you can simple load another page.
Get the list of columns that the user wants to see in his grid from the querystring. Before showing the grid to him you keep these columns visible and hide/remove from the gridview every other column. You must pass this parameter to your insert/update methods as well.
I think this is a good solution to start, assuming you don't have a list of hundreds of tables, I wouldn't try to create something completely dynamic.

Datagrid usage sourced from SQL server

I would like to know the best way to use a datagrid control linked to a combination (join) of data tables in a way that both simply allows both display, creation of new rows in underlying tables and deletion.
The datagrid appears to offer the latter capabilities but I have not found a way that I am happy with to do more than just display on one grid and offer specific separate edit create and delete facilities.
Suppose for the sake of illustration that the database contains:-
Customer Table
* CustomerID
* CustomerName
Order Table
* CustomerID
* OrderLineItem
* OrderLineQuanity
And that I want to lose the CustomerID for display purposes but would like to be able to create new customers and delete existing ones, perhaps with a confirmatory dialog.
CSharpAtl is correct, use a Master-Detail control. An example of using one in a WinForm app is at http://msdn.microsoft.com/en-us/library/y8c0cxey.aspx.
WinForm DataGrids support add, edit, and delete of both Master and Detail records. As for your question about what happens if you change a Detail record so it matches a new Master; that is not possible. By design a Detail row only contains records that match the Master, you cannot (for example) change an order to belong to a new customer because the Detail row does not contain any customer information.
If you want to move a Detail row to another Master, you have to create a new Detail row for the new Master, copy the data from the old Detail row, and delete the old Detail row. If you're ambitious you could support Cut and Paste or Drag and Drop of Detail rows, but internally you have to Create/Copy/Delete.
If the relationship is 1 to many you can go the route of using Master Detail. [link text][1]
[1]: http://msdn.microsoft.com/en-us/library/aa479344.aspx/"Master Detail"
If I understand your question correctly, you have a query that performs a join of several tables which you display on a single grid. You’d like the user to be able to manipulate that grid and have the underlying tables reflect the changes.
An approach is to solve this problem is to implement stored procedures to perform the CRUD operations. The stored procedures will contain the logic to insert, update and delete records from all of the required tables. Each procedure will need to have a parameter for each bound field on the grid. Set the procedures to be the insert, update and delete commands on your data source.
So imagine if you are adding a new record to the grid. The grid calls the insert command, passes the parameters to the stored procedure. Then within the stored procedure you’ll create the logic to determine if the new line in the grid requires a new customer or if it’s an existing customer then adjust the operation accordingly.

Categories