How's treating Exception's Windows Form?



  • I have a WinForms form. All data retained in OBD SQLand the data formats are different. How to process exemptions (for each type) that were short for all controllers (minimum 20-25) of this form when compressed Save Button

      private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                BandivanKatDataContext db = new BandivanKatDataContext();
                Animals newAnimal = new Animals();
                newAnimal.number = int.Parse(textBox1.Text);
                newAnimal.name = textBox2.Text;
                newAnimal.gender = textBox3.Text;
                newAnimal.age_Group = textBox4.Text;
                newAnimal.birthday = DateTime.Parse(textBox5.Text);
                newAnimal.breed = textBox7.Text;
                newAnimal.milk_volume = textBox6.Text;
                newAnimal.genetic = textBox17.Text;
                newAnimal.weghit = int.Parse(textBox18.Text);
                newAnimal.count_of_child = int.Parse(textBox19.Text);
                //
                db.Animals.InsertOnSubmit(newAnimal);
                db.SubmitChanges();
                //
                MotherAnimal newMotherAnimal = new MotherAnimal();
                newMotherAnimal.motherNumber = int.Parse(textBox8.Text);
                newMotherAnimal.name = textBox9.Text;
                newMotherAnimal.birthday = DateTime.Parse(textBox10.Text);
                newMotherAnimal.genetic = textBox16.Text;
                newMotherAnimal.number = int.Parse(textBox1.Text);
    
            //
            db.MotherAnimal.InsertOnSubmit(newMotherAnimal);
            db.SubmitChanges();
            //
            FatherAnimal newFatherAnimal = new FatherAnimal();
            newFatherAnimal.fatherNumber = int.Parse(textBox11.Text);
            newFatherAnimal.name = textBox12.Text;
            newFatherAnimal.birthday = DateTime.Parse(textBox13.Text);
            newFatherAnimal.genetic = textBox14.Text;
            newFatherAnimal.weghit = int.Parse(textBox15.Text);
            newFatherAnimal.number = int.Parse(textBox1.Text);
    
            db.FatherAnimal.InsertOnSubmit(newFatherAnimal);
            db.SubmitChanges();
            //
        }
        catch(Exception)
        {
            MessageBox.Show("Մուտքագրեք ճիշտ տվյալ");                
        }
    }
    




    1. Give all the counterfeit names, for example, if in TextBox Name, name. textBoxName, if the porridge is rolling, call it textBoxBreed etc. It will be easy to read and understand the code.

    2. Use more suitable contrasts. For example, for the introduction of the numbers - NumericUpDown - it won't automatically let anything be introduced except the numbers. Use the dates for entry. DateTimePickerfor the introduction of one of the known values (such as sex) - ComboBox etc. In doing so, it would be easier and faster for the user to enter data, and many errors could be eliminated automatically.

    3. Instruction errors may be used to signal to the user https://msdn.microsoft.com/ru-ru/library/system.windows.forms.errorprovider%28v=vs.100%29.aspx ♪

    4. Don't catch a basic exception. Exception♪ Get in. specific types of exceptions. Unfortunately, in MSDN, the documentation on Linq2Sql is very scarce. List of possible exemptions for implementation SubmitChanges collected https://stackoverflow.com/q/2904317/5045688 ♪
      Also catch and process any exceptions that might arise when the methods are challenged. Parse (But you better use TryParseand even better, the counterfoils, NumericUpDownDateTimePicker)

    5. Read it. https://msdn.microsoft.com/ru-ru/library/system.windows.forms.textbox.autocompletesource%28v=vs.110%29.aspx ♪ Its application will greatly assist users in the introduction (and will eliminate errors again).

    6. And for the future, use the data link-- https://msdn.microsoft.com/ru-ru/library/ef2xyb33%28v=vs.110%29.aspx ♪ Graphic use can significantly reduce the code.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2