forked from MicrosoftDocs/visualstudio-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewCustomer.cs
202 lines (179 loc) · 7.58 KB
/
NewCustomer.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
201
202
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace SimpleDataApp
{
public partial class NewCustomer : Form
{
public NewCustomer()
{
InitializeComponent();
}
//<Snippet1>
// Storage for IDENTITY values returned from database.
private int parsedCustomerID;
private int orderID;
/// <summary>
/// Verifies that the customer name text box is not empty.
/// </summary>
private bool IsCustomerNameValid()
{
if (txtCustomerName.Text == "")
{
MessageBox.Show("Please enter a name.");
return false;
}
else
{
return true;
}
}
/// <summary>
/// Verifies that a customer ID and order amount have been provided.
/// </summary>
private bool IsOrderDataValid()
{
// Verify that CustomerID is present.
if (txtCustomerID.Text == "")
{
MessageBox.Show("Please create customer account before placing order.");
return false;
}
// Verify that Amount isn't 0.
else if ((numOrderAmount.Value < 1))
{
MessageBox.Show("Please specify an order amount.");
return false;
}
else
{
// Order can be submitted.
return true;
}
}
/// <summary>
/// Clears the form data.
/// </summary>
private void ClearForm()
{
txtCustomerName.Clear();
txtCustomerID.Clear();
dtpOrderDate.Value = DateTime.Now;
numOrderAmount.Value = 0;
this.parsedCustomerID = 0;
}
//</Snippet1>
//<Snippet2>
/// <summary>
/// Creates a new customer by calling the Sales.uspNewCustomer stored procedure.
/// </summary>
private void btnCreateAccount_Click(object sender, EventArgs e)
{
if (IsCustomerNameValid())
{
// Create the connection.
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.connString))
{
// Create a SqlCommand, and identify it as a stored procedure.
using (SqlCommand sqlCommand = new SqlCommand("Sales.uspNewCustomer", connection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
// Add input parameter for the stored procedure and specify what to use as its value.
sqlCommand.Parameters.Add(new SqlParameter("@CustomerName", SqlDbType.NVarChar, 40));
sqlCommand.Parameters["@CustomerName"].Value = txtCustomerName.Text;
// Add the output parameter.
sqlCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
sqlCommand.Parameters["@CustomerID"].Direction = ParameterDirection.Output;
try
{
connection.Open();
// Run the stored procedure.
sqlCommand.ExecuteNonQuery();
// Customer ID is an IDENTITY value from the database.
this.parsedCustomerID = (int)sqlCommand.Parameters["@CustomerID"].Value;
// Put the Customer ID value into the read-only text box.
this.txtCustomerID.Text = Convert.ToString(parsedCustomerID);
}
catch
{
MessageBox.Show("Customer ID was not returned. Account could not be created.");
}
finally
{
connection.Close();
}
}
}
}
}
/// <summary>
/// Calls the Sales.uspPlaceNewOrder stored procedure to place an order.
/// </summary>
private void btnPlaceOrder_Click(object sender, EventArgs e)
{
// Ensure the required input is present.
if (IsOrderDataValid())
{
// Create the connection.
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.connString))
{
// Create SqlCommand and identify it as a stored procedure.
using (SqlCommand sqlCommand = new SqlCommand("Sales.uspPlaceNewOrder", connection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
// Add the @CustomerID input parameter, which was obtained from uspNewCustomer.
sqlCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
sqlCommand.Parameters["@CustomerID"].Value = this.parsedCustomerID;
// Add the @OrderDate input parameter.
sqlCommand.Parameters.Add(new SqlParameter("@OrderDate", SqlDbType.DateTime, 8));
sqlCommand.Parameters["@OrderDate"].Value = dtpOrderDate.Value;
// Add the @Amount order amount input parameter.
sqlCommand.Parameters.Add(new SqlParameter("@Amount", SqlDbType.Int));
sqlCommand.Parameters["@Amount"].Value = numOrderAmount.Value;
// Add the @Status order status input parameter.
// For a new order, the status is always O (open).
sqlCommand.Parameters.Add(new SqlParameter("@Status", SqlDbType.Char, 1));
sqlCommand.Parameters["@Status"].Value = "O";
// Add the return value for the stored procedure, which is the order ID.
sqlCommand.Parameters.Add(new SqlParameter("@RC", SqlDbType.Int));
sqlCommand.Parameters["@RC"].Direction = ParameterDirection.ReturnValue;
try
{
//Open connection.
connection.Open();
// Run the stored procedure.
sqlCommand.ExecuteNonQuery();
// Display the order number.
this.orderID = (int)sqlCommand.Parameters["@RC"].Value;
MessageBox.Show("Order number " + this.orderID + " has been submitted.");
}
catch
{
MessageBox.Show("Order could not be placed.");
}
finally
{
connection.Close();
}
}
}
}
}
/// <summary>
/// Clears the form data so another new account can be created.
/// </summary>
private void btnAddAnotherAccount_Click(object sender, EventArgs e)
{
this.ClearForm();
}
/// <summary>
/// Closes the form/dialog box.
/// </summary>
private void btnAddFinish_Click(object sender, EventArgs e)
{
this.Close();
}
//</Snippet2>
}
}