//To Add new restaurant type record public void AddRestaurant(RestaurantsByAdo.Models.Restaurant restaurant) { string filePath = @"C:\repos\CodeSmart-Intern-Training\RestaurantsByAdoXML\RestaurantsByAdo\XMLFile.xml"; XDocument xmlDocument = XDocument.Load(filePath); XElement root = new XElement("Restaurant"); root.Add(new XElement("RestaurantName", restaurant.RestaurantName)); root.Add(new XElement("CuisineType", restaurant.CuisineType)); root.Add(new XElement("City", restaurant.City)); root.Add(new XElement("Rating", restaurant.Rating)); root.Add(new XElement("Contact", restaurant.Contact)); root.Add(new XElement("Id", GetRandomNumber(100, 10000))); xmlDocument.Element("Restaurants").Add(root); xmlDocument.Save(filePath); //using (SqlConnection con = new SqlConnection(connectionString)) //{ // SqlCommand cmd = new SqlCommand("spAddRestaurant", con); // cmd.CommandType = CommandType.StoredProcedure; // cmd.Parameters.AddWithValue("@RestaurantName", restaurantstype.RestaurantName); // cmd.Parameters.AddWithValue("@CuisineType", restaurantstype.CuisineType); // cmd.Parameters.AddWithValue("@City", restaurantstype.City); // cmd.Parameters.AddWithValue("@Rating", restaurantstype.Rating); // cmd.Parameters.AddWithValue("@Contact", restaurantstype.Contact); // con.Open(); // cmd.ExecuteNonQuery(); // con.Close(); //} }
//To Add new restaurant type record public void AddRestaurant(RestaurantsByAdo.Models.Restaurant restaurantstype) { using (SqlConnection con = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand("spAddRestaurant", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@RestaurantName", restaurantstype.RestaurantName); cmd.Parameters.AddWithValue("@CuisineType", restaurantstype.CuisineType); cmd.Parameters.AddWithValue("@City", restaurantstype.City); cmd.Parameters.AddWithValue("@Rating", restaurantstype.Rating); cmd.Parameters.AddWithValue("@Contact", restaurantstype.Contact); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } }
//Get the details of a particular Restaurant Type public Restaurant GetRestaurantsData(int?id) { RestaurantsByAdo.Models.Restaurant restaurant = new RestaurantsByAdo.Models.Restaurant(); using (SqlConnection con = new SqlConnection(connectionString)) { string sqlQuery = "SELECT * FROM tblRestaurantsType WHERE ID= " + id.Value.ToString(); SqlCommand cmd = new SqlCommand(sqlQuery, con); con.Open(); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { restaurant.RestaurantName = rdr["RestaurantName"].ToString(); restaurant.CuisineType = rdr["CuisineType"].ToString(); restaurant.City = rdr["City"].ToString(); restaurant.Rating = rdr["Rating"].ToString(); restaurant.Contact = rdr["Contact"].ToString(); } } return(restaurant); }