Solved

ASP.Net  datatable to xml

Posted on 2009-05-07
7
343 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
  • 4
  • 3
7 Comments
 
LVL 20

Expert Comment

by:informaniac
Comment Utility
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
Comment Utility
could you possible supply some sample code?
0
 
LVL 20

Expert Comment

by:informaniac
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Author Comment

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

Accepted Solution

by:
amacleod1983 earned 0 total points
Comment Utility
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
Comment Utility
That would be very much fine...

HotelsDataSet what is this?
0
 
LVL 20

Expert Comment

by:informaniac
Comment Utility
Oh sorry.. That's the dataset.....
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now