private int UpdateHouse(House house) { int docID; try { using (var db = new Models.HouseContext()) { var ho = db.House.FirstOrDefault(c => c.HouseID == house.HouseID); ho.HouseName = house.HouseName; ho.HouseDescription = house.HouseDescription; docID = ho.docID; db.SaveChanges(); return docID; } } catch (DbEntityValidationException dbEx) { Console.WriteLine(dbEx); return 0; } }
protected void AddHomeForm_ItemInserting(object sender, FormViewInsertEventArgs e) { XmlNode name = HouseDoc.SelectSingleNode("home/name"); name.InnerText = e.Values["name"].ToString(); XmlNode longitude = HouseDoc.SelectSingleNode("home/longitude"); longitude.InnerText = e.Values["longitude"].ToString(); XmlNode latitude = HouseDoc.SelectSingleNode("home/latitude"); latitude.InnerText = e.Values["latitude"].ToString(); MembershipUser myObject = Membership.GetUser(); object UserID = myObject.ProviderUserKey; using (var db = new Models.HouseContext()) { try { //var query = from b in db.House // orderby b.HouseID // select b; //List<string> lst = new List<string>(); //XmlDocument HouseDoc = new XmlDocument(); //XmlElement root = HouseDoc.CreateElement("home"); //XmlElement nm = HouseDoc.CreateElement("name"); //root.AppendChild(nm); //HouseDoc.AppendChild(root); //XmlDataSource homeDatasource = new XmlDataSource(); //HouseDataSource.Data = HouseDoc.InnerXml; //HouseDataSource.DataBind(); //AddHomeForm.DataBind(); #region SQL interface #region Insert xml into localDB Object returnValue; using (SqlConnection con = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand("InsertXmlDoc", con)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@theDoc", SqlDbType.Xml).Value = HouseDoc.OuterXml; con.Open(); returnValue = cmd.ExecuteScalar(); con.Close(); //TextBox HouseNameInsertTextBox = (TextBox)AddHomeForm.FindControl("HouseNameInsertTextBox"); House newHouse = new House { HouseName = name.InnerText, HouseDescription = "The best house in the world", docID = (int)returnValue }; House addedHouse = db.House.Add(newHouse); db.SaveChanges(); int houseId = addedHouse.HouseID; using (SqlCommand newcmd = new SqlCommand("INSERT INTO Houses_Users (HouseId, UserId) VALUES (@houseID, @userID)", con)) { newcmd.CommandType = CommandType.Text; newcmd.Parameters.Add("@houseID", SqlDbType.Int).Value = houseId; newcmd.Parameters.Add("@userID", SqlDbType.UniqueIdentifier).Value = UserID; con.Open(); returnValue = newcmd.ExecuteScalar(); con.Close(); } } } #endregion #region Select from View // Table to store the query results //DataTable table = new DataTable(); //// Creates a SQL connection //using (var connection = new SqlConnection(connectionString)) //{ // connection.Open(); // // Creates a SQL command // using (var command = new SqlCommand("SELECT * FROM Select_HouseName_XML", connection)) // { // // Loads the query results into the table // table.Load(command.ExecuteReader()); // } // connection.Close(); //} #endregion SqlDataSource1.DataBind(); HousesRpter.DataBind(); AddHomeForm.Visible = false; #endregion e.Cancel = true; } catch (DbEntityValidationException dbEx) { Console.WriteLine(dbEx); } } }