Home > OS >  Getting Stack Overflow Error on specific SQL pull in WPF/C#
Getting Stack Overflow Error on specific SQL pull in WPF/C#

Time:01-20

My code keeps getting hung up on the 'using(SqlDataReader Reader = new cmd.ExecuteReader())' line throwing a StackOverflowException. I have not had this issue for any other SQL pulls from this same database, using the same general idea. I'm rather at a loss for what could be causing this issue, I've been trouble shooting it a fair bit by changing certain lines but it comes back to getting hung on that specific line.

public TeamRepository getTeamsinLeague(League L)
    {
        SqlCommand cmd;
        Model.TeamRepository MTR = new TeamRepository();
        Model.Team T = new Team();
        if (L == null) { L = new League { Id = 1 }; }
        String queryString = "SELECT Id, City, Name, r, g, b, Fontr, Fontg, Fontb FROM dbo.Team WHERE Team.League = "   L.Id  ";";
        using (SqlConnection conn = new SqlConnection(connection_String))
        {

            conn.Open();
            cmd = new SqlCommand(queryString, conn);

            using (SqlDataReader Reader = cmd.ExecuteReader())
            {
                while (Reader.Read())
                {
                    T = new Team
                    {
                        Id = (int)Reader[0],
                        City = Reader[1].ToString(),
                        Name = Reader[2].ToString(),
                        MainRed = (int)Reader[3],
                        MainGreen = (int)Reader[4],
                        MainBlue = (int)Reader[5],
                        SecondaryRed = (int)Reader[6],
                        SecondaryGreen = (int)Reader[7],
                        SecondaryBlue = (int)Reader[8],
                    };
                    MTR.addTeam(T);
                }
            }

        }
        return MTR;
    }

Editing in the call of the getTeamsInLeague

//*****************************************
        //Variable Declarations
        //*****************************************

        public Model.TeamRepository _TeamRepository { get; set; }
        public Model.Team _SelectedTeam;
        public LeagueRepository _MLR { get; set; }
        private Model.League _L;
        public Model.League L { get { return _L; } set { _TeamRepository = sdm.getTeamsinLeague(L); L = value; } }
        SqlDataMap sdm = new SqlDataMap();

and the relevent XML

<ComboBox ItemsSource="{Binding _MLR.LeagueArray}" SelectedItem="{Binding L}"  DisplayMemberPath="Name" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"/>

CodePudding user response:

Your L property is weirdly implemented. You cannot set a property in the setter of the same property itself. This will lead to a StackoverflowException.

Set the field instead of the property:

public Model.League L
{ 
    get { return _L; } 
    set { _L = value; _TeamRepository = sdm.getTeamsinLeague(_L); } 
}

And please following the naming conventions.

  •  Tags:  
  • Related