views:

348

answers:

5

Why I'm having this error "Object of type 'System.UInt16' cannot be converted to type 'System.Int16" when I'm trying to run below code

 public ActionResult List()
 {
     var x = account.All();
     return View(x);
 }

The errors showed up, when it's try to iterate my model in my List.aspx file (below code).

  <% foreach (var item in Model) { %>

I'm using Subsonic 3.0.3 and MySQL database. Below is my account table descriptions

acc_id          smallint(5)        unsigned pri
acc_type        smallint(5)        unsigned
acc_status      tinyint(3)         unsigned
acc_balance     int(11)            unsigned

Do I have to modify something in MySQL.ttinclude to make this thing work?

+1  A: 

Without knowing what account.All() or View(x) do, that's pretty hard to know. It would have helped if you'd used explicit typing for x as well.

My guess is that something's unboxing to short instead of ushort, but we can't really tell from just that bit of code. I assume this is an exception rather than a compile-time error? More information please!

Jon Skeet
All() is built in function from Subsonic T4 Active Records template that actually will return all records within account table without any filters.
Funky81
A: 

It is extremely difficult to provide an answer based on the code you provided. You are not saying which line is giving the error or where the problem is. However, Check the Convert class in C#, if the problem is in C# and not on your DB or Subsonic, then I think Convert will do the trick.

ushort num1 = some_number;

short num2;

num2 = Convert.ToUInt16(num1);

However, it is very difficult to understand the implication of this convertion, you should be aware this could broke something else later.

Freddy
I dont know about this, because all I know, the thing that you told me it's inside Subsonic.Core dll implementation.
Funky81
A: 

I have exactly the same problem with SubSonic 3 and MySQL - the only way round it I could find was to change the MySQL table definition to use signed columns and not unsigned ones.

If you need the extra range you could always use a bigint (although I believe this has a performance impact on 32 bit processors).

woodstylee
A: 

I've made fork from Subsonic3 Templates for MySQL. Within my fork, I modified so it can support MySQL unsigned data types which solved my own question :D please check it out, http://github/funky81

Funky81
A: 

Changing the properties for the unsigned columns in the ActiveRecord.cs from int or ?int to uint and ?uint will partly solve the problem.

Tobias