Solved

ASP.Net  datatable to xml

Posted on 2009-05-07
7
347 Views
Last Modified: 2013-12-17
I keep getting an error

System.NullReferenceException: Object reference not set to an instance of an object.
   at WebService.PropertyList() in c:\Users\Amac\Amac WebIT\WebProjects\AmacCMS\App_Code\WebService.cs:line 52

wich is the return line.

Can any one help as to what i'm doing wrong.

i am looking to get the results from a table adaptor return as an xml string;

[WebMethod]
    public string PropertyList()
    {
        HotelsDataSetTableAdapters.hotelsTableAdapter hotelAdaptor = new HotelsDataSetTableAdapters.hotelsTableAdapter();
        HotelsDataSet.hotelsDataTable hotels = new HotelsDataSet.hotelsDataTable();
        hotelAdaptor.FillAllHotels(hotels);
 
        return hotels.DataSet.GetXml();
 
    }

Open in new window

0
Comment
Question by:amacleod1983
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 24325272
That's true coz Hotels.DataSet will give u null. It is not inside a dataset.

U'll have to declare a dataset and set the Table[0] of the dataset.

Then u can render the dataset to xml
0
 
LVL 3

Author Comment

by:amacleod1983
ID: 24325329
could you possible supply some sample code?
0
 
LVL 20

Expert Comment

by:informaniac
ID: 24325424
The problem is I don't know what is inside

HotelsDataSetTableAdapters

and HotelsDataSet

But this is my guess...

HotelsDataSet dsHotels = new HotelsDataSet();

hotelAdaptor.Fill(dsHotels,"hotels");

return dsHotels.GetXml();
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:amacleod1983
ID: 24325529
What would you like to know?
Capture.JPG
0
 
LVL 3

Accepted Solution

by:
amacleod1983 earned 0 total points
ID: 24326883
got it working with this

but there must be a better way
HotelsDataSetTableAdapters.hotelsTableAdapter hotelAdaptor = new HotelsDataSetTableAdapters.hotelsTableAdapter();
        HotelsDataSet.hotelsDataTable hotels = hotelAdaptor.GetAllHotels();
        StringWriter sw = new StringWriter();
        hotels.WriteXml(sw);
        return sw.ToString();

Open in new window

0
 
LVL 20

Expert Comment

by:informaniac
ID: 24327481
That would be very much fine...

HotelsDataSet what is this?
0
 
LVL 20

Expert Comment

by:informaniac
ID: 24327517
Oh sorry.. That's the dataset.....
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question