0

I saw other similar questions, but I can't manage to apply other solutions.

Here is the code

Dim da1 As DateTime = DateTime.ParseExact(Date.Today, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Dim dataQuery As String = da1.ToString("dd-MM-yyyy", CultureInfo.InvariantCulture).PadLeft(10)
Dim errori = db.TRANSAZERROR.Where(Function(t) t.DATA.PadLeft(10) = dataQuery)

transazerror.data is a nullable varchar field (yes it's not a datetime) and it also contain a time, so I'm using padleft to get only the date in the format dd-MM-yyyy.

What I want is to select all record that have data=today so the errori var should be a list of records, but the code above returns

Nullreferenceexception

I don't know why.

I'm using EF 6.

Thanks for your hints!

Exception detail:

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Riferimento a un oggetto non impostato su un'istanza di oggetto.
  Source=transazErrorLog
  StackTrace:
       in transazErrorLog.Form1.log() in I:\Documenti\Vs15_Projects\transazErrorLog\transazErrorLog\Form1.vb:riga 22
       in transazErrorLog.Form1.Form1_Load(Object sender, EventArgs e) in I:\Documenti\Vs15_Projects\transazErrorLog\transazErrorLog\Form1.vb:riga 9
       in System.EventHandler.Invoke(Object sender, EventArgs e)
       in System.Windows.Forms.Form.OnLoad(EventArgs e)
       in System.Windows.Forms.Form.OnCreateControl()
       in System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       in System.Windows.Forms.Control.CreateControl()
       in System.Windows.Forms.Control.WmShowWindow(Message& m)
       in System.Windows.Forms.Control.WndProc(Message& m)
       in System.Windows.Forms.Form.WmShowWindow(Message& m)
       in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException: 

EDIT: I tried to use another filed of the db, it's a date field set to nullable=false, even with this I get the nullreference exception... I turn the question around: how can I get a list of record using entity framework?

  • EF6 does not support `PadLeft` method, hence the exception would be different. This sounds more like EF Core client evaluation exception. You'd better include the exception stack trace, otherwise the question will be closed as the usual NRE duplicate. – Ivan Stoev Feb 15 '18 at 09:33
  • Looks like the problem is in your code (`db` variable being `null`), voting to close. – Ivan Stoev Feb 15 '18 at 09:49
  • I tried with this Dim errori = (From errors In db.TRANSAZERROR Where errors.DATASCARICO = Date.Today).ToList() but I still get NRE. Datascarico is not nullable and it's a date type – Gabriele Cozzolino Feb 15 '18 at 10:12

2 Answers2

1

You might check transazerror.data is not null on where statement first.

You can use String.IsNullOrEmpty method

like this

Not String.IsNullOrEmpty(t.DATA) AND t.DATA.PadLeft(10) = dataQuery
D-Shih
  • 44,943
  • 6
  • 31
  • 51
0

Since you are saying TRANSAZERROR.DATA is nullable, imagine you have a null value there in one of the records. Now when you try to do PadLeft on NULL, it will throw your error.

The solution, I can suggest is before doing PadLeft inside your LINQ query, first check whether t.DATA is null and IF NOT perform the PadLeft.

Jaliya Udagedara
  • 1,087
  • 10
  • 16