LINQ: System.Data.SqlClient.SqlException: String or binary data would be truncated - asp.net-mvc

I have a table and am using ASP.NET MVC to create an object and save it. It's been working fine for a while. Somehow, recently, I started getting this error message on the production machine:
System.Data.SqlClient.SqlException: String or binary data would be truncated.
I can't seem to replicate the same error on my development machine. Is there anyway to find out what column is causing this? I'm running MSSQL 2008 R2 Express.

This usually occurs when you're trying to insert data that is longer than the field size defined on SQL Server. You can increase the size of the field in the database to solve the problem. If you're accepting user input and saving it to the database then you should be validating it against the defined maximum size of the database field.

Caused by data-length mismatch between your insert data and the defined column in your database. It indicates that the database would have to cut of data from your insert to be able to store it.
You may take closer look to the inner exception, I don't know it for sure, but I think you find more information there about the column that throws that error. And yes, you should prevent the users from inserting too long values and catch this in your model.
If you, for what ever don't want to implement a additional validation you should log the exception with the content that you tried to insert/update and then compare the values with the db.

Related

Weird errors -607/-11003 when retrieving BYTE-values

We have some very simple (id INTEGER PRIMARY KEY, data BYTE)-tables that I'm trying to retrieve the data from, but seem to run into some kind of data corruption. When accessing or retrieving the data-column e.g. via SELECT data FROM foobar WHERE id = 42 or SELECT LENGTH(data) FROM foo WHERE id = 42 I get
[HY000] [Informix][Informix ODBC Driver][Informix]Text/Byte subscript error. (-607) (SQLFetch)
which is obviously weird, as no byte subscript operator is used. It rather seems to me Informix is running into some kind of data corruption issue and is unable to retrieve the underlying BLOB. The query works if I explicitly exclude the offending row, so SELECT SUM(LENGTH(data)) FROM foo fails with the above error, while SELECT SUM(LENGTH(data)) FROM foo WHERE id NOT IN (42,...) succeeds.
For other rows, I retrieve an error
[01004] [Informix][Informix ODBC Driver]Data truncated. (-11003) (SQLGetData)
which also can only be mitigated by (finding and) excluding the offending row via their primary key.
First of all, is there a way to tell if this is a driver/odbc error or if the underlying data is actually corrupted? Is there a way to check the tables for data corruption, instead of running into this kind of problem one by one?
This is IBM Informix Dynamic Server Version 14.10.FC5WE
It sounds to me like running the same query, but using different tool or method, would give you an idea if it's the driver or the data itself.
If the data is corrupted, you can use the following to confirm the checksum value and identify:
CHECKSUM TABLE foobar;
Also, obviously updating the driver version is given, have you tried that?

Error 2002 : The EntityContainer 'X' for the storage model specified as part of this MSL does not exist in MetadataWorkspace

I'm trying to integrate my Oracle Database into my ASP.NET app, but I keep getting this error :
Error 2002: The EntityContainer 'OracleDBTargetContainer' for the
storage model specified as part of this MSL does not exist in
MetadataWorkspace.
I've tried everything I could think of but still can't figure out what's wrong.
What can I do to diagnose the root cause of this?
This is still new to me so I might miss something very obvious.
I'm assuming it my not like my database as the Diagram displayed when I integrated another DB. I've checked the foreign keys, primary keys, ... but to no good.
Using VS2013 with ODP.NET 12c Release 3.
EDIT : My EntityContainerMapping is empty, is that normal?
<EntityContainerMapping CdmEntityContainer="PMModelContainer" StorageEntityContainer="PMModelTargetContainer"></EntityContainerMapping>
I found what was wrong.
One of my foreign key didn't match the primary key (Number 20 instead of Number 10)
I saw on stack overflow that it could be a problem and checked my tables but I missed that one obviously.
How did I realize it ? Well because rather than creating the EF Designer, I chose the Code First from database option, and, oh, what do you know, the error message now tells you EXACTLY what the problem is ! (Table names and columns).

Complex T-SQL script executed via TADOQuery is firing "Multiple-step OLE DB operation generated errors."

I have a very large block of SQL that I am trying to execute inside of Delphi, against a Microsoft SQL Database. I am getting this:
Multiple-step OLE DB operation generated errors.
Check each OLE DB status value, if available. No work was done.
The script has multiple sql IF statements followed by BEGIN and END blocks with invocations of stored procedures, declaration of variables, and EXEC inside that. Finally it returns some of the variable values by SELECT #Variable1 AsName1,#Variable2 AsName2....
The above multi-step error is coming in as an OLEException from ADO, not from the Delphi code, and happens after all the SQL exec-stored-procedure have occurred, and therefore I suspect it's firing this OLE exception when it reaches the final stage which SELECT #Variable1 AsName1,... to get back a few variable values for my program to see them.
I know about this retired/deprecated MS KB article, and this is unfortunately not my actual issue:
http://support.microsoft.com/kb/269495
In short that KB article says to fix a registry key and remove "Persist Security Info" from the connection string. That's not my problem. I'm asking this question because I found the answer already and I think that someone else who gets stuck here might not want to waste several hours finding potential issues when there are several that I have found after searching for solutions for several hours. Anyone who wants to add another answer with different options, is fine, and I'll select yours if it's reproducible, and if necessary I'll turn this one into a Community Wiki because there could be a dozen obscure causes for this "ADO recordset is in a bad mood and is unhappy with your T-SQL" exception.
I have found several potential causes that are listed in various sources of documentation. The original KB article in the question suggests removing the 'Persist Security Info' from my ADO connection string, however in a standalone test in an application with just a TADOConnection and a single TADOQuery, the presence or absence of Persist Security Info had no effect, nor did explicitly setting it True or False.
What DID fix it was removing this CursorType declaration:
CursorType=ctKeyset
What I have learned is that bidirectional ADO datasets are fine for SELECT * FROM TABLE in ADO but are not so fine for complex SQL scripts.
Potential source of this error is updating char field with large value.
Example: Form has edit box with max length property set to 20 characters and Oracle database table has field defined as char(10).
Updating with 10 characters (or less) will work fine while updating with more then 10 characters will cause 'Multiple step...' error on ADOQuerry.UpdateBatch().
You also have to know that CHAR will allways have 20 characters. Consider Trimming value in edit box. CHAR behaves different than VARCHAR2 type.
If you have a query with parameter ,check the number of parameters in the query is matched with script...!

Uknown error finalizing or resetting statement in sqlite

I'm trying to get our current database to use FTS. I recompiled the project with FTS3 and FTS4 support on. I know this part works since I've tried some of the examples off of the sqlite pages and they work.
e.g.
CREATE TABLE t2(id INTEGER PRIMARY KEY, a, b, c);
CREATE VIRTUAL TABLE t3 USING fts4(content="t2", a, c);
What I want to do is add a Virtual table to a .db that is already saved to the documents directory of our app. It is writeable.
My question is multi part. First, I tried my query in Firefox's add-on SQLite manager. My query is this:
CREATE VIRTUAL TABLE VirtualTestTable USING fts4(content=Chromosomes, ID Integer Primary Key Autoincrement, rsID, ChrID, Position, Strand, GeneSymbol)
Once I run this in sqlite manager, it creates a few different tables (VirtualTestTable, VirtualTestTable_docsize, VirtualTestTable_segdir, VirtualTestTable_segments, VirtualTestTable_stat).
So I assume sqlite manager supports FTS4. My table is also populated with the data that was original in my Chromosomes Table. But when I try to match anything in the table, I always get zero results. So my first question is, does sqlite manager support FTS queries?
My other question, which is more important to me, is if my query works in sqlite manager, I try this query in my Xcode project with different variations of
content='Chromosomes'
content=\"Chromosomes\"
content=Chromosomes
I always get an error with my query. I'm using FMDB and using the executeUpdate: message. My error is:
Error calling sqlite3_step (1: SQL logic error or missing database) SQLITE_ERROR
Unknown error finalizing or resetting statement (1: unrecognized parameter: content=Chromosomes)
Does anyone know why I'm getting this error? I don't think it's a missing database as when I try to just create an empty Virtual table on my fmdatabase, it runs fine. Thanks.
FTS queries work just fine in SQL Manager:
CREATE VIRTUAL TABLE t USING FTS4(x);
INSERT INTO t VALUES('blah');
INSERT INTO t VALUES('blubb');
SELECT * FROM t WHERE x MATCH 'bl*';
The error message "unrecognized parameter: content=" is typical for SQLite versions that do not support contentless/external content tables, i.e., ones older than 3.7.11.

String Truncation on Transfer to ClientDataset

I'm using Firebird 2.1, DBExpress Driver from DevArt and Delphi 2010. Some of my reports that used to work with Delphi 2006 stopped working and produced an error message indicating that "arithmetic exception, numeric overflow, or string truncation" had occurred. The error occurred at this point in my code:
cds.Data := dsProvider.Data;
I found the place in my SQL statement that caused the error:
iif(ytd.hPayType <> -1,t.sCode, 'NET') sPayType
T.sCode is a Varchar(10) field. My conclusion is that the query returns data to the dsProvider and that when the dsProvider.Data is passed to the cds.Data, the cds component sets the field width based on the first value it receives. I get the same error message if I change the "iif" to a CASE statement. I managed to work around the issue by doing this:
CAST(iif(ytd.hPayType <> -1,t.sCode, 'NET') AS varchar(10)) sPayType
Since this used to work in Delphi 2006 without the CAST, I assume that the new behavior is due to an update to the TClientDataset. It would be nice to have the old, more forgiving behavior. Is there a way to configure the ClientDataset to accept this without a complaint or do I need to just tell my users to CAST on string results based on iif and CASE statements?
I used to work a lot with firebird in my last job, this error happens when you already have a large (length) varchar field value stored in the db and you are trying to "get" the string in delphi, try updating the value in the db to a smaller (length) varchar. I'm not sure if will work for you but give it a try.
Well, with a little more experience, it looks like I am seeing this truncation error show up consistently with the Delphi 2010 version of ClientDatasets. If I find a resolution that does not involve having to use CAST in the query, I will post it here. But for now, I am going to close this posting.

Resources