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

Unable to Insert/Update Record using Expando Object in VB.NET #719

Open
rizalmart opened this issue Feb 8, 2024 · 7 comments
Open

Unable to Insert/Update Record using Expando Object in VB.NET #719

rizalmart opened this issue Feb 8, 2024 · 7 comments

Comments

@rizalmart
Copy link

PetaPoco threw error message when inserting/updating records using expando object. It requires to manually create a class that represents the the row in order to insert it. This was hassle for developers to create a class which represents a row before inserting/updating records because it adds more time and lines of code

@asherber
Copy link
Collaborator

asherber commented Feb 8, 2024

Can you use an anonymous type in your code? That avoids having to create a concrete class, and it's probably no more code than populating an expando.

@rizalmart
Copy link
Author

Can you use an anonymous type in your code? That avoids having to create a concrete class, and it's probably no more code than populating an expando.

Do you have an idea on how to declare anonymous types in vb.net and how to use it on PetaPoco?

@iadaz
Copy link
Collaborator

iadaz commented Feb 9, 2024

PetaPoco threw error message when inserting/updating records using expando object. It requires to manually create a class that represents the the row in order to insert it. This was hassle for developers to create a class which represents a row before inserting/updating records because it adds more time and lines of code

There's an example of inserting with an expando here: https://github.com/CollaboratingPlatypus/PetaPoco/wiki/Support-for-dynamic
Does it throw an error if you follow this pattern?

@asherber
Copy link
Collaborator

asherber commented Feb 9, 2024

@iadaz Inserts with dynamics appears to be broken at the moment. There is a pending PR which I think addresses this.

@rizalmart In C# the code would look something like this:

var newRecord = new {
    field1 = "foo",
    field2 = "bar"
};
db.Insert("my_table", newRecord);

I don't work in VB, but here are the relevant docs on anonymous types.

@rizalmart
Copy link
Author

@iadaz Inserts with dynamics appears to be broken at the moment. There is a pending PR which I think addresses this.

@rizalmart In C# the code would look something like this:

var newRecord = new {
    field1 = "foo",
    field2 = "bar"
};
db.Insert("my_table", newRecord);

I don't work in VB, but here are the relevant docs on anonymous types.

I follow this method here from petapoco

`// Create a new record
dynamic a = new ExpandoObject();
a.title = "My New Article";

// Insert it
db.Insert("articles", "article_id", a);

// New record ID returned with a new property matching the primary key name
Console.WriteLine($"New record {a.article_id} inserted");`

But it only threw error message that it does not allow expando object on insert

@asherber
Copy link
Collaborator

asherber commented Feb 9, 2024

Yes, as I said, I think there's currently a bug. The code I gave above is for using anonymous types instead of Expandos. You can see that it's not much work, about the same as Expandos.

@mercurial-moon
Copy link

Hi,

I was facing a similar problem, but in C#, understand there is a bug but just wondering if there is a workaround.

So I was populating a ExpandoObject in a loop

IDictionary<string, object> row = new ExpandoObject();
for (int c = 0; c < colNames.Count; c++) {
    row.Add(colNames[c], rowData[c]);
}

the column names of the table are in the colNames list and the data for the row is in rowData.
Then I insert this into the database using

db.Insert(tableName, "id", true, row)

but this throws an Exception

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Can I use the looping technique on an anonymous object? or is there any other way I get the data into the database from the 2 lists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants