private List<Person> ReadFromSql() { SqlConnection myConnection = new SqlConnection("Data Source=ACADEMY025-VM;Initial Catalog=Contacts;Integrated Security=SSPI"); contactList.Clear(); try { myConnection.Open(); SqlCommand readAll = new SqlCommand("select * from Contact order by ID", myConnection); SqlDataReader myReader = readAll.ExecuteReader(); while (myReader.Read()) { Person tmpPerson = new Person(myReader["Firstname"].ToString(), myReader["Lastname"].ToString(), myReader["SSN"].ToString(), myReader["ID"].ToString()); contactList.Add(tmpPerson); } } catch (Exception ex) { Response.Write(ex.Message); return contactList; } finally { myConnection.Close(); } return contactList; }
protected void btn_Update_Click(object sender, EventArgs e) { Button btn = (Button)sender; string text = btn.Text; if (IsPostBack) { if (hiddenfield.Value == "add") { Person tmpPerson = new Person(Request.Form["modalfirstname"], Request.Form["modallastname"], Request.Form["modalssn"], "add"); contactList.Add(tmpPerson); SaveSql(); } else { Person tmpPerson = new Person(Request.Form["modalfirstname"], Request.Form["modallastname"], Request.Form["modalssn"], Request.Form["modalid"]); foreach (Person p in contactList) { if (tmpPerson.ID == p.ID) { p.FirstName = tmpPerson.FirstName; p.LastName = tmpPerson.LastName; p.SSN = tmpPerson.SSN; } } SaveSql(); Response.Redirect(Request.RawUrl); } } }
protected void btn_Add_Click(object sender, EventArgs e) { if (IsPostBack) { Person tmpPerson = new Person(Request.Form["form_firstName"], Request.Form["form_lastName"], Request.Form["form_SSN"], "add"); contactList.Add(tmpPerson); SaveSql(); } }