Showing posts with label returned. Show all posts
Showing posts with label returned. Show all posts

Saturday, March 24, 2012

Working with a returned DataSet

After I returned a DataSet through a WebMethod, how do I work with it?
I didn't understand this part of Atlas.

1. I loop through this DataSet creating new dynamic tables, trs and tds to display this data?

2. Can I bind it to an asp.net control like a GridView and it willautomatically update the control via JS, just like it happens on theserver side binding?

I tried looping through the returned DataSet but it has some weirdfunctions and attributes not quite like a DataSet... so... how do I useit?

function RetInfo(id)
{
PageMethods.RetDataSet(id,OnComplete);
}

function OnComplete(ds)
{
//do something with the DataSet
//ds.Tables (?)
}

[WebMethod]
public DataSet RetDataSet(int id)
{
DataSet ds = new DataSet();
//...get data from the database
return ds;
}

I recommend you check out this writeup by Garbin:

http://aspadvice.com/blogs/garbin/archive/2006/03/05/15591.aspx


You might want to review this page as well:

http://atlas.asp.net/docs/atlas/doc/data/default.aspx#datatable


Thanks man, that's exactly what I was looking for.

Just two more questions though:

1. If use that xml binding, can I use a PageMethod instead of a webservice as DataSource?

2. And, is a PageMethod considered a WebService? Will it have the same security issues as WebServices do (like being accessible to anyone)?

Thanks!

Wednesday, March 21, 2012

XML and Webserivces

Hi All,

I have created a webservice that returns XML data to be processed by the client.

The returned XML contains a complex type that I created, as well as having a number of nested objects on which I have used the XMLARRAY / XMLARRAYITEM.

Fragment of test XML shown below...

<?xmlversion="1.0"encoding="utf-8"?>

<Testxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://tempuri.org">

<test1>201</test1>

<test2>Blah Blah</test2>

<test3>3</test3>

<test4>51</test4>

<TestTest>

<Inner>

<inner1>300</inner1>

<inner2>TEXT</inner2>

<inner3>TEXT</inner3>

<inner4>TEXT</inner4>

<inner5>TEXT</inner5>

<inner6>TEXT</inner6>

<inner7>TEXT</inner7>

<inner8>TEXT</inner8>

<inner9>TEXT</inner9>

<inner10>TEXT</inner10>

</Inner>

<Inner>

<inner1>301</inner1>

<inner2>TEXT</inner2>

<inner3>TEXT</inner3>

<inner4>TEXT</inner4>

<inner5>TEXT</inner5>

<inner6>TEXT</inner6>

<inner7>TEXT</inner7>

<inner8>TEXT</inner8>

<inner9>TEXT</inner9>

<inner10>TEXT</inner10>

</Inner> </TestTest>

</Test>

Now when I use the test harness the XML is generated exactly as I require.

Also I can reference the returned data on the client, for example by using arg.test1 would return '201'.

My probelms/question are these...

1> How do I access the inner array items. For example how would I access elements under TestTest?

2> How do I convert the returned data from the webservice into valid xml on the client? Every time I try and do this I get the error 'XML document must have a top level element'.

I cant understand why it says it is not valid as every other application I have has no problem with the XML that is generated,

Can anyone point me in the right direction as I cant see where the problem is.

Thanks in advance

Problem sortedSmile

Once I found out about the service using JSON by default it was very straightforward.

I looked at the JSON formatted data that was sent with the response. From there it was obvious how to call the array items.

For example arg.Level1[1].Level2Value

Didn't know much about JSON before this but it is a great format (compared to XML) to use when you want to handle stuff on the client in javascript. Even handles custom objects with ease.

Hope this helps somebody

XML Web Services Encoding but No Soap Message

Hi,

I have double byte characters in the content that I am returning using Web Services. However, the encoding in the xml file returned by Web Services is utf-8 and I am unable to read the content, not even by changing browser encoding setting to the appropriate one.

I implemented SoapExtension to modify the xml encoding before it returns to the client, which works when a WinForm calls it, but it doesn't work when a webpage calls it, and here is why:
http://www.dotnet247.com/247reference/msgs/45/228371.aspx
"If you're testing this by hitting the "Invoke" button on the .asmx page, it will never work. That execution path does not send any SOAP message to the ASP.NET runtime, and no SOAP Extension will ever get executed. You need to generate a client and use it to send some traffic at the service if you want to test your extension."

Then I created an aspx page to call my web service using Atlas, stepping through in debug mode, the code did not touch the SoapExntension at all. So Atlas doesn't get SoapMessage, therefore using SoapExtension to modify xml doesn't work?

How do I modify the xml that the Web Services send to me in this case? HttpHandler?

Thanks,
Janh

I forgot to mention that the data is stored in sql server as windows-1252 (or iso-8859-1), not nvarchar. If user inserts big5, it'll store big5 in windows-1252 format.

When I return the string to the client, I see ¤é?g?ü???Q|?|ü. The problem is, that no matter what browser encoding setting I change to, I always see this string. I don't know why but it should change to 日經指數昨收至 when I change browser encoding to big5.

I found something that kind of works. Since I know it is big5, I am able to do:

System.Text.Encoding iso = System.Text.Encoding.GetEncoding(1252);
System.Text.Encoding iso2 = System.Text.Encoding.GetEncoding(950); //code page for big5
byte[] isoBytes = iso.GetBytes(content); //content is the string from the database
newcontent = iso2.GetString(isoBytes);

send it via web service and Atlas to the client. It displays the string automatically and correctly in big5 encoding. But when I change the browser encoding setting to anything else, it is still stuck as the same big5 string. It is somehow fixed, unchangeable.

This behavior is the same if, instead of using Atlas and web service, I just createnew XMLHttpRequest()andxmlhttp.open("GET", "test.aspx",true) which the aspx page returns the same string as above. The string still has a fixed encoding regardless of browser encoding setting.

Is the problem in how XMLHttp is handling the string? That makes the string encoding unchangeable on the client side?

This is bad for me because I have no way of knowing what encoding to use. With this example, I converted the encoding to big5 because I know it's big5 in the database. I have many other languages in my database.

I would still like to use Atlas... but is there a way to NOT add any manual encoding at all, return the raw string to the client, and let client change the browser setting to view the correct encoding?


Sorry guys, I found my own answer.

Guess responseText is ALWAYS utf-8, so I am supposed to use responseXML in order to preserve the encoding.

So my new question is, how do I use responseXML in Atlas...? Or is there another way to preserve encoding?


How do we solve this? please?Big Smile

There is a way to tell Atlas to treat the response as XML instead of text, for webservices that you want to return XML instead of JSON, you do this thru the WebOperationAttribute on the asmx webmethod...

[WebOperation(true /*getVerbEnabled*/, ResponseFormatMode.Xml, false /*safeForCrossDomain*/)]

Hope that helps,
-Hao