Using sqltypes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I define a column X in table T as a Sqlint32 type:

table.Columns.Add("X", GetType(SqlInt32))

After the Dataset is filled from the source, I try to access the column:

Dim Y as SqlInt32
Y = dataset.tables("T")("X")

....but since I have OPTION STRICT ON the above won't work (trying to cast
Object to SqlInt32), so I explicity cast it:

Y = Ctype(dataset.tables("T")("X"), SqlInt32)

...but at runtime, the above Ctype throws an invalid cast exception.

I've tried all kinds of workarounds, but obviously I'm missing something
very basic... but what???

Thanks!

DT
 
Hi David,

You should use .net types for columns, like Int32, string, etc.
 
<< table.Columns.Add("X", GetType(SqlInt32))
After the Dataset is filled from the source, I try to access the column:

Dim Y as SqlInt32
Y = dataset.tables("T")("X")>>
The DataColumn value maps back to System.Int32

http://msdn.microsoft.com/library/d.../en-us/cpref/html/frlrfsystemdatasqltypes.asp
so that's what you want to cast to. Using Sql types is the fastest way to
get data in this example so I do recommend using it, but remember that they
need to map over to 'real' .NET types, in this particular case System.Int32

<<but since I have OPTION STRICT ON the above won't work (trying to cast
Object to SqlInt32), so I explicity cast it:>>
Good - Option Strict is pure Goodness ;-)


<<Y = Ctype(dataset.tables("T")("X"), SqlInt32)
Y = CType(dataset.Tables("T")("X"), System.Int32)

However, I'd use indexes instead to reference the columns (or use Typed
datasets even better) for performance reasons

Good luck,

Bill
 
Back
Top