Delete Data(DB First Approach)

We want to delete record by using user id . Here is the step required .


Double click the delete button to generate the event handler. Copy and paste following codes .

      private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("Please provide information");
                return;
            }
            else
            {

                Cashir ca = Setinfromation(Convert.ToInt32(textBox1.Text));
                bool result = DeleteUserRecord(ca);
                label3.Text = "Record is deleted";
                MessageBox.Show("Record is deleted");
            }
        }

        public bool DeleteUserRecord(Cashir ca)
        {

            bool result = false;
            using (UserEntities user_entity = new UserEntities())
            {
                Cashir cashir = user_entity.Cashirs.Where(x => x.User_Id == ca.User_Id).Select(x => x).FirstOrDefault();
                user_entity.Cashirs.Remove(cashir);
                user_entity.SaveChanges();
                result = true;
            }
            return result;
        }

        public Cashir Setinfromation(int User_Id) //Setvalues method for binding field values to StudentInformation Model class
        {
            Cashir ca = new Cashir();
            ca.User_Id = User_Id;
           

            return ca;
        }
    }
}






No comments:

Post a Comment