简体   繁体   中英

Unable to pass null parameter in winform's event handler?

i want to subsequently apply filters to my search Say " if I give 10K as the myFee value it should show all subjects with 10K Fees however if after this I give year 2014 it should show results applying both fee and year constraint simultaneously

currently, I am getting a correct result if I give values on both of my comboBoxes at the same time (Fee & Year) [IMAGE IN BELOW] https://imgur.com/a/dYpCeeP

Here we are calling GetStudentsByYearEF with parameters but if i try to left any of the input null it will give error while passing as parameter.

            string _readYear = _yearInputBox?.SelectedItem.ToString();
            var _readFee = _feeInputBox?.SelectedItem.ToString();
            var readfee = Convert.ToInt32(_readFee);
            if (_readYear != string.Empty && readfee != null && readfee > 0)
            {
                var students = AcademyHelper.GetStudentsByYearEF(readfee, _readYear, this.dataGridView2, this, this.groupBox5);
                this.dataGridView2.DataSource = students;
            }

Definition of GetStudentsByYearEF()

    public static IEnumerable<Student> GetStudentsByYearEF(int fee, string year, DataGridView dtg, Form form, GroupBox groupbox)
    {
        List<Student> searchedStudent = new List<Student>();
        using (var retrive = new Models.Academy_MSDBEntities())
        {
            foreach (Control items in groupbox.Controls)
            {
                //removed .tolower() from here
                if (items.Name.AcademyContains("_courseInputBox") || items.Name.AcademyContains("_feeInputBox"))
                {
                    var query = retrive.Students
                     .Where(s => s.Year == year && s.Fees==fee).ToList();
                    searchedStudent.AddRange(query);
                    retrive.SaveChanges();
                }
            }
            return searchedStudent;
        }
    }

If want to subsequently apply filters on the search. however, it applies to search at once on pressing the search button but if I try to apply only Fee constraint on the search and leaving the Year constraint empty I gives me exception instantly.

Your where clause checks for both year and fee if items.Name.AcademyContains("_courseInputBox") || items.Name.AcademyContains("_feeInputBox") items.Name.AcademyContains("_courseInputBox") || items.Name.AcademyContains("_feeInputBox")

So if your Academy contains the courseinputbox OR feeinputbox it will run the where clause. So if Academy contains courseinputbox but doesnt contain feeinputbox your where caluse will try and filter with the fee value.

You need to add a null check for fee before running your where clause. Something like:

if(fee != null){
                 var query = retrive.Students
                 .Where(s => s.Year == year && s.Fees==fee).ToList();
                searchedStudent.AddRange(query);
                retrive.SaveChanges();
}else{                                                              
               var query = retrive.Students
                 .Where(s => s.Year == year).ToList();
                searchedStudent.AddRange(query);
                retrive.SaveChanges();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM