Reading dataset from XML problem

D

David

Hi,

(Sorry for duplicate post, finger trouble before I finished...)

using C# 1.1

I am writing a winform app that collects a dataset from a webservice.

At the same time I collect the data, I write the dataset out to the local
disk using DataSet.WriteXml, so that if the webservice is not available,
then the app will continue to run from my saved dataset. If the web service
is not available, then I load the dataset with the ReadXml method.

All appears to work excellent... However, you may have guessed, I have a
problem.

On form load, I load the dataset into a list box datasource. Most of my
application is happy with this. The listbox loads properly and can be used.

BUT...
in one point of the app, I was reading (DataTable)listbox.DataSource (I have
changed it now, but it made no difference) then

DataRow[] DR = DT.Select("ID = " +
Convert.ToInt32(SiteListBox.SelectedValue.ToString()));

then trying to read the DR by...

int SiteCount = Convert.ToInt32(DR[0]["SiteCount"].ToString());

This works almost as expected, but some rows in the dataset (Only when
offline and when the dataset is loaded from the XML) are not being read.

It is odd, but the first 23 rows read OK, the 24, 25 and 26 give an error,
then some later rows work and some others don't.

While in debug command window, (app is Offline)

DR[0]["SiteCount"] <-- THIS ONE WORKS
"4"
DR[0]["SiteCount"] <-- THIS ONE FAILS.
error: 'DR' is not an array or pointer. Indexing is not available

If I run the app while online, and do the same...


DR[0]["SiteCount"]
{4}
System.ValueType: {System.Int32}
m_value: 4
MaxValue: 2147483647
MinValue: -2147483648
DR[0]["SiteCount"]
{16}
System.ValueType: {System.Int32}
m_value: 16
MaxValue: 2147483647
MinValue: -2147483648

I have looked at the XML. An example is...
<Table>
<ID>68</ID>
<SiteName>BradfordFOCUS.Com</SiteName>
<TownName>Bradford</TownName>
<Colour1>#003399</Colour1>
<Colour2>#FFFFFF</Colour2>
<SiteCount>13</SiteCount>
</Table>

I am selecting on the ID field, though the app seems to fail where the ID
number is not in sequential order, though this is not a guaranteed fault as
it can still fail when in sequential order.

Can anyone point me in the right direction?

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

David said:
is happy with this. The listbox loads properly and can be used.
BUT...
in one point of the app, I was reading (DataTable)listbox.DataSource (I
have
changed it now, but it made no difference) then

DataRow[] DR = DT.Select("ID = " +
Convert.ToInt32(SiteListBox.SelectedValue.ToString()));

then trying to read the DR by...

int SiteCount = Convert.ToInt32(DR[0]["SiteCount"].ToString());

This works almost as expected, but some rows in the dataset (Only when
offline and when the dataset is loaded from the XML) are not being read.

It is odd, but the first 23 rows read OK, the 24, 25 and 26 give an error,
then some later rows work and some others don't.

Can you post the exact error? (I assume it's an exception)

It is possible that the select you are doing returns no rows, are you taking
this into consideration?

What about if the listbox has nothing selected?
 
D

David

In the Debug window, the error is...
error: 'DR' is not an array or pointer. Indexing is not available

(I can't get the error from the app at the moment, as I am in the middle of
writing a large method to connect to a web method, so cannot build with the
error in place. I have gotten around the failure by doing a foreach on the
datarow[], which bypasses it, however, there is definately data available.)

There is definately data there. I have opened the XML myself and navigated
to the row.

The method in question is actually in the OnSelectIndex of the listbox, so
can only happen at that point in time.

It is weird that it all works when connected to the dataset from the web
service, but not the dataset from the XML. (The XML data is saved locally
(as a dataset.writexml) as soon as it is received from the webservice, for
use if the webservice is not available.)

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

David said:
is happy with this. The listbox loads properly and can be used.
BUT...
in one point of the app, I was reading (DataTable)listbox.DataSource (I
have
changed it now, but it made no difference) then

DataRow[] DR = DT.Select("ID = " +
Convert.ToInt32(SiteListBox.SelectedValue.ToString()));

then trying to read the DR by...

int SiteCount = Convert.ToInt32(DR[0]["SiteCount"].ToString());

This works almost as expected, but some rows in the dataset (Only when
offline and when the dataset is loaded from the XML) are not being read.

It is odd, but the first 23 rows read OK, the 24, 25 and 26 give an
error,
then some later rows work and some others don't.

Can you post the exact error? (I assume it's an exception)

It is possible that the select you are doing returns no rows, are you
taking this into consideration?

What about if the listbox has nothing selected?
 
D

David

I guess this is not as simple to fix as I thought...

I am still struggling with this, so any help would be most welcome.

Thinking further into it...

When I am connected to the webservice, the data type is shown when I do the
DR[0]["SiteName"] but not when I read from the XML file that I saved
earlier. If this is likely to be the issue, then how do I save the datatypes
to the XML and reload them when I read it from the XML?

I have been using dataset.WriteXml(filename) and dataset.ReadXml(filename).

Thanks.
--
Best regards,
Dave Colliver.
http://www.MyLocalFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
D

David

Sorted it...

When I read the dataset from the webservice, I am now writing out the schema
as well dataset.WriteXmlSchema, then when I read in the dataset from the
XML, I will read the dataset.ReadXmlSchema prior to ReadXml.

This appears to make it work.

--
Best regards,
Dave Colliver.
http://www.LeicesterFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


David said:
I guess this is not as simple to fix as I thought...

I am still struggling with this, so any help would be most welcome.

Thinking further into it...

When I am connected to the webservice, the data type is shown when I do
the DR[0]["SiteName"] but not when I read from the XML file that I saved
earlier. If this is likely to be the issue, then how do I save the
datatypes to the XML and reload them when I read it from the XML?

I have been using dataset.WriteXml(filename) and
dataset.ReadXml(filename).

Thanks.
--
Best regards,
Dave Colliver.
http://www.MyLocalFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


David said:
In the Debug window, the error is...
error: 'DR' is not an array or pointer. Indexing is not available

(I can't get the error from the app at the moment, as I am in the middle
of writing a large method to connect to a web method, so cannot build
with the error in place. I have gotten around the failure by doing a
foreach on the datarow[], which bypasses it, however, there is definately
data available.)

There is definately data there. I have opened the XML myself and
navigated to the row.

The method in question is actually in the OnSelectIndex of the listbox,
so can only happen at that point in time.

It is weird that it all works when connected to the dataset from the web
service, but not the dataset from the XML. (The XML data is saved locally
(as a dataset.writexml) as soon as it is received from the webservice,
for use if the webservice is not available.)

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top