0

So for a school project I need to follow the instruction of a book called Programming with C# Databases. And in this chapter I need to make a datagridview with Bindingsources etc. So where I get the error in the title of this thread is when I'm defining a DataRelation. The code is as following:

private void CreateDataRelation()
{
    DataColumn dataColumnParent, dataColumnChild;

    //Hier definieer ik de variabelen dataColumnParent en Child en koppel ik ze aan de keys.
    dataColumnParent = datasetRugby.Tables["RugbyTeams"].Columns["Id"];
    dataColumnChild = datasetRugby.Tables["Spelers"].Columns["TeamId"];

    //Dit is de uiteindelijke relatie die wordt aangemaakt die ik later zal toevoegen aan DataSet.
    DataRelation dataRelationTeamSpeler = new DataRelation("relationTeamSpeler",
        dataColumnParent,
        dataColumnChild);

    //Op regel 118 voeg ik de relatie toe aan de DataSet.
    datasetRugby.Relations.Add(dataRelationTeamSpeler);

    //Hier zet ik de foreignkey van de dataset van Spelers en koppel ik hem aan de dataColumnChild.
    ForeignKeyConstraint foreignKeyConstraintTeamSpeler = dataRelationTeamSpeler.ChildKeyConstraint;

    //Hiermee geef ik aan dat je de team niet mag verwijderen als er nog spelers in dat team spelen.
    foreignKeyConstraintTeamSpeler.DeleteRule = Rule.None;

    //Als de Id van het team veranderd dan moet het teamId meeveranderen van alle spelers die in dat team spelen doormiddel van regel 129.
    foreignKeyConstraintTeamSpeler.UpdateRule = Rule.Cascade;

}

The line that the error occurred is this line of code

DataRelation dataRelationTeamSpeler = new DataRelation("relationTeamSpeler",
            dataColumnParent,
            dataColumnChild);`

I don't know how to fix this aswell as my fellow classmates. We compared our codes and they all have the same code because the books tells us what to do but for some reason the error only occurres with me.

What can I do ? HELP :(

Cheers,

Sach
  • 10,091
  • 8
  • 47
  • 84
  • 2
    Debug the code and see if either `dataColumnParent` or `dataColumnChild` is null before the `DataRelation` object is created. If so, that means your tables don't contain the data you expect. – Sach Jul 09 '18 at 18:04
  • 1
    Make sure you spelled the table and column names right. "Spelers" might be "Spielers" ? – Cetin Basoz Jul 09 '18 at 18:13
  • Like Sach mentioned, either `dataColumnParent` or `dataColumnChild` is null. The debugger is and always will be your best coding companion and can do a much better job helping you with this issue than we can. Consider updating your question to show the creation of your tables so we can help you pinpoint why one or both are null. – trix Jul 09 '18 at 18:15
  • (Just my curiosity, why the comments are in German and not in English or in your native language - copy pasted from a German textbook?) - Oh it is not even German, maybe Dutch? – Cetin Basoz Jul 09 '18 at 18:16
  • @CetinBasoz the comments are in Dutch. I need to explain to my teacher what line is for what. – Batuhan Beyogullari Jul 09 '18 at 18:40
  • OK. Do you have those tables and columns in those tables? If yes, do you have already filled the dataset with those tables? – Cetin Basoz Jul 09 '18 at 20:51

1 Answers1

0

Although this is homework, you've posted a reasonably acceptable MCVE, and made an attempt at the problem, so let me help you with some tips.

The answer to your immediate question is, either (or both) dataColumnParent or dataColumnParent is null. A tip on figuring out what's wrong is Googling the error message you get and also reading the documentation.

Documentation states that:

The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

And if you go to the documentation for the DataRelation class and see it's constructor details, it says the condition for ArgumentNullException is that:

One or both of the DataColumn objects contains null.

Essentially, your problem boils down to passing null values to the constructor when it's expecting non-null values.

In other words, one or both of following lines are returning null.

datasetRugby.Tables["RugbyTeams"].Columns["Id"];
dataColumnChild = datasetRugby.Tables["Spelers"].Columns["TeamId"];

So you should figure out why. Likely, as other users mentioned, you've misspelled a table or a column name.


Debugging

Best way to figure out your problem in a situation like this is to debug your code. Assuming you're a complete novice, here's how:

In your Visual Studio editor, you can click the gray vertical bar to the left of line numbers, which will put a break-point indicated by the red dot. Following is an extremely simple example.

enter image description here

Make sure your project is in Debug mode.

enter image description here

Now run your program, and the code execution will pause/halt at the point where you've put a breakpoint.

enter image description here

If you hover your mouse over a variable you can see it's current content.

enter image description here

Also there are Locals, Watch etc. windows that will also help see the contents of your variables.

enter image description here

By inspecting the contents of your variables (in your case, dataColumnParent and dataColumnParent) you can often easily narrow down on your problem.

Finally, here's some good resources on debugging.

Good luck!

Sach
  • 10,091
  • 8
  • 47
  • 84
  • Hello Sach, I would love to follow your reply but your pictures don't work for me. – Batuhan Beyogullari Jul 09 '18 at 18:48
  • What do you mean when you say _don't work_? Also even if you can't see them, if you follow the text instructions and the links I've given, that should be good enough. – Sach Jul 09 '18 at 20:43
  • 1
    Probably he meant the links to images are blocked in his country. @BatuhanBeyogullari, if that is the case, you could use VPN. For example you could download and use opera browser which has built-in VPN. – Cetin Basoz Jul 09 '18 at 20:47
  • @CetinBasoz Oh I didn't know that. If I'm not mistaken SO uses imgur? I don't know why someone would want to block that. – Sach Jul 09 '18 at 20:51
  • 1
    @BatuhanBeyogullari, I don't know what SO uses, just copy the link and navigate in opera. – Cetin Basoz Jul 09 '18 at 20:53
  • 1
    I thought OP was asking that. I don't know why someone would want to block that either, I don't know why would someone block wikipedia either. But that happens in my country and I am even afraid to spell the reason even if I know (and no I don't live in North Korea). – Cetin Basoz Jul 09 '18 at 21:03