Exemple #1
0
        protected void Button1_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);

            emp.ENAME    = txtename.Text;
            emp.JOB      = txtjob.Text;
            emp.MGR      = int.Parse(txtmgr.Text);
            emp.HIREDATE = DateTime.Parse(txthiredate.Text);
            emp.SAL      = int.Parse(txtsal.Text);
            // emp.COMM = Convert.ToInt32(txtcomm.Text);//because comm has null values,null will be converted to 0 but it will not handle empty
            if (c != 0)
            {
                emp.COMM = c;
            }
            emp.DEPTNO = int.Parse(txtdeptno.Text);


            d.SaveChanges();
        }
Exemple #2
0
 protected void Button1_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();
         label.Text       = "1 row added successfully";
         txtempno.Text    = "";
         txtename.Text    = "";
         txtjob.Text      = "";
         txtmgr.Text      = "";
         txtcomm.Text     = "";
         txtsal.Text      = "";
         txtdeptno.Text   = "";
         txthiredate.Text = "";
     }
     catch (DbUpdateException s)
     {
         SqlException ex = s.GetBaseException() as SqlException;
         if (ex.Message.Contains("EMP_PK"))
         {
             label.Text = "no duplicate empno";
         }
         else if (ex.Message.Contains("FK__EMPDATA__DEPTNO__236943A5"))
         {
             label.Text = "no deptno exists";
         }
         else
         {
             label.Text = ex.Message;
         }
         txtempno.Text    = "";
         txtename.Text    = "";
         txtjob.Text      = "";
         txtmgr.Text      = "";
         txtcomm.Text     = "";
         txtsal.Text      = "";
         txtdeptno.Text   = "";
         txthiredate.Text = "";
     }
 }