linq query giving message "Value cannot be null"

Anthony Matovu
Anthony Matovu used Ask the Experts™
on
Dear Experts,

The code below has come a long way and i am already proud of it. However i am getting a message    "Value cannot be null" of the line that starts with Dim query = From.... I really dont know how to about this .

Thank you

Dim tbl_time As DataTable = ds.Tables("ctime")
        Dim tbl_perhour As DataTable = ds.Tables("perhour")
        Dim tbl_area As DataTable = ds.Tables("areas")
        Dim tbl_basestation As DataTable = ds.Tables("station")
        Dim tbl_country As DataTable = ds.Tables("country")
        Dim tbl_ded As DataTable = ds.Tables("ded")

        Application.DoEvents()

                Dim query = From obj_perhour In tbl_perhour.AsEnumerable() Join obj_time In tbl_time.AsEnumerable() _
                    On obj_perhour.Field(Of Int64)("time_key") Equals obj_time.Field(Of Int64)("time_key") _
                    Group obj_perhour By customerid = obj_perhour.Field(Of Int64)("msisdn_nsk") _
                                     Into grp = Group _
                       Select New With _
                            { _
                              .msisdn = customerid,
                              .rev_onn_tot_nbn = grp.Sum(Function(x) If(x.Field(Of String)("utype") = "UG", x.Field(Of Decimal)("ccost"), 0))
                             }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Anthony MatovuBusiness Analyst, MTN Uganda

Author

Commented:
I have searched for help and made option changes from page

And now the error is at line

  public static EnumerableRowCollection<DataRow> AsEnumerable(this DataTable source)
        {
            DataSetUtil.CheckArgumentNull(source, "source");
           return new EnumerableRowCollection<DataRow>(source);
        }

datatableextensions.cs
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Going back to the original post before the changes you made that change the location of the error. Try running the application and place a breakpoint on the statement after the query and post the actual exception and inner exception that is thrown. That type of message is normally sent when a collection / object is being processed and it is expecting a value but there is none.
Anthony MatovuBusiness Analyst, MTN Uganda

Author

Commented:
Hi, Below is the detailed massage i get

Thank you

System.ArgumentNullException was unhandled
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: source
  ParamName=source
  Source=System.Data.DataSetExtensions
  StackTrace:
       at System.Data.DataTableExtensions.AsEnumerable(DataTable source)
       at MTN_business_intelligence_solutions.mainfrm.Button1_Click(Object sender, EventArgs e) in C:\bi\MTN business intelligence solutions\mainfrm.vb:line 53
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at MTN_business_intelligence_solutions.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Fernando SotoRetired
Distinguished Expert 2017

Commented:
What line in the code is it throwing the exception?
Anthony MatovuBusiness Analyst, MTN Uganda

Author

Commented:
Dim query = From obj_perhour In tbl_perhour.AsEnumerable() Join obj_time In tbl_time.AsEnumerable() _
                    On obj_perhour.Field(Of Int64)("time_key") Equals obj_time.Field(Of Int64)("time_key") _
                    Group obj_perhour By customerid = obj_perhour.Field(Of Int64)("msisdn_nsk") _
                                     Into grp = Group _
                       Select New With _
                            { _
                              .msisdn = customerid,
                              .rev_onn_tot_nbn = grp.Sum(Function(x) If(x.Field(Of String)("utype") = "UG", x.Field(Of Decimal)("ccost"), 0))
                             }
Anthony MatovuBusiness Analyst, MTN Uganda

Author

Commented:
Dear Fernando,

Seems i am figuring out the cause of the problem.  utype was not properly spelt during table loading  and is likely to be the cause of the problem. it is now running and i waiting to see progress.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi MatovuAnthony;

That seems to be strange seeming all values would not of match and then returned a 0, zero, but should not have caused it to throw that type of exception.

I was thinking that one of the Int64 values was a null value and the reason I stated this in my first post, "That type of message is normally sent when a collection / object is being processed and it is expecting a value but there is none."
Anthony MatovuBusiness Analyst, MTN Uganda

Author

Commented:
Fortunately it run after i checked all spelling for my columns

Thank you for the help so far

Anthony
Retired
Distinguished Expert 2017
Commented:
OK, that is different then your previous statement where you stated, "utype was not properly spelt ", which is a data type and was comparing it to a string.  This is the reason for my last statement and why I stated this, "I was thinking that one of the Int64 values was a null value ", which seems to have been the case because if the column name was misspelled the return value would have been a null and the reason why it threw the error.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi MatovuAnthony, did you understand my last post?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial