DynamoDB throws an error when trying to get the item count - amazon-dynamodb

I'm trying to get the total number of items in a Dynamodb table. Given below is the C# code that I use.
context = this.DynamoDBContext;
var someClassReuslts = context.Scan<SomeClass>(null);
int itemCount = someClassReuslts .Count<SomeClass>();
When I try to execute this it throws below error
"Unable to convert [Amazon.DynamoDBv2.DocumentModel.Document] of type Amazon.DynamoDBv2.DocumentModel.Document to System.String"
Is it a mismatch of a property in the data type of the "SomeClass" Vs the actual items properties in the DB? Can someone please help?

Found the issue here. I use two different programs to insert data to dynamo db and read data from dynamo db. Both of these programs are supposed to use the same "SomeClass" but unfortunately I had one of the properties altered in the "SomeClass" that I use to read the data (or run the count query) from Dynamo db. Once I fix the data type mismatch. it works fine now.

Related

Insert vertex into local Cosmos DB Emulator using Gremlin Console

I am trying to add vertices (and eventually edges) to a local Cosmos DB graph using the Gremlin console. I've been following this tutorial. However, whenever I try to add a vertex, I get an error about the partition key.
My query:
g.addV('person').property('firstName', 'Thomas').property('lastName', 'Andersen').property('age', 44).property('userid', 1).property('pk', 'pk')
The error:
ActivityId : cd07f7be-d824-40fa-8137-0f2726a9c26d
ExceptionType : GraphRuntimeException
ExceptionMessage :
Gremlin Query Execution Error: Cannot add a vertex where the partition key property has value 'null'.
Source : Microsoft.Azure.Cosmos.Gremlin.Core
GremlinRequestId : cd07f7be-d824-40fa-8137-0f2726a9c26d
Context : graphcompute
Scope : graphcomp-execquery
GraphInterOpStatusCode : GraphRuntimeError
HResult : 0x80131500
Type ':help' or ':h' for help.
Display stack trace? [yN]
How can I fix my query and insert the data?
Did the same mistake of mixing the two values up. So when you add your azure database, you have to specify a partition key, I picked '/client';
Now when I do my query, I have to add this property:
.property('client', 'pk')
-- the first value has to be the key itself, and the second one 'pk', short for 'partitionKey'; Then in your document you have to add a property:
client: 'TESTCLIENTID'
But again, a lot of this is about what your 'partitioning' strategy is based on, which is something you have to decide upfront for each collection, this video from Azure explains things in more detail quite good.
I don't have a CosmosDB test environment, but there's a public sample project:
Getting started with Azure Cosmos DB: Graph API
Looks like you have to add a pk property (which most likely means "partition key", and should be configurable somehow).
You don't need to add a partition key in your g.addV i looked at what the "Quick start" tab creates for you in the portal which is the "graphdb/mycollection" database/collection. You can create your own which works fine without specifying partition key when adding a vertex... Just specify
Partition key
/_partitionKey
and check the checkbox
My partition key is larger than 100 bytes
That solved it for me anyway.
I had mixed up the partition key label and value. Reversing these fixed my issue.

SQLite.net query with casted field (int to string) does not work

I tried to query Customers from a SQLite tabel
I want those wher the Customer Number Starts with certain numbers.
The following code throws an error that sqlite does not have a methode ToString.
Can I reach my aim with Cast ?? How?
return conn.Table().Where(v => v.kdnr.ToString().StartsWith(suchtext));
Using entity framework, that would probably work:
.Where(v=>SqlFunctions.StringConvert(v.kdnr).StartsWith(suchtext))
I dont know about SQLite provider

ASP.NET Website display wrong data

I built a web application using ASP.NET, data stored at SQL Server 2008.
The application is running ok, but once a couple of day the application displays wrong data and i get error when i enter some pages. system return to normal work after 5 minutes by it self.
can someone give a clue what is the problem?
I'm getting error on lines which try to take data from retrieved DataTable:
like:
txtbx_contact_fullname.Text = dt_contact.Rows[0]["Contact_Fullname"].ToString();
or
lbl_Creation_datetime.Text = dt_YC_Last_Transaction.Rows[0]["Creation_datetime"].ToString();
usually these lines works perfect, and there is no reason that the datatable will return empty.
the error i get is:
Column 'xxxxx' does not belong to table.
The Query that retrieve the data is:
SELECT [Request ID],[Creation Date],[Request Status],[Contact Fullname],[Start Date],[Start Time],[End Date],[End Time],[Work Mode],[Comments],[HPM Points],[FA Points]
FROM dbo.vw_All_Requests
WHERE [Request Status] = #YellowCard_Status
ORDER BY [Creation Date] DESC
From some reason some columns do not get back..
txtbx_contact_fullname.Text = dt_contact.Rows[0]["Contact Fullname"].ToString();
lbl_Creation_datetime.Text = dt_YC_Last_Transaction.Rows[0]["Creation datetime"].ToString();
you column name in asp.net code has _ for example full_name but in sql query it does not have _, i don't know you've assigned names to your datatable or not but give attention to this issue ...
if you code is correct. you are calling Creation_datetime from .NET code, but in SQL you have no such column, what you do have is a Creation date only (from your SELECT query).
so, to fix your problem, all you need to do is change
dt_YC_Last_Transaction.Rows[0]["Creation_datetime"]
to
dt_YC_Last_Transaction.Rows[0]["Creation_date"]
after the issue is fixed, you should learn a better way to query the database using explicit names, for example, using objects instead calling the string value... You should learn a bit of Entity Framework and Linq, it will improve your code a lot.

Entity Framework 5, Code First, Full Text Search but IQueryable via CreateQuery?

I am using .NET 4.5 and EF 5 with Code First approach and now I need to implement Full Text Search.
I have already read a lot about it and so far my conclusions are:
Stored procedures nor Table Value Functions can not be mapped with Code First.
Still I can call them using dynamic sql
dbContext.Database.SqlQuery<Movie>(Sql, parameters)
But this returns IEnumerable and I want IQueryable so that I can do more filtering before fetching the data from db server. I know I can send those parameters to Db function but I don't want that.
What I have found that could fulfill my needs is CreateQuery function from IObjectContextAdapter that looks like this(Select All just for test):
IQueryable<Movie> result = ((IObjectContextAdapter)dbContext).ObjectContext.CreateQuery<Movie>("SELECT * FROM Movie");
However executing this throws Exception"
'System.Data.EntitySqlException was unhandled
HResult=-2146232006
Message=The query syntax is not valid. Near term '*', line 1, column 9.'
So the questions are:
Why do I get this exception and can it be fixed ?
If not is there any way with Code First to do FTS that returns IQueryable ?
Try it like this
ObjectQuery<Movie> query =
objectContext.CreateQuery<Movie>(#"SELECT VALUE movie FROM Movies");
As for why see these links
Differences from Transact-SQL Unlike Transact-SQL, Entity SQL does not
support use of the * argument in the SELECT clause. Instead
Entity SQL Reference - SELECT
"SELECT VALUE" - value keyword in LINQ/Entity Framework query

LINQ: Cannot insert duplicate key row in object 'dbo.tblOutstandingCompletions' with unique index

I have an application (ASP.NET 3.5) that allows users to rerun a particular process if required. The process inserts records into an MS SQL table. I have the insert in a Try / Catch and ignore the catch if a record already exists (the error in the Title would be valid). This worked perfectly using ADO but after I conveted to LINQ I noticed an interesting thing. If on a re-run of the process there was already records in the table, any new records would be rejected with the same error even though there was no existing record.
The code is as follows:
Dim ins = New tblOutstandingCompletion
With ins
.ControlID = rec.ControlID
.PersonID = rec.peopleID
.RequiredDate = rec.NextDue
.RiskNumber = 0
.recordType = "PC"
.TreatmentID = 0
End With
Try
ldb.tblOutstandingCompletions.InsertOnSubmit(ins)
ldb.SubmitChanges()
Catch ex As Exception
' An attempt to load a duplicate record will fail
End Try
The DataContext for database was set during Page Load .
I resolved the problem by redefining the DataContext before each insert:
ldb = New CaRMSDataContext(sessionHandler.connection.ToString)
Dim ins = New tblOutstandingCompletion
While I have solved the problem I would like to know if anyone can explain it. Without the DataContext redefinition the application works perfectly if there are no duplicate records.
Regards
James
It sounds like the DataContext thinks the record was inserted the first time, so if you don't redefine the context, it rejects the second insert because it "knows" the record is already there. Redefining the context forces it to actually check the database to see if it's there, which it isn't. That's LINQ trying to save a round trip to the database. Creating a new context as you've done forces it to reset what it "knows" about the database.
I had seen a very similar issue in my code were the identity column wasn't an autoincrementing int column, but a GUID with a default value of newguid() - basically LINQ wasn't allowing the database to create the GUID, but inserting Guid.Empty instead, and the second (or later) attempts would (correctly) throw this error.
I ended up ensuring that I generated a new GUID myself during the insert. More details can be seen here: http://www.doodle.co.uk/Blogs/2007/09/18/playing-with-linq-in-winforms.aspx
This allowed me to insert multiple records with the same DataContext.
Also, have you tried calling InsertOnSubmit multiple times (once for each new record) but only calling SubmitChanges once?
gfrizzle seems to be right here...
My code fails with the duplicate key error even though I've just run a stored proc to truncate the table on the database. As far as the data context knows, the previous insertion of a record with the same key is in fact a duplicate key, and an exception is thrown.
The only way that I've found around this is:
db = null;
db = new NNetDataContext();
right after the SubmitChanges() call that executes the previous InsertOnSubmit requests. Seems kind of dumb, but it's the only way that works for me other than redesigning the code.

Resources