public ActionResult AddItem(AddUpdateItemModel model) { // Get the name and cost of the item from the model string itemName = model.AddName; int itemCost = model.AddCost; // Force all item name's passed through to be upper case itemName = itemName.ToUpper(); try { Console.WriteLine("Connecting to MySQL..."); conn.Open(); // Insert item based on the user's item name and cost of item // ID will be auto generated string sql = "INSERT INTO items (ITEM_NAME, COST) VALUES (@item, @cost)"; MySqlCommand cmd = new MySqlCommand(sql, conn); // Give parameters in query there value cmd.Parameters.AddWithValue("@item", itemName); cmd.Parameters.AddWithValue("@cost", itemCost); // Execute command cmd.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } conn.Close(); Console.WriteLine("Done."); // Direct POST request to updateAddItem page return(RedirectToAction("updateAddItem")); }
public ActionResult updateAddItem() { // Used to display the items in the front end string htmlOrig = "<div style=\"text-align: center; line-heght: 8px;\"><h3 style=\"text-align: center\">ID ---- Item Name ---- Cost</h3><br>"; try { Console.WriteLine("Connecting to MySQL..."); conn.Open(); // Check if there is nothing in the database first string sql = "SELECT ID,ITEM_NAME,COST FROM items"; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { htmlOrig += ("<span style=\"display: inline-block; vetical-align: middle; line-height: normal; font-size: 15px;\">" + rdr[0].ToString() + "\t ---- " + rdr[1].ToString() + "\t ---- " + rdr[2].ToString() + "</span><br>"); } htmlOrig += "</div>"; rdr.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } conn.Close(); Console.WriteLine("Done."); // Render through the html generated based on what the route is ViewBag.htmlOrig = htmlOrig; AddUpdateItemModel model = new AddUpdateItemModel(); return(View(model)); }
public ActionResult UpdateItem(AddUpdateItemModel model) { // Get the updated name and cost of the item based on the id from the model string itemName = model.UpdateName; int itemID = model.UpdateID; int itemCost = model.UpdateCost; // Force all item name's passed through to be upper case itemName = itemName.ToUpper(); try { Console.WriteLine("Connecting to MySQL..."); conn.Open(); // Update the name and/or cost of item based on id string sql = "UPDATE items SET ITEM_NAME = @item, COST = @cost WHERE ID = @id"; MySqlCommand cmd = new MySqlCommand(sql, conn); // Give parameters in query there value cmd.Parameters.AddWithValue("@item", itemName); cmd.Parameters.AddWithValue("@cost", itemCost); cmd.Parameters.AddWithValue("@id", itemID); // Execute command cmd.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } conn.Close(); Console.WriteLine("Done."); // Direct POST request to updateAddItem page return(RedirectToAction("updateAddItem")); }