473,403 Members | 2,323 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,403 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 11154
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
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...
1
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...
3
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...
4
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...
1
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...
2
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...
5
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...
2
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
7
JustJim
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.