Click here to Skip to main content
14,920,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am working in an application in VB.NET. I got stuck in this ERROR:

System.NotSupportedException
HResult=0x80131515
Message=The member ClientId of type BillTimeLibrary.BillTimeLibrary.Models.ClientModel cannot be used as a parameter value


Contextualizing...
First, I have this same application working in C#. I am converting to VB.NET to learn and understand how to use SQLite Database with Dapper with these both languages.

The error appears in the pass-through of a parameter from a sub routine that connects two tables from my database.
The Payments table has a Foreing Key named ClientId, which identifies which client the payment is from. In the payments window I have a Combobox where I choose the client, and if there is any payment already made the dates of the payments is listed in another Combobox.
That query is not working, when I choose the client the program should fetch the Payments in the Tablet Payments by the ClientId, but it gives that error that I mentioned above.
The subroutine that should pass the parameters for query is:

VB
Private Sub LoadDateDropDown()
        Dim sql As String = "select * from Payment where ClientId = @ClientId"
        Dim parameters As Dictionary(Of String, Object) = New Dictionary(Of String, Object) From {
                {"@ClientId", ClientDropDown.SelectedValue}
            }
        Dim records = SQLiteDataAccess.LoadData(Of PaymentsModel)(sql, parameters)
        payments.Clear()
        records.ForEach(Sub(x) payments.Add(x))
    End Sub


the message is viewed in the Load Sub which receives the parameters.


The parameter it is mentioned in the message is created in a Model: it has the same type of variable used in the Database Table.

VB
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks


Namespace BillTimeLibrary.Models
    Public Class PaymentsModel
        Public Property Id As Integer
        Public Property ClientId As Integer
        Public Property Hours As Double
        Public Property Amount As Double
        Public Property DatePayment As String
    End Class
End Namespace



I think I might be using the wrong property of the ClientDropDown (my combobox)

See the original code in the C#:


private void LoadDateDropDown()
        {
            string sql = "select * from Payment where ClientId = @ClientId";

        Dictionary<string, object> parameters = new Dictionary<string, object>
        {
            { "@ClientId", ClientDropDown.SelectedValue }
        };

        var records = SQLiteDataAccess.LoadData<PaymentsModel>(sql, parameters);

        payments.Clear();
        records.ForEach(x => payments.Add(x));
    }


What I have tried:

I am trying to see how to access the payments table through the ClientId Field. No success yet.

This is the FULL Details about the error.. it seems to be something with Dapper. But As I am new with it I do not know how to solve yet.

System.NotSupportedException
HResult=0x80131515
Message=The member ClientId of type BillTimeLibrary.BillTimeLibrary.Models.ClientModel cannot be used as a parameter value
Source=Dapper
StackTrace:
at Dapper.SqlMapper.LookupDbType(Type type, String name, Boolean demand, ITypeHandler& handler) in C:\projects\dapper\Dapper\SqlMapper.cs:line 417
at Dapper.DynamicParameters.AddParameters(IDbCommand command, Identity identity) in C:\projects\dapper\Dapper\DynamicParameters.cs:line 238
at Dapper.DynamicParameters.Dapper.SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, Identity identity) in C:\projects\dapper\Dapper\DynamicParameters.cs:line 156
at Dapper.SqlMapper.<>c__DisplayClass161_0.<getcacheinfo>b__0(IDbCommand cmd, Object obj) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1694
at Dapper.CommandDefinition.SetupCommand(IDbConnection cnn, Action`2 paramReader) in C:\projects\dapper\Dapper\CommandDefinition.cs:line 128
at Dapper.SqlMapper.<queryimpl>d__138`1.MoveNext() in C:\projects\dapper\Dapper\SqlMapper.cs:line 1075
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 723
at BillTimeLibrary.SQLiteDataAccess.LoadData[T](String sqlStatement, Dictionary`2 parameters, String connectionName) in C:\BillTime_VB\BillTimeLibrary\DataAccess\SQLiteDataAccess.vb:line 25
at BillTime_VB.Payments.LoadDateDropDown() in C:\BillTime_VB\BillTime_VB\Controls\Payments.vb:line 141
at BillTime_VB.Payments.ClientDropDown_SelectedIndexChanged(Object sender, EventArgs e) in C:\BillTime_VB\BillTime_VB\Controls\Payments.vb:line 133
at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)
at System.Windows.Forms.ComboBox.RefreshItems()
at System.Windows.Forms.ComboBox.OnDataSourceChanged(EventArgs e)
at System.Windows.Forms.ListControl.SetDataConnection(Object newDataSource, BindingMemberInfo newDisplayMember, Boolean force)
at System.Windows.Forms.ListControl.set_DataSource(Object value)
Posted
Updated 22-Apr-19 5:04am
v5
Comments
Dave Kreskowiak 22-Apr-19 10:17am
   
We couldn't tell you as the problem lies in/behind SQLiteDataAccess. That's not a normal type so it has to be something in you code somewhere.

What's the "type" of ClientId?

From the looks of it, you want to load "PaymentsModel" using a "ClientId" from the "ClientModel".

It seems to be pretty clear that it doesn't like the "type" you're passing in.
   
Comments
Member 13954123 22-Apr-19 9:58am
   
It is Integer
Richard Deeming 24-Apr-19 18:17pm
   
Not according to the error message:
"The member ClientId of type BillTimeLibrary.BillTimeLibrary.Models.ClientModel ...
A couple of hints that may help:

1) Google the error : HResult=0x80131515 and you will find that it leads you to the error : "Could not load file or assembly". Of course, the question is why can't it load the file or assembly?

2) Take a very close look at two lines from your source -- the error is actually pointing to them as the original cause:

1. Controls\Payments.vb:line 133
2. Controls\Payments.vb:line 141

Post those two lines here and I may be able to help more.
   
Comments
Member 13954123 22-Apr-19 11:14am
   
I copied the entire block that includes these two lines.

127 Private Sub ClientDropDown_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ClientDropDown.SelectedIndexChanged
128 Label2.Visible = True
129 dateDropDown.Visible = True
130 OrTextBox.Visible = True
131 newButton.Visible = True
132
133 LoadDateDropDown()
134 End Sub
135
136 Private Sub LoadDateDropDown()
137
138 Dim sql As String = "select * from Payment where ClientId = @ClientId"
139 Dim parameters As Dictionary(Of String, Object) = New Dictionary(Of String, Object) From {
140 {"@ClientId", ClientDropDown.ValueMember}'SelectedValue}
141 }
142
143 Dim records = SQLiteDataAccess.LoadData(Of PaymentsModel)(sql, parameters)
144 payments.Clear()
145 records.ForEach(Sub(x) payments.Add(x))
146 End Sub
Member 13954123 22-Apr-19 11:22am
   
payments is declared in the beginning of the code as

Private payments As ObservableCollection(Of PaymentsModel) = New ObservableCollection(Of PaymentsModel)()
A couple of hints that may help:

1) Google the error : HResult=0x80131515 and you will find that it leads you to the error : "Could not load file or assembly". Of course, the question is why can't it load the file or assembly?

2) Take a very close look at two lines from your source -- the error is actually pointing to them as the original cause:

1. Controls\Payments.vb:line 133
2. Controls\Payments.vb:line 141

Post those two lines here and I may be able to help more.

3) I also notice that the error states:
The member ClientId of type BillTimeLibrary.BillTimeLibrary.Models.ClientModel cannot be used as a parameter value.

But you have shown us the class :
BillTimeLibrary.Models
Public Class PaymentsModel

Wonder if you're referencing the value in the wrong type??
   
Comments
Member 13954123 22-Apr-19 11:16am
   
I found this strange too. This may be the problem. The variable ClientID is in the PaymentModel. I will check it.
raddevus 22-Apr-19 11:42am
   
It's difficult to determine what is going on. But I do have two suggestions (maybe you've already tried them?).
1) replace the SelectedValue with a hard-coded value (possibly one you know is valid) and try running.
change => { "@ClientId", ClientDropDown.SelectedValue }
to => { "@ClientId", 5 }

2) Run in debug mode step into LoadDateDropDown and press F11 on each line (step into) to determine exactly where the code is failing.
Member 13954123 22-Apr-19 12:32pm
   
Actually I haven't tried these options before reading your comment.

Did (1), and it works when passing a number instead of the "ClientDropDown.SelectedValue".

In (2), the code fail in the line 25 of my SQLiteDataAccess Class, which is the line that use the parameters passed (@ClientId, Value) to access the DataBase. The Id is not being passed correctly from the dropdown.


10Public Class SQLiteDataAccess
11 'generic code to load data
12 'LoadData<personmodel>("Select * from Person", null) = List<personmodel>
13 Public Shared Function LoadData(Of T)(ByVal sqlStatement As String, ByVal parameters As Dictionary(Of String, Object), ByVal Optional connectionName As String = "Default") As List(Of T)
14
15 Dim p As DynamicParameters = New DynamicParameters()
16 'DynamicPirameters is a Dapper Type
17
18 For Each param In parameters
19 '@FirstName, "Tim"
20 p.Add(param.Key, param.Value)
21 Next
22
23
24 Using cnn As IDbConnection = New SQLiteConnection(DataAccessHelpers.LoadConnectionString(connectionName))
25 Dim rows = cnn.Query(Of T)(sqlStatement, p)
26 Return rows.ToList()
27 End Using
28
29 End Function
30
Member 13954123 22-Apr-19 12:41pm
   
I am not understanding why the message error refers to CLientModel. The code seems to be referring to the PaymentModel as I look.
raddevus 22-Apr-19 13:24pm
   
Since it works with the hard-coded ID value (a known integer) then the problem is probably with how the value is being retrieved from the droplist. Have you set DisplayMember and ValueMember of the droplist (combobox, or whatever that is) properly?
For example of what I mean see:
https://stackoverflow.com/questions/9521980/combobox-valuemember-and-displaymember
Member 13954123 22-Apr-19 13:52pm
   
I think so. I have set just like your example.

dateDropDown.DataSource = payments '.ItemsSource = payments
dateDropDown.DisplayMember = "DatePayment" '.DisplayMemberPath = "Date"
dateDropDown.ValueMember = "Id"

But I also set the client combobox to:
ClientDropDown.ItemsSource = clients
ClientDropDown.DisplayMemberPath = "Name"
ClientDropDown.SelectedValuePath = "Id"

Maybe is this the cause?
raddevus 22-Apr-19 14:22pm
   
Yes, I think that is the cause. I don't have a lot of info to help you fix that though.
Is this a WPF app? I thought it was WinForms.
If it is WPF there is a very good explanation here at CP about this databinding like you are attempting: https://www.codeproject.com/Articles/671544/Understanding-SelectedValue-SelectedValuePath-Sele

it's short and may help.
Member 13954123 22-Apr-19 15:26pm
   
It is a WindowsForm App.


Maybe I made some mistake, because the original app (C#) I mentioned is WPF, and my VB.NET application is WinForm. I was a little confused about which properties have the same meaning in the WinForm, since is no exactly the same names.
raddevus 22-Apr-19 15:37pm
   
Yeah, since it's a winform app try using just the DataSource, DisplayMember and ValueMember properties on that clientDropDown and then it may work -- depending upon a number of other things. it can be a bit tricky getting it just right.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900