forked from MicrosoftDocs/visualstudio-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFillOrCancel.cs
200 lines (179 loc) · 7.56 KB
/
FillOrCancel.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace SimpleDataApp
{
public partial class FillOrCancel : Form
{
public FillOrCancel()
{
InitializeComponent();
}
//<Snippet1>
// Storage for the order ID value.
private int parsedOrderID;
/// <summary>
/// Verifies that an order ID is present and contains valid characters.
/// </summary>
private bool IsOrderIDValid()
{
// Check for input in the Order ID text box.
if (txtOrderID.Text == "")
{
MessageBox.Show("Please specify the Order ID.");
return false;
}
// Check for characters other than integers.
else if (Regex.IsMatch(txtOrderID.Text, @"^\D*$"))
{
// Show message and clear input.
MessageBox.Show("Customer ID must contain only numbers.");
txtOrderID.Clear();
return false;
}
else
{
// Convert the text in the text box to an integer to send to the database.
parsedOrderID = Int32.Parse(txtOrderID.Text);
return true;
}
}
//</Snippet1>
//<Snippet2>
/// <summary>
/// Executes a t-SQL SELECT statement to obtain order data for a specified
/// order ID, then displays it in the DataGridView on the form.
/// </summary>
private void btnFindByOrderID_Click(object sender, EventArgs e)
{
if (IsOrderIDValid())
{
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.connString))
{
// Define a t-SQL query string that has a parameter for orderID.
const string sql = "SELECT * FROM Sales.Orders WHERE orderID = @orderID";
// Create a SqlCommand object.
using (SqlCommand sqlCommand = new SqlCommand(sql, connection))
{
// Define the @orderID parameter and set its value.
sqlCommand.Parameters.Add(new SqlParameter("@orderID", SqlDbType.Int));
sqlCommand.Parameters["@orderID"].Value = parsedOrderID;
try
{
connection.Open();
// Run the query by calling ExecuteReader().
using (SqlDataReader dataReader = sqlCommand.ExecuteReader())
{
// Create a data table to hold the retrieved data.
DataTable dataTable = new DataTable();
// Load the data from SqlDataReader into the data table.
dataTable.Load(dataReader);
// Display the data from the data table in the data grid view.
this.dgvCustomerOrders.DataSource = dataTable;
// Close the SqlDataReader.
dataReader.Close();
}
}
catch
{
MessageBox.Show("The requested order could not be loaded into the form.");
}
finally
{
// Close the connection.
connection.Close();
}
}
}
}
}
/// <summary>
/// Cancels an order by calling the Sales.uspCancelOrder
/// stored procedure on the database.
/// </summary>
private void btnCancelOrder_Click(object sender, EventArgs e)
{
if (IsOrderIDValid())
{
// Create the connection.
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.connString))
{
// Create the SqlCommand object and identify it as a stored procedure.
using (SqlCommand sqlCommand = new SqlCommand("Sales.uspCancelOrder", connection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
// Add the order ID input parameter for the stored procedure.
sqlCommand.Parameters.Add(new SqlParameter("@orderID", SqlDbType.Int));
sqlCommand.Parameters["@orderID"].Value = parsedOrderID;
try
{
// Open the connection.
connection.Open();
// Run the command to execute the stored procedure.
sqlCommand.ExecuteNonQuery();
}
catch
{
MessageBox.Show("The cancel operation was not completed.");
}
finally
{
// Close connection.
connection.Close();
}
}
}
}
}
/// <summary>
/// Fills an order by calling the Sales.uspFillOrder stored
/// procedure on the database.
/// </summary>
private void btnFillOrder_Click(object sender, EventArgs e)
{
if (IsOrderIDValid())
{
// Create the connection.
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.connString))
{
// Create command and identify it as a stored procedure.
using (SqlCommand sqlCommand = new SqlCommand("Sales.uspFillOrder", connection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
// Add the order ID input parameter for the stored procedure.
sqlCommand.Parameters.Add(new SqlParameter("@orderID", SqlDbType.Int));
sqlCommand.Parameters["@orderID"].Value = parsedOrderID;
// Add the filled date input parameter for the stored procedure.
sqlCommand.Parameters.Add(new SqlParameter("@FilledDate", SqlDbType.DateTime, 8));
sqlCommand.Parameters["@FilledDate"].Value = dtpFillDate.Value;
try
{
connection.Open();
// Execute the stored procedure.
sqlCommand.ExecuteNonQuery();
}
catch
{
MessageBox.Show("The fill operation was not completed.");
}
finally
{
// Close the connection.
connection.Close();
}
}
}
}
}
/// <summary>
/// Closes the form.
/// </summary>
private void btnFinishUpdates_Click(object sender, EventArgs e)
{
this.Close();
}
//</Snippet2>
}
}