added samples
[windows-sources.git] / sdk / samples / CrossTechnologySamples / ClassRegistration / workflowlibrary / registrationworkflow.cs
blob76902b5fb9b4f210d2873516a92b940010aa7096
1 using System;
2 using System.Data;
3 using System.Data.SqlClient;
4 using System.Drawing;
5 using System.Workflow.Runtime;
6 using System.Workflow.Activities;
7 using System.Workflow.Activities.Rules;
9 namespace Microsoft.Samples.ClassRegistration
11 public sealed partial class RegistrationWorkflow : SequentialWorkflowActivity
13 public RegistrationWorkflow()
15 InitializeComponent();
18 private bool IsClassFull = true;
19 private bool IsRegistered = false;
21 // Application settings
22 static Properties.Settings _settings = Properties.Settings.Default;
24 // Workflow properties
25 private String userIdValue;
26 public String UserId
28 get { return this.userIdValue; }
29 set { this.userIdValue = value; }
32 private String sessionIdValue;
33 public String SessionId
35 get { return this.sessionIdValue; }
36 set { this.sessionIdValue = value; }
39 private String registrationStatus;
40 public String RegistrationStatus
42 get { return this.registrationStatus; }
45 private void validateRegistration_ExecuteCode(object sender, EventArgs e)
47 Console.WriteLine("Validating registration.");
49 int studentCount = 0;
50 int seatCount = 0;
52 SqlConnection connection = new SqlConnection(_settings.ClassRegistrationConnectionString);
53 connection.Open();
55 // Get number of students registered for current session
56 SqlCommand command = new SqlCommand("SELECT Count(*) FROM Registration where SessionId=@SessionId", connection);
57 command.Parameters.AddWithValue("@SessionId", this.sessionIdValue);
58 studentCount = (int)command.ExecuteScalar();
60 // Get seat count for current session
61 command = new SqlCommand("SELECT SeatCount FROM ClassSession WHERE Id=@SessionId", connection);
62 command.Parameters.AddWithValue("@SessionId", this.sessionIdValue);
63 seatCount = (int)command.ExecuteScalar();
65 // Check if class is full
66 this.IsClassFull = studentCount >= seatCount;
68 // Check for duplicate registration
69 command = new SqlCommand("SELECT Count(*) FROM Registration WHERE SessionId=@SessionId AND StudentID=@StudentID", connection);
70 command.Parameters.AddWithValue("@SessionId", this.sessionIdValue);
71 command.Parameters.AddWithValue("@StudentId", this.userIdValue);
72 IsRegistered = (int)command.ExecuteScalar() > 0;
74 // Close SQL Connection
75 connection.Close();
78 private void notifyStudentAlreadyRegistered_ExecuteCode(object sender, EventArgs e)
80 Console.WriteLine("Notify student that they've already registered for this session.");
83 private void notifyStudentClassFull_ExecuteCode(object sender, EventArgs e)
85 Console.WriteLine("Nofify student that the requested session is already full.");
88 private void updateRegistrationStatusWaitlisted_ExecuteCode(object sender, EventArgs e)
90 this.registrationStatus = "Waitlisted";
93 private void updateRegistrationStatusApproved_ExecuteCode(object sender, EventArgs e)
95 this.registrationStatus = "Approved";
98 private void updateRegistrationStatusRejected_ExecuteCode(object sender, EventArgs e)
100 this.registrationStatus = "Rejected";
103 private void updateRegistrationStatusWaitingForApproval_ExecuteCode(object sender, EventArgs e)
105 this.registrationStatus = "Waiting for Approval";
108 private void addRegistrationToDatabase_ExecuteCode(object sender, EventArgs e)
110 Console.WriteLine("Adding registration to database.");
112 // Create SQL Connection
113 SqlConnection connection = new SqlConnection(_settings.ClassRegistrationConnectionString);
114 connection.Open();
116 // Execute SQL Command
117 string commandText = "INSERT INTO Registration (StudentId, SessionID, Status) " +
118 "VALUES (@StudentId, @SessionID, @Status)";
120 SqlCommand command = new SqlCommand(commandText, connection);
121 command.Parameters.AddWithValue("@StudentId", this.userIdValue);
122 command.Parameters.AddWithValue("@SessionId", this.sessionIdValue);
123 command.Parameters.AddWithValue("@Status", this.registrationStatus);
124 command.ExecuteNonQuery();
127 private void notifyStudentOfSessionStatus_ExecuteCode(object sender, EventArgs e)
129 Console.WriteLine("Notify student of session status.");