protected void btnupdate_Click(object sender, EventArgs e) { int eno = int.Parse(txtempno.Text); var E = from E1 in d.EMPDATAs where E1.EMPNO == eno select E1; EMPDATA emp = E.First(); int c = 0; bool b = int.TryParse(txtcomm.Text, out c);//to convert null to 0 emp.ENAME = txtename.Text; emp.JOB = txtjob.Text; emp.MGR = int.Parse(txtmgr.Text); emp.SAL = int.Parse(txtsal.Text); if (c != 0)//to show null if commission is 0 { emp.COMM = c; } emp.DEPTNO = int.Parse(txtdeptno.Text); d.SaveChanges(); }
protected void btnsubmit_Click(object sender, EventArgs e) { try { EMPDATA E = new EMPDATA(); E.EMPNO = int.Parse(txtempno.Text); E.ENAME = txtename.Text; E.JOB = txtjob.Text; E.MGR = int.Parse(txtmgr.Text); E.HIREDATE = DateTime.Parse(txthiredate.Text); E.SAL = int.Parse(txtsal.Text); E.COMM = int.Parse(txtcomm.Text); E.DEPTNO = int.Parse(txtdeptno.Text); d.EMPDATAs.Add(E); d.SaveChanges(); } catch (DbUpdateException E) { SqlException ex = E.GetBaseException() as SqlException; if (ex.Message.Contains("EMP_PK")) { lblMessage.Text = "no duplicate empno"; } else if (ex.Message.Contains("FK__EMPDATA")) { lblMessage.Text = "No Deptno "; } else { lblMessage.Text = ex.Message; } txtempno.Text = ""; txtename.Text = ""; txtjob.Text = ""; txtmgr.Text = ""; txthiredate.Text = ""; txtsal.Text = ""; txtcomm.Text = ""; txtdeptno.Text = ""; } }