Repodb: Bug: Querying by string field fails in VB .Net

Created on 13 Feb 2021  路  13Comments  路  Source: mikependon/RepoDB

Bug Description

The following line raise Object reference Not Set To an instance Of an Object.
Dim myInvoice = Connection.Query(Of Factures)(Function(f) f.Country = "France").First

This line is working ok:
Dim myInvoice = Connection.Query(Of Factures)(Function(f) f.Id_Facture = 13624).First

Exception Message:

``VB .net ' System.NullReferenceException ' HResult = 0x80004003 ' Message = Object reference Not Set To an instance Of an Object. ' Source = RepoDb 'StackTrace: ' at RepoDb.QueryGroup.<> c__DisplayClass61_0.<GetFields>b__0(QueryGroup queryGroup) ' at RepoDb.QueryGroup.<> c__DisplayClass61_0.<GetFields>b__0(QueryGroup queryGroup) ' at RepoDb.QueryGroup.GetFields(Boolean traverse) ' at RepoDb.QueryGroup.Fix() ' at RepoDb.QueryGroup.Parse[TEntity](Expression1 expression)
' at RepoDb.DbConnectionExtension.ToQueryGroupTEntity
' at TestRepoDB.Module1.Main() in D: DataTestRepoDBTestRepoDBModule1.vb:line 17


**Schema and Model:**

CREATE TABLE [dbo].[Factures](
    [Id_Facture] [int] NOT NULL,
    [Country] [varchar](30) NULL,
CONSTRAINT [PK_Factures] PRIMARY KEY CLUSTERED 

And also the model that corresponds the schema.

```VB Class
Public Class Factures
    Public Property Id_Facture() As Integer
    Public Property Country() As String
    Public Property Company_Num_Compta As Integer
End Class

Library Version:

RepoDb v1.12.8 and RepoDb.SqlServer v1.1.3

Fixed bug deployed

All 13 comments

My problem seems to be simmilar to the one described here : https://stackoverflow.com/questions/2413046/lambda-expressions-in-vb-net-what-am-i-doing-wrong

I tried to use Equals:
Dim myInvoice = Connection.Query(Of Factures)(Function(f) f.Country.Equals("France")).First

But it throw a not supported exception:

System.NotSupportedException
HResult=0x80131515
Message=Expression 'f => f.Country.Equals("France")' is currently not supported.
Source=RepoDb
StackTrace:
at RepoDb.QueryGroup.ParseTEntity in D:DatareposmikependonRepoDBRepoDb.CoreRepoDbExtensionsDbConnectionExtension.cs:line 2456
at RepoDb.DbConnectionExtension.QueryTEntity in D:DatareposmikependonRepoDBRepoDb.CoreRepoDbOperationsDbConnectionQuery.cs:line 538
at TestRepoDB.Module1.Main() in D:DataTestRepoDBTestRepoDB-VBModule1.vb:line 17

Is there a solution or should I consider that RepoDb won't work with VB .Net.

The Equals is not parsed by RepoDB if I remember. Can you try this f => f.Country == "France"?

Apology, I am bit occupied recently, a preparation for the talk. Can you help me here. Do you have a very small Console application that could mimic this? A one that is written in VB.NET?

If you as well can attached the SQL Script for Schema, that would be better. Just attached it here is zip file and I will give priority to it by tomorrow, then probably issue a beta if I find it really a bug. Thanks

Note: Your problem is a very common case, which I pre-assumed well supported by RepoDB. Of course, I had not tested that in VB.NET yet.

Thank you Mike
Creating a database is not requiered since the error is in the parse of the query tree.
f => f.Country == "France" is a C# lambda expression the VB equivalent is Function(f) f.Country = "France"
I made a C# equivalent project and of course it works.

To reproduce create a console app in VB then paste the following code in the Module1.vb file

Module Module1

Dim ConStr As String
Dim Connection As SqlConnection

Sub Main()
    RepoDb.SqlServerBootstrap.Initialize()
    ConStr = "Data Source=srv;Initial Catalog=Database;Integrated Security=False;Persist Security Info=True;User ID=sa;Password=pwd;MultipleActiveResultSets=True;Application Name=Tests;"
    Connection = New SqlConnection(ConStr)

    'NOK Null Reference Exception
    Dim myInvoice = Connection.Query(Of Factures)(Function(f) f.Country = "France").First

    'NOK Not Supported Exception
    'Dim myInvoice = Connection.Query(Of Factures)(Function(f) f.Country.Equals("France")).First

    'Ok
    'Dim myInvoice = Connection.Query(Of Factures)(Function(f) f.Id_Facture = 13624).First

    'Ok
    'Dim myInvoice = Connection.Query(Of Factures)(Function(f) f.Company_Num_Compta = 4113427).First

    Console.WriteLine($"Invoice: {myInvoice.Id_Facture} Country: {myInvoice.Country}")

End Sub

End Module
Public Class Factures
Public Property Id_Facture() As Integer
Public Property Country() As String
Public Property Company_Num_Compta As Integer
End Class

Jacques

Will get back to you probably EOW together with a new beta build. Is that okay with you?

It is ok. I am in the process of testing RepoDB to replace Dapper in our internal apps.

Hi Mike,

If it helps I found similar/related problems here (https://letmegooglethat.com/?q=linq+lambda+expression+vb+net+comparestring):

Jacques

Initial investigation result, it seems to me that the expression you used is using the CompareString method underneath, in which is internal to VB.Net.

image

Let me see how I can handle this.

While the issue is being fixed, can you for now utilize the Query Object like below?

Dim myInvoice = Connection.Query(Of Factures)(New QueryField("Country", "France")).FirstOrDefault

Referencing the actual fix from here. Was tagged wrongly to the other issue.

The fix is now available at v1.12.8-beta2.

Problem is fixed with the beta
Thank you

Was this page helpful?
0 / 5 - 0 ratings