0

I am working on a project in Visual Studio 2017 using C# which shows and saves test results, although I created a trial solution file to simplify the tables.

I have made a service-based database dtbTrial and created the following tables: for tblTestMain:

CREATE TABLE [dbo].[tblTestMain]
(
    [TestNumber] NUMERIC(8) NOT NULL PRIMARY KEY, 
    [TestDate] DATE NOT NULL, 
    [AmbientTemp] INT NOT NULL, 
    [Test1] BIT NOT NULL, 
    [Test2] BIT NOT NULL, 
    [Test3] BIT NOT NULL
)

for tblTest1 (also similar with tblTest2 and tblTest3):

CREATE TABLE [dbo].[tblTest1]
(
    [TestNumber] NUMERIC(8) NOT NULL PRIMARY KEY, 
    [ParameterA] REAL NULL, 
    [ParameterB] REAL NULL, 
    CONSTRAINT [FK_tblTest1_tblTestMain] FOREIGN KEY ([TestNumber]) REFERENCES [tblTestMain]([TestNumber])
)

The main table is in Form1 and the three others are in Form2, Form3, and Form4. The main form has a TabControl containing these three forms and functions like my previous question.

Now, what I am trying to do is that the data contained in the three sub forms or child forms navigate together with the parent form such that when Form1 navigates from test number 10000000 to 10000001, the three other forms follow suit when open. I tried using this query to dtbTrialDataSet:

SELECT        tblTestMain.TestNumber, tblTestMain.TestDate, tblTestMain.AmbientTemp, tblTestMain.Test1, tblTestMain.Test2, tblTestMain.Test3, tblTest1.ParameterA, tblTest1.ParameterB, tblTest2.ParameterC, tblTest2.ParameterD, 
                         tblTest3.ParameterE, tblTest3.ParameterF
FROM            tblTestMain LEFT OUTER JOIN
                         tblTest1 ON tblTestMain.TestNumber = tblTest1.TestNumber LEFT OUTER JOIN
                         tblTest2 ON tblTestMain.TestNumber = tblTest2.TestNumber LEFT OUTER JOIN
                         tblTest3 ON tblTestMain.TestNumber = tblTest3.TestNumber

and used the resulting dataset to the forms above but to no avail. What did I miss or what should I do to rectify it?

PS I haven't made any related codes to the main form yet since I'm at loss as to which commands I shall be needing.

eleu
  • 27
  • 3
  • Just to make sure I understand, you retrieve ALL of the data in your main form and then once it's retrieved you send the relevant data to the sub forms? What does "to no avail" mean? What is or is not happening and what error(s) do you receive, if any? – squillman Jan 08 '18 at 17:34
  • @squillman When I navigate in my main form, the main form shows the next or previous data but the three other forms remain with test number 10000000. I failed to understand the retrieving part but what I did was that the columns in `tblTestMain` is presented in `Form1`. – eleu Jan 08 '18 at 17:42

1 Answers1

0

Your sub forms don't know anything about what happens in the main form unless you specifically tell them. There are a couple of ways you can do that:

  1. You create an event on your main form (call it DataChanged or something like that) that is fired every time you retrieve data. Then your subforms subscribe to this event and read the updated data as appropriate.
  2. You have a public property on your subforms to hold the data loaded to those forms. Then, after retrieving data in your main from, you set that data property on the sub-forms.
squillman
  • 13,363
  • 3
  • 41
  • 60
  • I apologize, I'm still quite the beginner. Although I quite got the gist, I don't know how to execute it properly. What I tried to do is that when the main form navigates, a public string `TestData` (in the subform) captures the value shown in `testNumberTextBox` then an SQL command runs a query in the subform to check whether a record exists in the child tables. My problem is how I would subscribe to the event and call the string. – eleu Jan 09 '18 at 14:54
  • With the help of your answer together with [this question](https://stackoverflow.com/questions/23471705/winforms-raise-event-from-another-form), I have made it work thank you very much. – eleu Jan 09 '18 at 16:13