473,394 Members | 4,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

[Datable] change column datatype


Here is my 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

"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 ??


Nov 21 '05 #1
6 11152
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

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,

Nov 21 '05 #3
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
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.


Nov 21 '05 #5
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



Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Sanjay Minni | last post by:
What is the datatype to be used for Primary Key columns for most optimised access given that - There will be a single column primary key only - The values will only be integers (but as...
by: Bruce | last post by:
Hi, I want to change the datatype of an existing column from char to varbinary. When I run the "Alter Table" statement, I get the following error message - Disallowed implicit conversion...
by: rdraider | last post by:
I'm doing a data conversion project, moving data from one SQL app to another. I'm using INSERT INTO with Select and have the syntax correct. But when executing the script I get: Server: Msg...
by: Brian Brane | last post by:
I have properties that wrap DataRow columns as in: public int aNumber { get{ return m_DataRow; } set{ m_DataRow = value; } } If the column happens to contain DBNull, I get a cast exception...
by: John E. | last post by:
I have a datagrid with one column that is boolean (it is a checkbox column). So that myDataTable.Column..DataType.UnderlyingSystemType == System.Boolean. For printing purposes, I would like to...
by: ad | last post by:
I use ado.net to fill a Excel wroksheet into a DataTable. The data in the Excel wroksheet is digital. After the data filled into the DataTable, the DataType of each column is set to Double, but...
by: Ken | last post by:
I'm trying to run a loop to capture column property information from a table in my datasource. Can anybody see where this is going wrong? Dim tbl As New DataTable Dim col As DataColumn Dim x...
by: coolminded | last post by:
dear all, i have to modify the datatype of a column of a table. the datatype of the column is varchar(2). i need to increase the length to 20. can u provide the query to change it TIA
by: JustJim | last post by:
Hi If all you can do with ALTER TABLE....ALTER COLUMN is change the datatype/size then how do I change the name of a field? I have several database files spread all over the state and some of...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.