In C#, when I insert a record into Access tabel, why will it cause error if the length of string

I define a field "myField" in the tabel "AccessTable", the length of "myField" is 3
when I insert a string with the length 7, it will cause error, why?

myCommand.CommandText="Insert into AccessTable (myField) values ('123457')"; //cause error!!!
myCommand.CommandText="Insert into AccessTable (myField) values ('12')"; //OK


When I do the same thing using Pascal in Delphi, if the length of string exceed the length of a field,
it will not cause error, the exceeded string will be trim automatically!

OleDbConnection myOleDbConnection=null;
OleDbTransaction trans=null;
myTreePar.myTreeView.BeginUpdate();
try
{

myOleDbConnection=ConstClass.GetDatabaseConnect();
myOleDbConnection.Open();

trans=myOleDbConnection.BeginTransaction();

OleDbCommand myCommand=new OleDbCommand();
myCommand.Connection=myOleDbConnection;
myCommand.Transaction=trans;
myCommand.CommandText="Insert into AccessTable (myField) values ('123457')";
myCommand.ExecuteNonQuery();

trans.Commit();

}
catch(Exception e)
{
MessageBox.Show(e.Message);
trans.Rollback();
}
finally
{
myOleDbConnection.Close();
}

[1257 byte] By [CUIWEI] at [2007-12-16]
# 1
I think the more important question is "why does Delphi just throw away some data without warning you or seeking your permission"? Without getting into an argument over which is the better language, C# does not simply assume that it is OK to discard the rest of the data. C# forces you to check the length of the string yourself and then you decide whether to discard the extra charcters or not. This is a better system because, while it may mean you need a few extra lines of code, it also means that if data is discarded it is because you decided that it wasn't important and discarded it yourself.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
It's so bother to check the length of string before insert record into table every time! Many times it's not very serious to discard the rest of string without warning. but in C#, it will cause serious error, the whole program will be crashed. so I hope the extra charcters can be discarded automatically, how can I do in C#!
CUIWEI at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
Hi,
I definitely agree with jmcilhinney. But if you want to save the data without warning, then you could truncate the data first (w/o checking) and save it to the table...

cheers,
Paul June A. Domag

PaulDomag at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
If you want the string to be truncated then it is up to you to do the truncating. What is more serious: your program crashes so you have to fix the bug, or some company enters 10,000 records into their database only to find that they've all been truncated without their knowledge and they have no other copy of the data that your program assumed that it was safe to throw away? Your code should never have to make assumptions on your behalf. If you want something to happen then make it happen. Check the maximum length of the field and, if your string is longer than that then take a substring.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
Hi,
Better if you provide your user an options form, which has the choice to enable/disable automatic string truncating. But disable the feature at default.Smile
cheers,
Paul June A. Domag
PaulDomag at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 6

I would turn this question the other way around. Rather than asking 'why does .NET warn me that I am about to lose data?' I would ask (in a Delphi forum, of course) 'why does Delphi *not* warn me that I am about to lose data?' . (If that really is the normal behaviour of Delphi.)
..NET is doing the right thing here. This is expected behaviour. If you want to ignore the error, you can do that, or better still, trim the string to the maximum length yourself before attempting to insert it. You could, if you wished, warn the user of the problem and let the user choose whether to trim the string or re-enter a different string. All of these choices are available to you only because .NET raises the error. No error, no choices.
-- Brendan Reynolds (Access MVP)

MVPUser at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 7
I dont know what you are doing in delphi, but my Delphi 7 doesnt truncate, but errors with code 8152 using ado or DBExpress components (dont know about bde/odbc)

Delphi blows vb6 away, by the way (vb6 was a dog. . . vb.7 is not much better)

BlairStark at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 8
Delphi 5.0 make string truncate with ADO
CUIWEI at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 9
Blair Stark wrote:
I dont know what you are doing in delphi, but my Delphi 7 doesnt truncate, but errors with code 8152 using ado or DBExpress components (dont know about bde/odbc)
Delphi blows vb6 away, by the way (vb6 was a dog. . . vb.7 is not much better)
If you want to extoll the virtues of Delphi over VB then I'd suggest that a C# forum is a pretty pointless place to do it.
CUI WEI wrote:
Delphi 5.0 make string truncate with ADO
Isn't Delphi at version 8 or 9 now? Using Delphi 5 is like using VB2 or 3. Comparing any current language to that is probably not a good idea.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...