Esempio n. 1
0
        /// <summary>
        /// Adds a new Carpool to the database.
        /// </summary>
        /// <param name="carpool">Carpool to add to the database.</param>
        /// <returns>Primary Key (ID) of the newly added Carpool.</returns>
        public static int AddCarpool(Carpool carpool, out string errorMessage)
        {
            try
            {
                using (SqlConnection connection = ConnectionManager.GetConnection())
                {
                    errorMessage = String.Empty;

                    var data = new ActivEarthDataProvidersDataContext(connection);
                    var carpoolData = new CarpoolDataProvider
                    {
                        start = carpool.Start,
                        destination = carpool.Destination,
                        time = carpool.Time,
                        seats_available = carpool.SeatsAvailable,
                        comments = carpool.Comments,
                        user_id = carpool.UserId
                    };

                    data.CarpoolDataProviders.InsertOnSubmit(carpoolData);
                    data.SubmitChanges();

                    return carpoolData.id;
                }
            }
            catch (Exception e)
            {
                errorMessage = e.Message;
                return 0;
            }
        }
        public string AddCarpool(XElement input)
        {
            try
            {
                Carpool carpool = new Carpool()
                {
                    Start = input.Element("Start").Value,
                    Destination = input.Element("Destination").Value,
                    SeatsAvailable = byte.Parse(input.Element("SeatsAvailable").Value),
                    Time = input.Element("Time").Value,
                    Comments = input.Element("Comments").Value,
                    UserId = int.Parse(input.Element("UserId").Value)
                };
                string errorMessage;

                int carpoolId = CarpoolDAO.AddCarpool(carpool, out errorMessage);

                return "Carpool addition was " + (carpoolId > 0 ? "successful." : ("unsuccessful. Reason: " + errorMessage));
            }
            catch (Exception e)
            {
                return String.Format("Carpool addition was unsuccessful. Reason: {0}", e.Message);
            }
        }