protected void grdPasswords_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { Int32 PasswordID = Convert.ToInt32(grdPasswords.DataKeys[e.RowIndex].Values["PasswordID"]); var p = (from pas in conn.Passwords1 where pas.PasswordID == PasswordID select pas).FirstOrDefault(); //process the delete conn.Passwords1.Remove(p); conn.SaveChanges(); //update the grid GetPasswords(); } } catch { Response.Redirect("/error.aspx"); } }
protected void GetPassword() { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //get id from url parameter and store in a variable Int32 PasswordID = Convert.ToInt32(Request.QueryString["PasswordID"]); var p = (from pas in conn.Passwords1 where pas.PasswordID == PasswordID select pas).FirstOrDefault(); //populate the form txtWebsiteName.Text = p.WebsiteName; txtPassword.Text = p.UserPassword; ddlSecurityLevel.SelectedValue = p.SecurityLevel.ToString(); } } catch { Response.Redirect("/error.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //create a new password object Passwords p = new Passwords(); //decide if updating or adding if (Request.QueryString.Count > 0) { Int32 PasswordID = Convert.ToInt32(Request.QueryString["PasswordID"]); p = (from pas in conn.Passwords1 where pas.PasswordID == PasswordID select pas).FirstOrDefault(); } //fill the properties of our object from the form inputs p.WebsiteName = txtWebsiteName.Text; p.UserPassword = txtPassword.Text; p.SecurityLevel = Convert.ToInt32(ddlSecurityLevel.SelectedValue); p.CreationDate = DateTime.Today; if (Request.QueryString.Count == 0) { conn.Passwords1.Add(p); } conn.SaveChanges(); //redirect to updated departments page Response.Redirect("/admin/home.aspx"); } } catch { //Response.Redirect("/error.aspx"); } }
protected void GetPasswords() { try { //connect using our connection string from web.config and EF context class using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //use link to query the Departments model var pass = from p in conn.Passwords1 select p; //bind the query result to the gridview grdPasswords.DataSource = pass.ToList(); grdPasswords.DataBind(); } } catch { Response.Redirect("/error.aspx"); } }