472,147 Members | 1,263 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,147 software developers and data experts.

[Datable] change column datatype

Sam
Hi

Here is my code :

Code:
Dim dt As New DataTable
dt.Columns.Add.ColumnName = "New"
dt.Columns("New").DataType = System.Type.GetType("System.String")
dt.Columns.Add.ColumnName = "Id"
dt.Columns("Id").DataType = System.Type.GetType("System.Integer")
Dim Row As DataRow = dt.NewRow()
Dim rc As DataRowCollection = dt.Rows
rc.InsertAt(Row, 0)

Defining the ype of the column "New" doesn't crash the application but
defining the type of the column "Id" to integer raises the following
exception:

"System.ArgumentException: Cannot change DataType of a column once it
has data.

But there is no data in it since I've just created the column...
What's wrong with my code ??

Thx

Nov 21 '05 #1
6 10726
Sam
hummm.... I've found out.
System.Integer does not exists, I should have done System.Int32 instead
but the compiler didn't complain

Nov 21 '05 #2
Sam,

Are you sure you get that error, because it is more likely that you get an
error that System.integer does not exist.

However, here the same code what is not such a C# look alike as in the
samples on MSDN and works easier because you can use the intelisence

\\\
Dim dt As New DataTable
dt.Columns.Add.ColumnName = "New"
dt.Columns("New").DataType = GetType(System.String)
dt.Columns.Add.ColumnName = "Id"
dt.Columns("Id").DataType = GetType(System.Int32)
Dim Row As DataRow = dt.NewRow()
Dim rc As DataRowCollection = dt.Rows
rc.InsertAt(Row, 0)
///

(I assume that you know that in the constructor from the datatable you can
as well do)

\\\
dt.Columns.Add.ColumnName("New", GetType(System.String))
///

I hope this helps,
Cor

Nov 21 '05 #3
Sam
thanks. as I said the compiler didn't complain about that. This is the
reason I couldn't find out about this error.

Nov 21 '05 #4
Sam,
thanks. as I said the compiler didn't complain about that. This is the
reason I couldn't find out about this error.


I was sending in the same time a you, so how could I know you had seen that
already, however watch those easier methods I showed you.

:-)

Cor
Nov 21 '05 #5
Sam
I will
Thanks Cor

Nov 21 '05 #6
Sam wrote:
System.Integer does not exists, I should have done System.Int32
instead but the compiler didn't complain


It didn't complain because you used the System.Type.GetType() method, which
takes a string as its parameter. You passed a valid string, so the compiler
is happy. The fact that the string contained an invalid value for this
function couldn't be picked up until run time.

If you use the GetType() function as in Cor's example, you pass an actual
type rather than a string containing the name of the type. That way the
compiler is able to spot the error at compile time instead of when the code
runs.

--

(O)enone

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Sanjay Minni | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.