Asp.net C# - вставка и обновление базы данных
В настоящее время я занимаюсь проектом университетского веб-сайта, и мне было поручено создать свою страницу профиля, а также страницу редактирования профиля. Моя страница профиля будет отображать только информацию о пользователе и имеет кнопку "Изменить профиль" внизу, которая приведет пользователя на страницу редактирования профиля, содержащую текстовые поля с кнопкой "Сохранить" внизу. Я пытался обновить и вставить некоторые данные на странице редактирования профиля, и это не внесло никаких изменений в базу данных. Вот мой код:
Что я уже пробовал:
public partial class EditAccount : System.Web.UI.Page { SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True"); private readonly object MessageBox; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlCommand com; string str; conn.Open(); str = "SELECT Address.HouseNumber, Address.AddressLine1, Address.AddressLine2, Address.City, Address.PostCode, CardDetails.CardDetailsID, CardDetails.NameOnCard, CardDetails.NameOfCard, CardDetails.CardNumber, CardDetails.CardExpiryDate, CardDetails.CVV, AddressType.AddressTypeDescription, Member.MemberID, Member.MemberName, Member.Phone, Member.Email, Member.Username FROM Address FULL JOIN AddressType ON Address.AddressTypeID = AddressType.AddressTypeID FULL JOIN Member ON AddressType.MemberID = Member.MemberID FULL JOIN CardDetails ON Member.MemberID = CardDetails.MemberID WHERE Member.Email = '" + Session["Email"] + "'"; com = new SqlCommand(str, conn); SqlDataReader reader = com.ExecuteReader(); if (reader.Read()) { TxtEName.Text = reader["MemberName"].ToString(); LblEUser.Text = reader["Username"].ToString(); TxtEEmail.Text = reader["Email"].ToString(); TxtEPhone.Text = reader["Phone"].ToString(); TxtEType.Text = reader["AddressTypeDescription"].ToString(); TxtEHouse.Text = reader["HouseNumber"].ToString(); TxtEA1.Text = reader["AddressLine1"].ToString(); TxtEA2.Text = reader["AddressLine2"].ToString(); TxtECity.Text = reader["City"].ToString(); TxtEPostcode.Text = reader["PostCode"].ToString(); TxtENameOf.Text = reader["NameOfCard"].ToString(); TxtENameOn.Text = reader["NameOnCard"].ToString(); TxtECardNo.Text = reader["CardNumber"].ToString(); TxtEExpDate.Text = reader["CardExpiryDate"].ToString(); TxtECVV.Text = reader["CVV"].ToString(); reader.Close(); conn.Close(); } } } protected void BtnSave_Click(object sender, EventArgs e) { conn.Open(); SqlDataAdapter str = new SqlDataAdapter ("SELECT Address.HouseNumber, Address.AddressLine1, Address.AddressLine2, Address.City, Address.PostCode, CardDetails.NameOnCard, CardDetails.NameOfCard, CardDetails.CardNumber, CardDetails.CardExpiryDate, CardDetails.CVV, AddressType.AddressTypeDescription, Member.MemberName, Member.Phone, Member.Email, Member.Username FROM Address FULL JOIN AddressType ON Address.AddressTypeID = AddressType.AddressTypeID FULL JOIN Member ON AddressType.MemberID = Member.MemberID FULL JOIN CardDetails ON Member.MemberID = CardDetails.MemberID WHERE Member.Email = '" + Session["Email"] + "'",conn); DataSet ds = new DataSet(); str.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { SqlCommand updateCommand = new SqlCommand("UPDATE Member SET MemberName = '" + TxtEName.Text + "', Email = '" + TxtEEmail.Text + "', Phone = '" + TxtEPhone.Text + "' WHERE MemberID = @MemberID", conn); SqlCommand updateCommand1 = new SqlCommand("UPDATE Address SET HouseNumber ='" + TxtEHouse.Text + "', AddressLine1 = '" + TxtEA1.Text + "', AddressLine2 = '" + TxtEA2.Text + "', City = '" + TxtECity.Text + "',PostCode = '" + TxtEPostcode.Text + "' WHERE MemberID = @MemberID", conn); SqlCommand updateCommand2 = new SqlCommand("UPDATE AddressType SET AddressTypeDescription = '" + TxtEType.Text + "'", conn); SqlCommand updateCommand3 = new SqlCommand("UPDATE CardDetails SET NameOnCard = '" + TxtENameOn.Text + "', NameOfCard = '" + TxtENameOf.Text + "', CardNumber = '" + TxtECardNo.Text + "', CardExpiryDate = '" + TxtEExpDate.Text + "', CVV = '" + TxtECVV.Text + "' WHERE MemberID = @MemberID", conn); updateCommand.ExecuteNonQuery(); updateCommand1.ExecuteNonQuery(); updateCommand2.ExecuteNonQuery(); updateCommand3.ExecuteNonQuery(); Response.Write("<script>alert('Changes saved')</script>"); } else { SqlCommand insertCommand = new SqlCommand("INSERT INTO AddressType (AddressTypeDescription) VALUES (@AddressTypeDescription) WHERE MemberID = @MemberID", conn); insertCommand.Parameters.AddWithValue("@AddressTypeDescription", TxtEType.Text); insertCommand.ExecuteNonQuery(); SqlCommand insertCommand1 = new SqlCommand("INSERT INTO Address (HouseNumber, AddressLine1, AddressLine2, City, PostCode) VALUES (@HouseNumber, @AddressLine1, @AddressLine2, @City, @PostCode) WHERE MemberID = @MemberID", conn); insertCommand1.Parameters.AddWithValue("@HouseNumber", TxtEHouse.Text); insertCommand1.Parameters.AddWithValue("@AddressLine1", TxtEA1.Text); insertCommand1.Parameters.AddWithValue("@AddressLine2", TxtEA2.Text); insertCommand1.Parameters.AddWithValue("@City", TxtECity.Text); insertCommand1.Parameters.AddWithValue("@PostCode", TxtEPostcode.Text); insertCommand1.ExecuteNonQuery(); SqlCommand insertCommand2 = new SqlCommand("INSERT INTO CardDetails (NameOnCard, NameOfCard, CardNumber, CardExpiryDate, CVV) VALUES (@NameOnCard, @NameOfCard, @CardNumber, @CardExpiryDate, @CVV) WHERE MemberID = @MemberID", conn); insertCommand2.Parameters.AddWithValue("@NameOnCard", TxtENameOn.Text); insertCommand2.Parameters.AddWithValue("@NameOfCard", TxtENameOf.Text); insertCommand2.Parameters.AddWithValue("@CardNumber", TxtECardNo.Text); insertCommand2.Parameters.AddWithValue("@CardExpiryDate", TxtEExpDate.Text); insertCommand2.Parameters.AddWithValue("@CVV", TxtECVV.Text); insertCommand2.ExecuteNonQuery(); Response.Write("<script>alert('Changes saved')</script>"); } conn.Close(); } }