invalidCastException while adding to a SqlCeUpdatableRecord

  • Thread starter Thread starter Milsnips
  • Start date Start date
M

Milsnips

Hi there,
im testing some code for performance improvements by using the
sqlceResultSet to import data locally, however on one field i'm always
hitting this problem.

Locally, my field (quantity) is a SMALLINT, and whatever i try adding to
that field in the record returns me the invalidCastException.

Any help appreciated,
Paul
 
Hi Simon,

my code was something like:
SqlCeUpdateableRecord rec;
rec.CreateRecord();
rec["quantity"] = dr["qty"].tostring();

i think i also tried converting it as an int also but kept returning me the
error. i noticed in some other forums, someone also had a same problem with
the smallint datatype but using replication.

regards,
Paul
 
What type is quantity? if its a numeric you will need to cast it.

ie: rec["quantity"] = (short)dr["qty"];
--
Simon Hart
http://srhartone.blogspot.com


Milsnips said:
Hi Simon,

my code was something like:
SqlCeUpdateableRecord rec;
rec.CreateRecord();
rec["quantity"] = dr["qty"].tostring();

i think i also tried converting it as an int also but kept returning me the
error. i noticed in some other forums, someone also had a same problem with
the smallint datatype but using replication.

regards,
Paul
 
"quantity" is also a SMALLINT in the remote database.
I'll give that conversion a try and see how it goes.

regards,.
Paul
Simon Hart said:
What type is quantity? if its a numeric you will need to cast it.

ie: rec["quantity"] = (short)dr["qty"];
--
Simon Hart
http://srhartone.blogspot.com


Milsnips said:
Hi Simon,

my code was something like:
SqlCeUpdateableRecord rec;
rec.CreateRecord();
rec["quantity"] = dr["qty"].tostring();

i think i also tried converting it as an int also but kept returning me
the
error. i noticed in some other forums, someone also had a same problem
with
the smallint datatype but using replication.

regards,
Paul

Simon Hart said:
What are you casting it into? a smallint is a 2 byte numeric so casting
to
a
short should work.

For more info, see:
http://msdn2.microsoft.com/en-us/library/ms187745.aspx

--
Simon Hart
http://srhartone.blogspot.com


:

Hi there,
im testing some code for performance improvements by using the
sqlceResultSet to import data locally, however on one field i'm always
hitting this problem.

Locally, my field (quantity) is a SMALLINT, and whatever i try adding
to
that field in the record returns me the invalidCastException.

Any help appreciated,
Paul
 
If dr["qty"] was null then he'd be getting a System.NullReferenceException
not a System.InvalidCastException so the field must be valid. But it is good
practice to check for DBNull.Value before assigning any data even if you
expect the data to be non-null.

--
Simon Hart
http://srhartone.blogspot.com


Chris Craft said:
Hi Paul,

Are you allowing nulls in the database? If so that is your problem. A
breakpoint will tell you quickly or database schema check.

because if dr["qty"] = null then you are effectively running
null.tostring() which will throw an exception.

If null is the issue you can handle it in several ways:

write an if statement

use the ?? operator
The ?? operator returns the left-hand operand if it is not null,
or else it returns the right operand.
http://msdn2.microsoft.com/en-us/library/ms173224(VS.80).aspx

you should also be able to use the Convert.ToString(___) method

hope it helps,


Milsnips said:
Hi Simon,

my code was something like:
SqlCeUpdateableRecord rec;
rec.CreateRecord();
rec["quantity"] = dr["qty"].tostring();

i think i also tried converting it as an int also but kept returning me the
error. i noticed in some other forums, someone also had a same problem with
the smallint datatype but using replication.

regards,
Paul

Simon Hart said:
What are you casting it into? a smallint is a 2 byte numeric so casting to
a
short should work.

For more info, see: http://msdn2.microsoft.com/en-us/library/ms187745.aspx

--
Simon Hart
http://srhartone.blogspot.com


:

Hi there,
im testing some code for performance improvements by using the
sqlceResultSet to import data locally, however on one field i'm always
hitting this problem.

Locally, my field (quantity) is a SMALLINT, and whatever i try adding to
that field in the record returns me the invalidCastException.

Any help appreciated,
Paul
 
Yes i think that might have been the problem. most of my fields have values
but i think 2-3 had a null value which could have been causing the error.

Thanks for the feedback from both of you.
regards,
Paul

Simon Hart said:
If dr["qty"] was null then he'd be getting a System.NullReferenceException
not a System.InvalidCastException so the field must be valid. But it is
good
practice to check for DBNull.Value before assigning any data even if you
expect the data to be non-null.

--
Simon Hart
http://srhartone.blogspot.com


Chris Craft said:
Hi Paul,

Are you allowing nulls in the database? If so that is your problem. A
breakpoint will tell you quickly or database schema check.

because if dr["qty"] = null then you are effectively running
null.tostring() which will throw an exception.

If null is the issue you can handle it in several ways:

write an if statement

use the ?? operator
The ?? operator returns the left-hand operand if it is not null,
or else it returns the right operand.
http://msdn2.microsoft.com/en-us/library/ms173224(VS.80).aspx

you should also be able to use the Convert.ToString(___) method

hope it helps,


Milsnips said:
Hi Simon,

my code was something like:
SqlCeUpdateableRecord rec;
rec.CreateRecord();
rec["quantity"] = dr["qty"].tostring();

i think i also tried converting it as an int also but kept returning me the
error. i noticed in some other forums, someone also had a same problem with
the smallint datatype but using replication.

regards,
Paul

What are you casting it into? a smallint is a 2 byte numeric so
casting to
a
short should work.

For more info, see: http://msdn2.microsoft.com/en-us/library/ms187745.aspx

--
Simon Hart
http://srhartone.blogspot.com


:

Hi there,
im testing some code for performance improvements by using the
sqlceResultSet to import data locally, however on one field i'm
always
hitting this problem.

Locally, my field (quantity) is a SMALLINT, and whatever i try adding to
that field in the record returns me the invalidCastException.

Any help appreciated,
Paul
 
Back
Top