By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,677 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,677 IT Pros & Developers. It's quick & easy.

arraylist to datatable

P: n/a
Hi,

I have a code that creates a datatable from an arraylist, but i am getting
an error in casting in

for (int intRow = 0; intRow < alLCPlist.Count; intRow++)
{
DataRow drow = dtLCPack.NewRow();
int intColCount = dtLCPack.Columns.Count;
ArrayList arrlRow = (ArrayList)alLCPlist[intRow]; <== Specified cast
is not valid here
for (int intCol=0; intCol < intColCount; intCol++)
{
drow[intCol] = arrlRow[intCol];
}
dtLCPack.Rows.Add(drow);
}
Can someone help me how to fix it?

Thanks
Gane
Nov 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Without seeing what alLCPlist is and how it's populated, it's quite hard to
help. Clearly,alLCPlist doesn't contain arraylists...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"gane kol" <ga*****@softca.com> wrote in message
news:O2**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have a code that creates a datatable from an arraylist, but i am getting
an error in casting in

for (int intRow = 0; intRow < alLCPlist.Count; intRow++)
{
DataRow drow = dtLCPack.NewRow();
int intColCount = dtLCPack.Columns.Count;
ArrayList arrlRow = (ArrayList)alLCPlist[intRow]; <== Specified cast is not valid here
for (int intCol=0; intCol < intColCount; intCol++)
{
drow[intCol] = arrlRow[intCol];
}
dtLCPack.Rows.Add(drow);
}
Can someone help me how to fix it?

Thanks
Gane

Nov 19 '05 #2

P: n/a
Well, the code before the for loop is
--------
ArrayList alLCPlist = new ArrayList();

DataRow dr;

for (int i=0;i<infoNode.ChildNodes.Count;i++)

{

currentNode = infoNode.ChildNodes[i];

alLCPlist = product.GetLCPack(strvalue,
currentNode.Attributes["Name"].Value.ToString());

if (alLCPlist.Count > 0)

{

------

alLCPlist gets the data returned from the dataaccess layer.
product.GetLCPack returns an arraylist.

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:Or**************@TK2MSFTNGP15.phx.gbl...
Without seeing what alLCPlist is and how it's populated, it's quite hard to help. Clearly,alLCPlist doesn't contain arraylists...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"gane kol" <ga*****@softca.com> wrote in message
news:O2**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have a code that creates a datatable from an arraylist, but i am getting an error in casting in

for (int intRow = 0; intRow < alLCPlist.Count; intRow++)
{
DataRow drow = dtLCPack.NewRow();
int intColCount = dtLCPack.Columns.Count;
ArrayList arrlRow = (ArrayList)alLCPlist[intRow]; <== Specified

cast
is not valid here
for (int intCol=0; intCol < intColCount; intCol++)
{
drow[intCol] = arrlRow[intCol];
}
dtLCPack.Rows.Add(drow);
}
Can someone help me how to fix it?

Thanks
Gane


Nov 19 '05 #3

P: n/a
alLCPList itself is an arraylist, because, as you say, GetLCPack returns an
arraylist...but what does the items within the array list contain?

ArrayList arr = new ArrayList()
arr.Add("1");
arr.Add("2");
arr.Add("3");
string value = (string)arr[1];
ArrayList arr = new ArrayList()
arr.Add(1);
arr.Add(2);
arr.Add(3);
int value = (int)arr[0];

ArrayList arr = new ArrayList()
arr.Add(new DataSet());
arr.Add(new DataSet());
arr.Add(new DataSet());
DataSet value = (DataSet)arr[2];
if you are casting an item of the arraylist to arraylist, than this item
must be an arraylist, ala:

ArrayList arr = new ArrayList()
arr.Add(new ArraList());
arr.Add(new ArraList());
arr.Add(new ArraList());
ArraListvalue = (ArraList)arr[1];
If you are getting an invalid cast, then the items within your alLCPlist
ArrayList aren't embedded ArrayLists..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"gane kol" <ga*****@softca.com> wrote in message
news:uI**************@TK2MSFTNGP10.phx.gbl...
Well, the code before the for loop is
--------
ArrayList alLCPlist = new ArrayList();

DataRow dr;

for (int i=0;i<infoNode.ChildNodes.Count;i++)

{

currentNode = infoNode.ChildNodes[i];

alLCPlist = product.GetLCPack(strvalue,
currentNode.Attributes["Name"].Value.ToString());

if (alLCPlist.Count > 0)

{

------

alLCPlist gets the data returned from the dataaccess layer.
product.GetLCPack returns an arraylist.

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:Or**************@TK2MSFTNGP15.phx.gbl...
Without seeing what alLCPlist is and how it's populated, it's quite hard

to
help. Clearly,alLCPlist doesn't contain arraylists...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"gane kol" <ga*****@softca.com> wrote in message
news:O2**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have a code that creates a datatable from an arraylist, but i am getting an error in casting in

for (int intRow = 0; intRow < alLCPlist.Count; intRow++)
{
DataRow drow = dtLCPack.NewRow();
int intColCount = dtLCPack.Columns.Count;
ArrayList arrlRow = (ArrayList)alLCPlist[intRow]; <== Specified

cast
is not valid here
for (int intCol=0; intCol < intColCount; intCol++)
{
drow[intCol] = arrlRow[intCol];
}
dtLCPack.Rows.Add(drow);
}
Can someone help me how to fix it?

Thanks
Gane



Nov 19 '05 #4

P: n/a
Actually i am converting a datareader to arraylist.

----------
arrOracleParams[0] = objDB.BuildOracleParameter("Product",
System.Data.OracleClient.OracleType.VarChar,
ParameterDirection.Input,Product,50);
arrOracleParams[1] = objDB.BuildOracleParameter("Type",
System.Data.OracleClient.OracleType.VarChar,
ParameterDirection.Input,Type,20);
arrOracleParams[2] = objDB.BuildOracleParameter("pCursor",
System.Data.OracleClient.OracleType.Cursor,
ParameterDirection.Output,null,0);
OracleDataReader objOraReader =
objDB.RunExecuteOracleDataReader("pkg_Prod.sp_get_ CP", arrOracleParams);

while (objOraReader.Read())
{

object[] values = new object[objOraReader.FieldCount];

objOraReader.GetValues(values);

alList.Add(values);

}

return alList;

------
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:eq**************@TK2MSFTNGP10.phx.gbl...
alLCPList itself is an arraylist, because, as you say, GetLCPack returns an arraylist...but what does the items within the array list contain?

ArrayList arr = new ArrayList()
arr.Add("1");
arr.Add("2");
arr.Add("3");
string value = (string)arr[1];
ArrayList arr = new ArrayList()
arr.Add(1);
arr.Add(2);
arr.Add(3);
int value = (int)arr[0];

ArrayList arr = new ArrayList()
arr.Add(new DataSet());
arr.Add(new DataSet());
arr.Add(new DataSet());
DataSet value = (DataSet)arr[2];
if you are casting an item of the arraylist to arraylist, than this item
must be an arraylist, ala:

ArrayList arr = new ArrayList()
arr.Add(new ArraList());
arr.Add(new ArraList());
arr.Add(new ArraList());
ArraListvalue = (ArraList)arr[1];
If you are getting an invalid cast, then the items within your alLCPlist
ArrayList aren't embedded ArrayLists..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"gane kol" <ga*****@softca.com> wrote in message
news:uI**************@TK2MSFTNGP10.phx.gbl...
Well, the code before the for loop is
--------
ArrayList alLCPlist = new ArrayList();

DataRow dr;

for (int i=0;i<infoNode.ChildNodes.Count;i++)

{

currentNode = infoNode.ChildNodes[i];

alLCPlist = product.GetLCPack(strvalue,
currentNode.Attributes["Name"].Value.ToString());

if (alLCPlist.Count > 0)

{

------

alLCPlist gets the data returned from the dataaccess layer.
product.GetLCPack returns an arraylist.

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:Or**************@TK2MSFTNGP15.phx.gbl...
Without seeing what alLCPlist is and how it's populated, it's quite hard
to
help. Clearly,alLCPlist doesn't contain arraylists...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"gane kol" <ga*****@softca.com> wrote in message
news:O2**************@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> I have a code that creates a datatable from an arraylist, but i am

getting
> an error in casting in
>
> for (int intRow = 0; intRow < alLCPlist.Count; intRow++)
> {
> DataRow drow = dtLCPack.NewRow();
> int intColCount = dtLCPack.Columns.Count;
> ArrayList arrlRow = (ArrayList)alLCPlist[intRow]; <==

Specified cast
> is not valid here
> for (int intCol=0; intCol < intColCount; intCol++)
> {
> drow[intCol] = arrlRow[intCol];
> }
> dtLCPack.Rows.Add(drow);
> }
> Can someone help me how to fix it?
>
> Thanks
> Gane
>
>



Nov 19 '05 #5

P: n/a
Looks like you are converting it to an array of objects...which is very
different than an array list.

try

object[] arrlRow = (object[])alLCPlist[intRow];

in the initial code you sent ...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"gane kol" <ga*****@softca.com> wrote in message
news:OJ**************@TK2MSFTNGP14.phx.gbl...
Actually i am converting a datareader to arraylist.

----------
arrOracleParams[0] = objDB.BuildOracleParameter("Product",
System.Data.OracleClient.OracleType.VarChar,
ParameterDirection.Input,Product,50);
arrOracleParams[1] = objDB.BuildOracleParameter("Type",
System.Data.OracleClient.OracleType.VarChar,
ParameterDirection.Input,Type,20);
arrOracleParams[2] = objDB.BuildOracleParameter("pCursor",
System.Data.OracleClient.OracleType.Cursor,
ParameterDirection.Output,null,0);
OracleDataReader objOraReader =
objDB.RunExecuteOracleDataReader("pkg_Prod.sp_get_ CP", arrOracleParams);

while (objOraReader.Read())
{

object[] values = new object[objOraReader.FieldCount];

objOraReader.GetValues(values);

alList.Add(values);

}

return alList;

------
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:eq**************@TK2MSFTNGP10.phx.gbl...
alLCPList itself is an arraylist, because, as you say, GetLCPack returns

an
arraylist...but what does the items within the array list contain?

ArrayList arr = new ArrayList()
arr.Add("1");
arr.Add("2");
arr.Add("3");
string value = (string)arr[1];
ArrayList arr = new ArrayList()
arr.Add(1);
arr.Add(2);
arr.Add(3);
int value = (int)arr[0];

ArrayList arr = new ArrayList()
arr.Add(new DataSet());
arr.Add(new DataSet());
arr.Add(new DataSet());
DataSet value = (DataSet)arr[2];
if you are casting an item of the arraylist to arraylist, than this item
must be an arraylist, ala:

ArrayList arr = new ArrayList()
arr.Add(new ArraList());
arr.Add(new ArraList());
arr.Add(new ArraList());
ArraListvalue = (ArraList)arr[1];
If you are getting an invalid cast, then the items within your alLCPlist
ArrayList aren't embedded ArrayLists..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"gane kol" <ga*****@softca.com> wrote in message
news:uI**************@TK2MSFTNGP10.phx.gbl...
Well, the code before the for loop is
--------
ArrayList alLCPlist = new ArrayList();

DataRow dr;

for (int i=0;i<infoNode.ChildNodes.Count;i++)

{

currentNode = infoNode.ChildNodes[i];

alLCPlist = product.GetLCPack(strvalue,
currentNode.Attributes["Name"].Value.ToString());

if (alLCPlist.Count > 0)

{

------

alLCPlist gets the data returned from the dataaccess layer.
product.GetLCPack returns an arraylist.

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:Or**************@TK2MSFTNGP15.phx.gbl...
> Without seeing what alLCPlist is and how it's populated, it's quite hard to
> help. Clearly,alLCPlist doesn't contain arraylists...
>
> Karl
>
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/ - New and Improved (yes, the popup is
> annoying)
> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to > come!)
> "gane kol" <ga*****@softca.com> wrote in message
> news:O2**************@TK2MSFTNGP12.phx.gbl...
> > Hi,
> >
> > I have a code that creates a datatable from an arraylist, but i am
getting
> > an error in casting in
> >
> > for (int intRow = 0; intRow < alLCPlist.Count; intRow++)
> > {
> > DataRow drow = dtLCPack.NewRow();
> > int intColCount = dtLCPack.Columns.Count;
> > ArrayList arrlRow = (ArrayList)alLCPlist[intRow]; <== Specified > cast
> > is not valid here
> > for (int intCol=0; intCol < intColCount; intCol++)
> > {
> > drow[intCol] = arrlRow[intCol];
> > }
> > dtLCPack.Rows.Add(drow);
> > }
> > Can someone help me how to fix it?
> >
> > Thanks
> > Gane
> >
> >
>
>



Nov 19 '05 #6

P: n/a
That works great,
I appreciate that Karl,

Thanks.
Gane

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
Looks like you are converting it to an array of objects...which is very
different than an array list.

try

object[] arrlRow = (object[])alLCPlist[intRow];

in the initial code you sent ...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"gane kol" <ga*****@softca.com> wrote in message
news:OJ**************@TK2MSFTNGP14.phx.gbl...
Actually i am converting a datareader to arraylist.

----------
arrOracleParams[0] = objDB.BuildOracleParameter("Product",
System.Data.OracleClient.OracleType.VarChar,
ParameterDirection.Input,Product,50);
arrOracleParams[1] = objDB.BuildOracleParameter("Type",
System.Data.OracleClient.OracleType.VarChar,
ParameterDirection.Input,Type,20);
arrOracleParams[2] = objDB.BuildOracleParameter("pCursor",
System.Data.OracleClient.OracleType.Cursor,
ParameterDirection.Output,null,0);
OracleDataReader objOraReader =
objDB.RunExecuteOracleDataReader("pkg_Prod.sp_get_ CP", arrOracleParams);

while (objOraReader.Read())
{

object[] values = new object[objOraReader.FieldCount];

objOraReader.GetValues(values);

alList.Add(values);

}

return alList;

------
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:eq**************@TK2MSFTNGP10.phx.gbl...
alLCPList itself is an arraylist, because, as you say, GetLCPack returns
an
arraylist...but what does the items within the array list contain?

ArrayList arr = new ArrayList()
arr.Add("1");
arr.Add("2");
arr.Add("3");
string value = (string)arr[1];
ArrayList arr = new ArrayList()
arr.Add(1);
arr.Add(2);
arr.Add(3);
int value = (int)arr[0];

ArrayList arr = new ArrayList()
arr.Add(new DataSet());
arr.Add(new DataSet());
arr.Add(new DataSet());
DataSet value = (DataSet)arr[2];
if you are casting an item of the arraylist to arraylist, than this
item must be an arraylist, ala:

ArrayList arr = new ArrayList()
arr.Add(new ArraList());
arr.Add(new ArraList());
arr.Add(new ArraList());
ArraListvalue = (ArraList)arr[1];
If you are getting an invalid cast, then the items within your alLCPlist ArrayList aren't embedded ArrayLists..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"gane kol" <ga*****@softca.com> wrote in message
news:uI**************@TK2MSFTNGP10.phx.gbl...
> Well, the code before the for loop is
> --------
> ArrayList alLCPlist = new ArrayList();
>
> DataRow dr;
>
> for (int i=0;i<infoNode.ChildNodes.Count;i++)
>
> {
>
> currentNode = infoNode.ChildNodes[i];
>
> alLCPlist = product.GetLCPack(strvalue,
> currentNode.Attributes["Name"].Value.ToString());
>
> if (alLCPlist.Count > 0)
>
> {
>
> ------
>
> alLCPlist gets the data returned from the dataaccess layer.
> product.GetLCPack returns an arraylist.
>
>
>
> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> > wrote in message news:Or**************@TK2MSFTNGP15.phx.gbl...
> > Without seeing what alLCPlist is and how it's populated, it's quite hard
> to
> > help. Clearly,alLCPlist doesn't contain arraylists...
> >
> > Karl
> >
> > --
> > MY ASP.Net tutorials
> > http://www.openmymind.net/ - New and Improved (yes, the popup is
> > annoying)
> > http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ
(more
to > > come!)
> > "gane kol" <ga*****@softca.com> wrote in message
> > news:O2**************@TK2MSFTNGP12.phx.gbl...
> > > Hi,
> > >
> > > I have a code that creates a datatable from an arraylist, but i

am > getting
> > > an error in casting in
> > >
> > > for (int intRow = 0; intRow < alLCPlist.Count; intRow++)
> > > {
> > > DataRow drow = dtLCPack.NewRow();
> > > int intColCount = dtLCPack.Columns.Count;
> > > ArrayList arrlRow = (ArrayList)alLCPlist[intRow]; <==

Specified
> > cast
> > > is not valid here
> > > for (int intCol=0; intCol < intColCount; intCol++)
> > > {
> > > drow[intCol] = arrlRow[intCol];
> > > }
> > > dtLCPack.Rows.Add(drow);
> > > }
> > > Can someone help me how to fix it?
> > >
> > > Thanks
> > > Gane
> > >
> > >
> >
> >
>
>



Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.