Example #1
0
        private void button1_Click(object sender, EventArgs e)    //save button for Form4.  Note: we differentiate between insert and update operation
        {
            CompanyDBDataContext dc = new CompanyDBDataContext(); //establish connection

            if (textBox1.ReadOnly == false)                       // insert operation is happening
            {
                Employee obj = new Employee();
                obj.Eno    = int.Parse(textBox1.Text);
                obj.Ename  = textBox2.Text;
                obj.Job    = textBox3.Text;
                obj.Salary = decimal.Parse(textBox4.Text);
                obj.Dname  = textBox5.Text;

                dc.Employees.InsertOnSubmit(obj); //pending insert
                dc.SubmitChanges();               //commit the data
                MessageBox.Show("Record inserted into the table.");
            }
            else  //update operation triggered on Form3 and now new entries must be inserted in this Form4
            {
                Employee obj = dc.Employees.SingleOrDefault(E => E.Eno == int.Parse(textBox1.Text)); //reference to existing record.  Use lambda to access values
                obj.Ename  = textBox2.Text; //user modified value is in textbox
                obj.Job    = textBox3.Text;
                obj.Salary = decimal.Parse(textBox4.Text);
                obj.Dname  = textBox5.Text;
                dc.SubmitChanges();
                MessageBox.Show("Record updated in the table.");
            }
            {
            }
        }
Example #2
0
        private void Button1_Click(object sender, EventArgs e)
        {
            CompanyDBDataContext d = new CompanyDBDataContext();

            if (textBox1.ReadOnly == false)    // as we this method will be called while we press insert as well a update , so the difference between the insert and the update  is the textBox.ReadOnly as false or true. Hence we can use it as a condition to differentiate if its a insert or update.
            {
                Employee obj = new Employee(); // to access the Employee class we created the instance

                //Assigning the values to the property

                obj.Eno    = int.Parse(textBox1.Text);     //here we have to explicitly convert the string value to int .int.Parse converts strings to int.
                obj.Ename  = textBox2.Text;                //it is string
                obj.Job    = textBox3.Text;
                obj.Salary = decimal.Parse(textBox4.Text); // because we used type money in database so we have to convert into decimal or int
                obj.Dname  = textBox5.Text;
                d.Employees.InsertOnSubmit(obj);           //InsertOnSubmit(obj) inserts the record to the table but wont commit (appending k)
                d.SubmitChanges();                         // this will commit the data changes
                MessageBox.Show("The Record has been inserted into the table");
            }
            else
            {
                //Creating a refernce to an existing record
                Employee obj = d.Employees.SingleOrDefault(E => E.Eno == int.Parse(textBox1.Text)); //here the Eno. becomes a refernce to the existing records
                obj.Ename  = textBox2.Text;                                                         //now if user doesnt modifies it then the old value will be overridden to the old values but if the user modifies it with a new value then the new value overrides the old value.
                obj.Job    = textBox3.Text;
                obj.Salary = decimal.Parse(textBox4.Text);
                obj.Dname  = textBox5.Text;
                d.SubmitChanges(); // this will commit the data changes
                MessageBox.Show("The Record has been Updated into the table", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Example #3
0
 private void button3_Click(object sender, EventArgs e) //Delete button
 {
     if (dgView.SelectedRows.Count > 0)
     {
         if (MessageBox.Show("Are you sure of deleting the selected record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)   ////user clicked yes
         {
             int      Eno = Convert.ToInt32(dgView.SelectedRows[0].Cells[0].Value);
             Employee obj = dc.Employees.SingleOrDefault(E => E.Eno == Eno); //becomes reference of existing record
             dc.Employees.DeleteOnSubmit(obj);
             dc.SubmitChanges();
             LoadData();
         }
     }
     else
     {
         MessageBox.Show("Please select a record to delete.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
Example #4
0
 private void Button3_Click(object sender, EventArgs e)
 {
     if (dgView.SelectedRows.Count > 0)
     {
         if (MessageBox.Show("Are you sure you want to delete this particular row", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
         {
             int      Eno = Convert.ToInt32(dgView.SelectedRows[0].Cells[0].Value); // this converts the object to integer
             Employee obj = dc.Employees.SingleOrDefault(E => E.Eno == Eno);        // here now the obj becomes the refernce of the existing record
             dc.Employees.DeleteOnSubmit(obj);                                      // this deletes the record on a pending delete state, that is it hasnt commited the data
             dc.SubmitChanges();                                                    // this will commit the data
             MessageBox.Show("The record has been sucessfully deleted!!", "Success!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
             LoadData();
         }
     }
     else
     {
         MessageBox.Show("Select the rows you want to delete first then only click Update", "There was an Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }