Question

I've got a problem with two tables which I thought is straight forward and turn out it's not. Table 1 (Word):

WordID - key
WordValue string
...

Table 2 (Sentence):

SentenceID - key
SentenceValue String
WordKey int

I need that one word has several sentences. So I set the relation in VS C# DataSet Designer. After I start the program I got the child table with red circles with exclamation marks and messages ForeignKeyConstraint WordSentence requires the child key value (30) to exist in the parent table. But I have several WordKey=30 and one WordId = 30. So as I understand I have a child with a key and the parent also has this value in ID field = 30 and 1 but don't have anything in a parent table although my structure looks like in Northwind database.

Was it helpful?

Solution 2

Thanks for answers. My problem was a bit silly cuz I needed the easiest way to deal with two tables in a database and wanted to present them with standard way C# and VS give (just carrying and dropping tables to a form and attach a navigator). So in my case I had two tables with a relationship 1 to many and needed so that when focus is on a record N of a parent table only some set of records could be seen in a child table. Instead I saw all of them in both parent and child tables. And when I set a relationship through a "Edit DataSet with Designer" I got an error.

Now a solution to this problem: as it stated in the book "Beginning Microsoft Visual C# 2008" and many other online sources, you create a data connection with a DB and create a DataSet. Then you drag tables you need to a form. But if you want a correct dependence so only particular set of records was seen in a child table but not all of them you need to drag a subtable in the DataSource window in VS!!! Like

enter image description here

OTHER TIPS

Though not directly addresses your problem as I am not really sure if I understood the issue correctly but given the problem statement, IMHO a better design for this could be this:

Table 1 (Word)

WordID - Primary Key
WordValue - String
...

Table 2 (Sentence)

SentenceID - Primary Key
SentenceValue - String
...

Table 3 (WordSentenceMap)

WordID (Foreign Key to Word)
SentenceID (Foreign Key to Sentence)

This way, one word can be used in multiple sentences and at the same time, one sentence can be used as an example for multiple words.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top