-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrphanRaces.aspx.cs
111 lines (99 loc) · 3.47 KB
/
OrphanRaces.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SailTally
{
public partial class OrphanRaces : Page
{
#region Enum
protected enum CellType
{
Header,
Normal,
Delete
};
#endregion
#region Methods
protected void AddColumn(TableRow row, string display, CellType cellType)
{
AddColumn(row, display, cellType, -1);
}
protected void AddColumn(TableRow row, string display, CellType cellType, int raceId)
{
var cell = new TableCell();
if (cellType == CellType.Delete)
{
var link = new LinkButton
{
Text = display,
CommandArgument = raceId.ToString()
};
link.Click+=link_Click;
cell.Controls.Add(link);
}
else
{
var label = new Label { Text = display };
if (cellType == CellType.Header)
{
label.Font.Bold = true;
}
cell.Controls.Add(label);
}
row.Cells.Add(cell);
}
#endregion
#region Events
protected void Page_Load(object sender, EventArgs e)
{
var row = new TableRow();
AddColumn(row, "Action", CellType.Header);
AddColumn(row, "Race ID", CellType.Header);
AddColumn(row, "First Warning", CellType.Header);
AddColumn(row, "Scored Races", CellType.Header);
tableOrphans.Rows.Add(row);
var ctx=new SailTallyDataContext();
// Sub Query
var raceSeries = (from rs in ctx.SS_RaceSeries
orderby rs.RaceID
select rs.RaceID).Distinct();
// Main Query
var races = from r in ctx.SS_Races
where !raceSeries.Contains(r.RaceID)
select r;
foreach (var race in races)
{
row = new TableRow();
AddColumn(row, "Delete", CellType.Delete, race.RaceID);
AddColumn(row, race.RaceID.ToString(), CellType.Normal);
AddColumn(row, CentralLibrary.FormatDisplayTime(race.FirstWarningDate), CellType.Normal);
AddColumn(row, (CentralLibrary.IsRacesScored(race.RaceID) ? "Yes" : "No"), CellType.Normal);
tableOrphans.Rows.Add(row);
}
}
protected void link_Click(object sender, EventArgs e)
{
var link = (LinkButton)sender;
var raceId = Convert.ToInt32(link.CommandArgument);
labelError.Text = "";
try
{
CentralLibrary.RemoveScores(raceId);
// Remove Races
var ctx = new SailTallyDataContext();
var race = (from r in ctx.SS_Races
where r.RaceID == raceId
select r).Single();
ctx.SS_Races.DeleteOnSubmit(race);
ctx.SubmitChanges();
Server.Transfer("~/OrphanRaces.aspx");
}
catch (Exception ex)
{
labelError.Text = "Unable to remove Orphan Race (Race ID: " + raceId.ToString() + "). Reason:" + ex.Message;
}
}
#endregion
}
}