/// <summary> /// Issues the service delete. /// </summary> /// <param name="data">The data.</param> private void IssueServiceDelete(ServerServiceRequest data) { var js = new JavaScriptSerializer(); var url = new Uri(ClientConfiguration.ServerUrl.ToString() + data.ToUrl()); var client = HttpWebRequest.Create(url); client.Method = "DELETE"; client.GetRequestStream().Close(); using (var responseStream = client.GetResponse().GetResponseStream()) using (var reader = new StreamReader(responseStream)) { var content = reader.ReadToEnd(); try { var result = js.Deserialize <JsonSuccess>(content); var responseText = "Server Response: -" + result.Message; Response.Redirect("/Pages/DeletePage.aspx?response=" + responseText); } catch (Exception) { var error = js.Deserialize <JsonError>(content); var responseText = "Server Response: -" + error.Message; Response.Redirect("/Pages/DeletePage.aspx?response=" + responseText); } } }
/// <summary> /// Handles the Click event of the execute control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void execute_Click(object sender, EventArgs e) { ClientErrors.Text = "Client Errors<br>..."; ServerErrors.Text = "Server Errors<br>..."; // Populate data for service call var data = new ServerServiceRequest(); PopulateFields(data); // Bail out, if we have errors if (HasClientErrors()) { return; } // Make sure that the user has not selected both customers and products var hasCustomerField = (data.Customer_CustID != null || data.Customer_FirstName != null || data.Customer_LastName != null || data.Customer_PhoneNumber != null); var hasProductField = (data.Product_ProdID != null || data.Product_ProdName != null || data.Product_ProdWeight != null || data.Product_Price != null // Ignore the instock field... ); if (hasCustomerField && hasProductField) { AppendClientError("Cannot have <b>both</b> customer and product fields filled"); return; } if (GeneratePurchaseOrder.Checked) { // Different fields are mandatory when generate P.O. is checked if (data.Customer_CustID == null && data.Customer_LastName == null && data.Customer_FirstName == null && data.Order_OrderID == null && data.Order_PoNumber == null && data.Order_OrderDate == null) { AppendClientError(@"At least one of the following fields must be filled: Customer custID, Customer lastName, <br> Customer firstName, Order orderID, Order poNumber or Order orderDate when 'Generate P.O.' is checked"); return; } } if (!data.HasOneOrMoreFieldsWithAValue()) { AppendClientError("Please fill in at least one of the fields"); return; } if (RequestType == PageType.Insert) { if (!EnforceRequiredInsertFieldsFilled(data)) { return; } } // Check maximum lengths, because Linq to SQL generates crappy messages on the server end DateTime parsed; if (data.Customer_PhoneNumber != null && !Regex.Match(data.Customer_PhoneNumber, @"^\d{3}-\d{3}-\d{4}$").Success) { AppendClientError("Please use the following format on phone numbers (xxx-xxx-xxxx)"); return; } else if (data.Order_OrderDate != null && !DateTime.TryParseExact(data.Order_OrderDate, "MM-dd-yy", CultureInfo.CurrentCulture, DateTimeStyles.None, out parsed)) { AppendClientError("Please use the following format for order dates (MM-DD-YY) and proper dates"); return; } switch (RequestType) { case PageType.Search: { var purchaseOrder = GeneratePurchaseOrder.Checked; var url = new Uri(ClientConfiguration.ServerUrl.ToString() + data.ToUrl(purchaseOrder)); var client = HttpWebRequest.Create(url); client.Method = "GET"; using (var responseStream = client.GetResponse().GetResponseStream()) using (var reader = new StreamReader(responseStream)) { SearchContent = reader.ReadToEnd(); try { var js = new JavaScriptSerializer(); var result = js.Deserialize <SearchResult>(SearchContent); SearchResult = result; if (purchaseOrder) { Server.Transfer("/Pages/PurchaseOrderPage.aspx"); } else { Server.Transfer("/Pages/SearchResultsPage.aspx"); } } catch (Exception) { // Check if we have a Json Error var js = new JavaScriptSerializer(); var error = js.Deserialize <JsonError>(SearchContent); AppendServerError(error.Message); } } break; } case PageType.Insert: { var url = ClientConfiguration.ServerUrl; var client = HttpWebRequest.Create(url); client.Method = "POST"; var js = new JavaScriptSerializer(); var json = js.Serialize(data); var requestContent = Encoding.ASCII.GetBytes(json); client.ContentLength = requestContent.Length; var requestStream = client.GetRequestStream(); requestStream.Write(requestContent, 0, requestContent.Length); requestStream.Close(); using (var responseStream = client.GetResponse().GetResponseStream()) using (var reader = new StreamReader(responseStream)) { var responseContent = reader.ReadToEnd(); try { var success = js.Deserialize <JsonSuccess>(responseContent); WebFormTools.MessageBoxShow(this, success.Message); } catch (Exception) { var error = js.Deserialize <JsonError>(responseContent); AppendServerError(error.Message); } } break; } case PageType.Update: { var url = ClientConfiguration.ServerUrl; var client = HttpWebRequest.Create(url); client.Method = "PUT"; var js = new JavaScriptSerializer(); var json = js.Serialize(data); var requestContent = Encoding.ASCII.GetBytes(json); client.ContentLength = requestContent.Length; using (Stream requestStream = client.GetRequestStream()) { requestStream.Write(requestContent, 0, requestContent.Length); } using (var responseStream = client.GetResponse().GetResponseStream()) using (var reader = new StreamReader(responseStream)) { var responseContent = reader.ReadToEnd(); try { var success = js.Deserialize <JsonSuccess>(responseContent); WebFormTools.MessageBoxShow(this, success.Message); } catch (Exception) { var error = js.Deserialize <JsonError>(responseContent); AppendServerError(error.Message); } } break; } } }