public static MenuItem GetMenuItemForIdAndType(int id, MenuItem.ItemType type) { MenuItem item = null; if (id != -1) { //SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=\"X:\\New folder\\MenuOrdering\\App_Data\\Database.mdf\";Integrated Security=True"); SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=X:\\menu_order_websit\\MenuOrdering\\MenuOrdering\\App_Data\\Database.mdf;Integrated Security=True"); connection.Open(); string TABLE = type == MenuItem.ItemType.Dessert ? "Desserts" : type == MenuItem.ItemType.Drink ? "Drinks" : "Meals"; string sql = "SELECT id, description, price FROM " + TABLE + " WHERE id = @id"; SqlCommand cmd = new SqlCommand(sql, connection); cmd.Parameters.AddWithValue("@id", id); cmd.CommandType = CommandType.Text; SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { item = new MenuItem(type, reader.GetInt32(0), reader.GetString(1), (double)reader.GetDecimal(2)); } } } return(item); }
public static List <MenuItem> GetItemsOrdered(int theOrderId) { List <MenuItem> items = new List <MenuItem>(); //SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=\"X:\\New folder\\MenuOrdering\\App_Data\\Database.mdf\";Integrated Security=True"); SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=X:\\menu_order_websit\\MenuOrdering\\MenuOrdering\\App_Data\\Database.mdf;Integrated Security=True"); connection.Open(); string sql = "SELECT id, drink_id, meal_id, dessert_id, order_id FROM OrderedItems WHERE order_id = @order_id"; SqlCommand cmd = new SqlCommand(sql, connection); cmd.Parameters.AddWithValue("@order_id", theOrderId); cmd.CommandType = CommandType.Text; SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { int id = -1; MenuItem.ItemType type = MenuItem.ItemType.Dessert; if (reader.IsDBNull(1) == false) { id = reader.GetInt32(1); type = MenuItem.ItemType.Drink; } if (reader.IsDBNull(2) == false) { id = reader.GetInt32(2); type = MenuItem.ItemType.Meal; } if (reader.IsDBNull(3) == false) { id = reader.GetInt32(3); type = MenuItem.ItemType.Dessert; } items.Add(GetMenuItemForIdAndType(id, type)); } } connection.Close(); return(items); }