Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am trying to convert the numbers from one datatable (dt) which are string values, to another datatable (dt1) that I want to be int. But I am getting an error:
'Cannot implicity convert type int to object[]'
DataTable dt1 = new DataTable();
DataRow row;
int obj1;
for ( int x= 0; x<dt.Rows.Count; x++)
{	int.TryParse(dt.Rows[x][0].ToString(), out obj1);
                            
	row = dt1.NewRow();
	row.ItemArray = obj1;
	dt1.Rows.Add(row);
}

Specific at obj1. I tried to use row.ItemArray = (int)obj1; but does not works...
Posted
Updated 14-Oct-10 3:19am
v4

row.ItemArray is an object[] I think, you can't assign an int to it. I'm a bit confused by your code.

to get rows from a table
dt.Rows
where dt is a DataTable

to access fields in a row where dr is a DataRow
int val = (int)dr["myIntField"];

or
int val = (int)dr[0]; // the first field


This might come in handy
http://msdn.microsoft.com/en-us/library/system.data.datarow.itemarray.aspx
 
Share this answer
 
v2
Use row.ItemArray = (object)obj1;
 
Share this answer
 
Comments
jalmonte 14-Oct-10 9:24am    
I tried that, but it give me another error: 'Cannot implicitly convert type object to object[]. An explicit conversion exists.'
Sauro Viti 14-Oct-10 9:39am    
Ops, sorry! Try this:

row.ItemArray = new object[1];
row.ItemArray[0] = (object)obj1;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900