Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Querying by string field fails in VB .Net #767

Closed
Trobeloo opened this issue Feb 13, 2021 · 13 comments
Closed

Bug: Querying by string field fails in VB .Net #767

Trobeloo opened this issue Feb 13, 2021 · 13 comments
Assignees
Labels
bug Something isn't working deployed Feature or bug is deployed at the current release fixed The bug, issue, incident has been fixed.

Comments

@Trobeloo
Copy link

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:

   '        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](Expression`1 expression)
        '   at RepoDb.DbConnectionExtension.ToQueryGroup[TEntity](Expression`1 where)
        '   at RepoDb.DbConnectionExtension.Query[TEntity](IDbConnection connection, Expression`1 where, IEnumerable`1 fields, IEnumerable`1 orderBy, Nullable`1 top, String hints, String cacheKey, Nullable`1 cacheItemExpiration, Nullable`1 commandTimeout, IDbTransaction transaction, ICache cache, ITrace trace, IStatementBuilder statementBuilder)
        '   at TestRepoDB.Module1.Main() in D: \Data\TestRepoDB\TestRepoDB\Module1.vb:line 17

Schema and Model:

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

And also the model that corresponds the schema.

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

@Trobeloo Trobeloo added the bug Something isn't working label Feb 13, 2021
@mikependon mikependon changed the title Querying by string field fails in VB .Net Bug: Querying by string field fails in VB .Net Feb 13, 2021
@Trobeloo
Copy link
Author

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.Parse[TEntity](Expression1 expression) in D:\Data\repos\mikependon\RepoDB\RepoDb.Core\RepoDb\QueryGroup\ParseExpression.cs:line 66 at RepoDb.DbConnectionExtension.ToQueryGroup[TEntity](Expression1 where) in D:\Data\repos\mikependon\RepoDB\RepoDb.Core\RepoDb\Extensions\DbConnectionExtension.cs:line 2456
at RepoDb.DbConnectionExtension.Query[TEntity](IDbConnection connection, Expression1 where, IEnumerable1 fields, IEnumerable1 orderBy, Nullable1 top, String hints, String cacheKey, Nullable1 cacheItemExpiration, Nullable1 commandTimeout, IDbTransaction transaction, ICache cache, ITrace trace, IStatementBuilder statementBuilder) in D:\Data\repos\mikependon\RepoDB\RepoDb.Core\RepoDb\Operations\DbConnection\Query.cs:line 538
at TestRepoDB.Module1.Main() in D:\Data\TestRepoDB\TestRepoDB-VB\Module1.vb:line 17

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

@mikependon
Copy link
Owner

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

@mikependon
Copy link
Owner

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?

@mikependon
Copy link
Owner

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.

@Trobeloo
Copy link
Author

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

@mikependon
Copy link
Owner

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

@Trobeloo
Copy link
Author

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

@mikependon
Copy link
Owner

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.

@mikependon
Copy link
Owner

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

@mikependon mikependon added the fixed The bug, issue, incident has been fixed. label Feb 19, 2021
@mikependon
Copy link
Owner

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

@mikependon
Copy link
Owner

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

@mikependon mikependon added the deployed Feature or bug is deployed at the current release label Feb 19, 2021
@Trobeloo
Copy link
Author

Problem is fixed with the beta
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working deployed Feature or bug is deployed at the current release fixed The bug, issue, incident has been fixed.
Projects
None yet
Development

No branches or pull requests

2 participants