0

I have a form with four combobxes say cbmefrom(age from ) cbmeto( age to) cbperiod(periodtype values liek this "Next 7 Days" ......) and cbgender(cbgender)..

i have a datagrid view also(dgvreports)..

what i am trying to do is i am populating the members details those whoose age is between 20 to 40 and whoose membership will expire in next 8 days or 24 days or like that ...

for that i have written one class that i have specified below...

 public static string ConvertGender(string Gender)
 {
     switch (Gender)
     {
         case "Male": return "M";
         case "Female": return "F";
         default: return "";
     }
 }
public BindingSource getmebershipexpirymembers(string gender , DateTime strtdate,DateTime enddate,DateTime min , DateTime max)
{
    bs2.DataSource = null;
    var membersreports = from report in eclipse.members
                        let dob= eclipse.members.Take(1).Select(x=>report.member_Dob).Cast<DateTime>().FirstOrDefault()
                        let strtdatees = eclipse.membertomships.Take(1).Select(x=>x.memberToMship_EndDate).Cast<DateTime>().FirstOrDefault()
                        join memtomship in eclipse.membertomships on report.member_Id equals memtomship.member_Id
                        into joinmemtomship from memtomship in joinmemtomship.DefaultIfEmpty()
                        join mshoption in eclipse.mshipoptions on memtomship.mshipOption_Id equals mshoption.mshipOption_Id
                        into joinmshipoption from mshoption in joinmshipoption.DefaultIfEmpty()
                        join membershiptypes in eclipse.mshiptypes on mshoption.mshipType_Id equals membershiptypes.mshipType_Id
                        into joinmembershipdifftypes from membershiptypes in joinmembershipdifftypes.DefaultIfEmpty()
                        join membershipstatustypes in eclipse.mshipstatustypes on memtomship.mshipStatusType_Id equals membershipstatustypes.mshipStatusType_Id
                        into joinmemberstatusdifftypes from membershipstatustypes in joinmemberstatusdifftypes.DefaultIfEmpty()                                
                        where (report.member_Gender.StartsWith(gender) || string.IsNullOrEmpty(gender))
                        && dob >= min && dob < max
                        && (strtdatees > strtdate && strtdatees < enddate)
                        select new
                        {
                            MemberID = report.member_Id,
                            Lastname = report.member_Lastname,
                            Firstname = report.member_Firstname,
                            Postcode = report.member_Postcode,
                            Reference = report.member_Reference,
                            CardNum = report.member_CardNum,
                            IsBiometric = report.member_IsBiometric,
                            DOB = report.member_Dob,
                            MShipType = membershiptypes.mshipType_Name,
                            StatusType = membershipstatustypes.mshipStatusType_Name,
                            EndDate = memtomship.memberToMship_EndDate
                        };
        bs2.DataSource = membersreports;
    return bs2;
 }

and i am accessing above class in the form that i have mentioned below...

  public void Getgroupcorporatemembers()
  {

        int startdays = 0;
        int enddays = 0;
        if (cbMeperiodType.Text == membershipexpiry.type1)
        {
            startdays = 0;
            enddays = 7;

        }

        if (cbMeperiodType.Text == membershipexpiry.type2)
        {
            startdays = 8;
            enddays = 14;

        }

        if (cbMeperiodType.Text == membershipexpiry.type3)
        {
            startdays = 15;
            enddays = 30;

        }
        if (cbMeperiodType.Text == membershipexpiry.type4)
        {
            startdays = 31;
            enddays = 90;            

        }

        DateTime today = DateTime.Now;
        DateTime strtdate = today.AddDays(startdays);
        DateTime enddate = today.AddDays(enddays);

        int agefrom = Convert.ToInt32(cbMeFrom.Text);
        int ageto = Convert.ToInt32(CbMeTo.Text);
        DateTime max = today.AddYears(-agefrom);
        DateTime min = today.AddYears(-ageto);
        string gender = "";
        gender = Classes.reportmembers.ConvertGender(cbMEGendertype.Text);

            dgvReportMembers.DataSource = objreports.getmebershipexpirymembers(gender, strtdate, enddate, max, min);// here i am accessing the method in class
            SetDgvheaders();


    }
    struct membershipexpiry
    {
        public const string type1 = "Next 7 Days";
        public const string type2 = "8 - 14 Days";
        public const string type3 = "15 - 30 Days";
        public const string type4 = "31 - 90 Days";        
    }

but I am getting the error at this line ... bs2.DataSource = membersreports;

   Error :Nullreference excpetion was unhandled 
          Object reference not set to an instance of an object.
John Saunders
  • 157,405
  • 24
  • 229
  • 388
Enigma State
  • 16,494
  • 25
  • 86
  • 179
  • @Yochai - But then the first line of the function would throw the exception. – Oded Sep 15 '11 at 18:25
  • @Yochai nope. something in the query is null. The query gets evaluated only when it's assigned to the datasource and hence the null reference exception occurs on that line. – Bala R Sep 15 '11 at 18:26
  • @Oded i am sure i am getting null reference exception at this line "bs2.DataSource = membersreports;" – Enigma State Sep 15 '11 at 18:27
  • Are you compiling in debug ? or release with optimizations ? – Yochai Timmer Sep 15 '11 at 18:31
  • @Yochai i have placed this method into button click event and when i select any of the item in "cbperiod" it gives the error at this line bs2.DataSource = membersreports; – Enigma State Sep 15 '11 at 18:35
  • @errorstacks, that's where it's happening because something in your query is null. It's really hard to tell you anything else. – msarchet Sep 15 '11 at 18:36
  • See http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net – John Saunders Sep 15 '11 at 19:18

2 Answers2

0

It's probably the query, not the bs2 variable as I initially thought.

The fields (like eclipse.mshipoption) wont raise an exception.

The Take(1) might not get a result, then when you try to cast the result it will fail.

I'd try to see what these return:

let dob = eclipse.members.Take(1)
                         .Select(x => report.member_Dob)
                         .Cast<DateTime>()
                         .FirstOrDefault()
let strtdatees = eclipse.membertomships.Take(1)
                                       .Select(x => x.memberToMship_EndDate)
                                       .Cast<DateTime>()
                                       .FirstOrDefault()
Ondrej Janacek
  • 11,896
  • 14
  • 52
  • 87
Yochai Timmer
  • 44,746
  • 21
  • 135
  • 179
  • the datatype for member_dob is string so i cant compare string with datetime for that i have done this.... let dob= eclipse.members.Take(1).Select(x=>report.member_Dob).Cast().FirstOrDefault() would you pls suggest any alternative to this... – Enigma State Sep 15 '11 at 18:49
  • Can't really think of an alternative to braking it up and debugging parts of the query – Yochai Timmer Sep 15 '11 at 18:55
  • there is no alternative to convert string to datetime using entity framework and i have tried this convert.ToDatetime() but entity framework does not support this one.... – Enigma State Sep 15 '11 at 18:58
  • You don't have to go that far. It would fail if the Take(1) doesn't return anything. Just check if Take(1) returns and object. – Yochai Timmer Sep 15 '11 at 19:14
0

I agree with Yochai, the most likely problem is the let statements in the query. For instance, if there is a null member_Dob, then the Cast method will throw a NullReferenceException.

A Linq query is not executed until it is enumerated. The datasource assignement statement will enumerate the query, but it's not telling you on what row it failed. Try to enumerate the query manually, with a try catch block and set a breakpoint in the catch block. That may help you to find the row that is causing the problem.

        foreach (var memb in membersreports)
        {
            try
            {
                Console.WriteLine(memb.DOB);
            }
            catch (Exception ex)
            {
                //set a breakpoint here or in the try block
                Console.WriteLine(ex.Message);
            }
        }
John C
  • 775
  • 9
  • 11