private void btnOk_Click(object sender, EventArgs e) { if (id != -1) { using (var ctx = new UPdatabase()) { var result = ctx.UpData.SingleOrDefault(b => b.ID == id); result.UserName = txtName.Text; result.Password = Encryption.Encrypt(txtPassword.Text, true); ctx.SaveChanges(); } } else { var data = new DataVM(); data.UserName = txtName.Text; data.Password = Encryption.Encrypt(txtPassword.Text, true); using (var dbCtx = new UPdatabase()) { //Add Student object into Students DBset dbCtx.UpData.Add(data); // call SaveChanges method to save student into database dbCtx.SaveChanges(); } //MessageBox.Show("Data stored in the database"); } this.Close(); }
private void btnDelete_Click(object sender, EventArgs e) { UPdatabase ctx = new UPdatabase(); var item = new DataVM() { ID = id }; ctx.UpData.Attach(item); ctx.UpData.Remove(item); ctx.SaveChanges(); GetAllDbItems.RefreshGrid(grdData); }
internal static void RefreshGrid(DataGridView grid) { grid.DataSource = typeof(List<>); // OR use grid_accounts.DataSource = null; using (UPdatabase ctx = new UPdatabase()) { var allRows = ctx.UpData.ToList(); grid.DataSource = allRows; grid.Rows[0].Selected = true; } }
internal static void RefreshGrid(DataGridView grid) { grid.DataSource = typeof(List <>); // OR use grid_accounts.DataSource = null; using (UPdatabase ctx = new UPdatabase()) { var allRows = ctx.UpData.ToList(); grid.DataSource = allRows; grid.Rows[0].Selected = true; } }
internal static void myClickHandler(object sender, EventArgs e) { var menuItem = sender.ToString(); var idParts = menuItem.Split('-'); var id = Convert.ToInt32(idParts[0]); using (var ctx = new UPdatabase()) { var result = ctx.UpData.SingleOrDefault(b => b.ID == id); Clipboard.SetText(Encryption.Decrypt(result.Password, true)); } }
internal static void GetAllDbData(ToolStripDropDownItem toolStripMenu) { using (UPdatabase ctx = new UPdatabase()) { var allRows = ctx.UpData.ToList(); if (toolStripMenu.DropDownItems.Count != 0) { toolStripMenu.DropDownItems.Clear(); } foreach (var name in allRows) { toolStripMenu.DropDownItems.Add(name.ID + "-" + name.UserName, null, myClickHandler); item = ctx.UpData.FirstOrDefault(s => s.UserName == name.UserName); } } }
public frmData(int Id) { InitializeComponent(); id = Id; if (id != -1) { using (UPdatabase ctx = new UPdatabase()) { var item = ctx.UpData.FirstOrDefault(s => s.ID == id); txtName.Text = item.UserName; lblPassword.Text = "New password"; //txtPassword.Text = Encryption.Decrypt(item.Password, true); } } else { txtName.Text = ""; txtPassword.Text = ""; } }