C# App Start To Finish Lesson 22 - Tournament Viewer Part 1

preview_player
Показать описание


Check out this video to see how we are going to build a complete application from start to finish in C#. Using .NET and Visual Studio, we will construct an application that is fit to launch. The application will use Winforms, a class library, events, SQL database, text file data storage (in CSV format), custom events and more. This is a 25-hour course that will allow you to follow along as I build an entire application, all for free!
Рекомендации по теме
Комментарии
Автор

I too got the null exception error like others in the comments, that Tim wasn't getting. Adding a null check on the LoadMatchup call of the method fixed it.

Another great lesson.

dctackett
Автор

All I had to do after the debug at 42:00 was to add a null check on the SelectedItem in the matchupListBox in the LoadMatchup method. Just added a return when the model is null. Strangely first time it comes as null.
Nice video btw

huzaifatinwala
Автор

The bug you ran into at minute 46 seemed pretty easy to solve..
I actually got a null-reference exception thrown when switching rounds, because we were trying to display the TeamName while we didn't yet have a team.
So I paused the video and tried to solve the issue on myself first (using the debugging methods from Video 21)
Eventually I built in a null-safety in the LoadMatchup function.
Right after we populate our MatchupModel I check if we actually have one there, and if it's null I exit the function.

I think this happens because when we change our DataSource it also registers as a "SelectedIndexChange".
It may be some kind of a work-around rather than a real 'fix', but so far it seems to work ^^

(I haven't yet finished the video while writing this, so I have yet to see how you fixed the issue)

Empathies
Автор

@IAmTimCorey Not sure why...but where you test the application at 42:10...the only thing I had to do to fix the issues was to modify the LoadMatchup so the bye for the first matchup would display in the team name label correctly, and I had to add a null check on the SelectedItem in the matchupListBox in the LoadMatchup method--it was causing a NullReferenceException on startup for some reason. I didn't need to apply any of the changes you made in the rest of the video for the application to work on my end--the team names and rounds list refreshes like it's supposed to. I know there's a two year difference--but do you why it works that way? It's a good thing in my opinion because I didn't have to write extra code and the code is simpler to read on my end--but it makes me wonder why I didn't get the same bugs as you...

zacharywatson
Автор

Thanks man! I love this course . Im really learning so much and im from argentina! You really are very clear to explain !!

neymarsr
Автор

Hi, great video. What we can conclude from this video, should we use a List or a BindingList
for data source and what’s are the differences (advantages or dis advantages)
between this two lists?

vladimirmilatovic
Автор

I came up with a different solution:

First, I tracked the initial problem to the event being fired when you change the round dropdown selection.

My solution:

Create a private method as follows:

private void ReloadMatchupLists()
{
matchupListBox.Items.Clear();

matchupListBox.DisplayMember = "DisplayName";

matchupListBox.SetSelected(0, true);
}
After calling the LoadMatchups(); method in the event, call the ReloadMatchupLists() method:

private void sender, EventArgs e)
{
LoadMatchups();
ReloadMatchupLists();
}

To ensure the first items are selected, add the following line to your WireUpRoundsLists() method:
matchupListBox.SetSelected(0, true); like so:

private void WireUpRoundsLists()
{
roundDropdown.DataSource = null;
roundDropdown.DataSource = rounds;
matchupListBox.SetSelected(0, true);
}

I also added the same line to the ReloadMatchupLists() method as shown above.

This gives me the same functionality without resorting to binding lists.

I hope this helps someone and I would love to hear your comments Tim.

Thanks for the excellent videos :)

Nawu
Автор

Hi Tim,
I think this issue where on loading Tournament Viewer, rounds dropdown was not being populated on form load.
As soon as you call method LoadRounds() (in TournamentViewer Constructor)where we add list of Matchups in designated Round and then call WireUpRoundsLists() which would assign
rounds to roundDropDown.DataSource. It fires automatically where we call method LoadMatchup(), where we populate the matchup List.
This works without any hiccup.

Also, thanks a lot for your teaching man. I start gaining confidence in writing code with your help. Long way to go.
You are a Guru!

ayushbaunthiyal
Автор

I've got the issue at minute 27 when in my MatchupModel me.TeamCompeting still returns null and the application crashes with nullreference exception. When I debug, instead of me.TeamCompeting accessing the TeamModel, it just says null. Can someone help?

shahnawazyusuf
Автор

i didn't have this problem, the only issue was I needed to check if the matchupmodel in LoadMatchup is not null. After that everything worked perfect. didn't need any of the binding list part. hope I don't run into a problem later because of this.
Thank you for eveything Tim you are the best !

saadablat
Автор

Hi Tim, I didn't do any changes from the minute "1:02:00" until the end of the video. Instead I just comment ( WireUpRoundsLists(); and WireUpMatchupsLists(); ) in the constructor and uncomment them inside ( LoadRounds() and LoadMatchups() ) and its worked very well for me. Should I keep it like that or should I do the changes you did ? Thank you.

iyad
Автор

I suspect this may be a difference in VisualStudio or C# version since I am over a year behind, but the solution for updating the matchupListBox breaks on me. When the LoadMatchup method gets called, the initial matchupListBox.SelectedItem is null, and so it throws an exception on the next line where it looks for the Entry.Count of the selectedMatchup(null). I tried several solutions for selecting a default index position, but ultimately failed because I was trying to call for a selection before the Entries are added in.


I was able to work past it by implementing the BindingLists that you did earlier in the video (at the 59:30 mark), except we had commented out the call for WireUpRoundsList and WireUpMatchupsList, in the LoadRounds and LoadMatchups methods. (Hence the Wireups were being called in the constructor, but only before the Rounds and Matchups were loaded). I suspect moving LoadRounds up in the constructor before the Wireups are called could be a second solution. The listbox now works as intended, however prevents the implementation of the Unplayed Only checkbox in the next video, since it was pretty dependent on the solution you came up with at the end of this video.


All that being said, kind of glad it didn't work as expected above, because I had to do some coding on my own to get around it.


Thanks for the videos!

CynicalSaint
Автор

Hi Tim, I've been having a problem with this part. The issue occurs when changing rounds and is, in some ways, the opposite of what has happened to you. Whenever I call Rounds.Clear() in the LoadMatchups method it clears the list, then it tries to call the method, but because the list is empty, selectedItem is null, so the LoadMatchup throws a System.NullReferenceException when trying to do the for loop.

For now I can just put a check to see if selectedItem is null, which in my opinion should be included in some form anyway as error handling, but I don't know why I'm getting a different behavior to you. It seems like you only call the method when you manually change it, while my program calls it whenever the ListBox changes. Maybe some update to C# or VS has caused the change in behavior? Just to confirm, I'm using a System.Windows.Forms.ListBox object and my event is:

+= new

EDIT: Funnily enough, the program only tries to run when I clear the List, but not when I try to add new elements.

KakoriGames
Автор

Hi Tim, I do not have the issue with the matchupListbox on .Net Framework 4.7.2! It seems to work properly with the code as before 51:20. I wanted to counter check with a version (4.5.2) mentioned in one of the comments below but when building solution, it was yelling about not finding TrackerLibrary.dll (error) and also Dapper versioning (warning). How to solve these?
Thanks for your great work!

zoltantoth
Автор

at the LoadMatchup debug, i had something different,
can't say if it's my visual studio 2019 or that i am using .net 5 winforms (not the .netframework),
but i actually got the errors thrown at me, and i instantly recognised it's a null check solution.

Labatbat
Автор

I see I'm definitely not the first to have a slightly different bug, but in my VS 2022 with Windows Form .NET 6.0 project I had too only crash at start of LoadMatchup() method which was solved with this two lines of code at start of that method (yeah just another version of easy fix mentioned here):
if (matchupListBox.SelectedItem == null)
return;
It's working with both List or BindingList.

But I admit I was really enjoying those last 20 minutes of this video when I knew my app was working. Yeah I know I'm mean :o)))

Thanks for video.

barnabas_
Автор

I think that the problem with bindingLists is in that that you first WierUp Form and then you LoadRounds. Problem is in that that you make new object for rounds (rounds = new BindingSource...) that mean that rounds now point to new object. So roundDropDown.DataSource doesn’t point on the same object as rounds because of that. You just need to do WireUp afret LoadRounds and then it will work.

vladimirmilatovic
Автор

I'm having this super weird problem. During your fix at around 28:50 for selecting round #2 or #3 from the selectRoundDropDown. Once I select round 2 from the dropdown, and call LoadMatchups() the line "int round = exception is the selectedItem from the dropdown is null 🤣

brad
Автор

Hi Tim,
I have doubt on passing tournament data from one form to other. In previous videos I observed that interface has been used to send data from one form to other. I expected an interface here. But in this the tournament data is passing through constructor. Why interface is not used here?

soujanyaguntuku
Автор

Hi Tim first Thanks a lot for this generous tutorial, here 16:18 when I click on load button it blow up an exception of Sequence contain no element at if > currRound) any help please I'm in urgent

hixamjocular