ラベル dataset の投稿を表示しています。 すべての投稿を表示
ラベル dataset の投稿を表示しています。 すべての投稿を表示

2012年3月26日月曜日

Retrieve JSON result set

What is the best method for retrieving a result set and returning a JSON object? I tried using a DataSet, but received a serialization error.

TIA

You do not have to do anything special to convert in JSON. The Asp.net Ajax Automatically handles it. But in case you want to some manual proessing use the JavaScriptSerializer.


You do not have to do anything special to convert in JSON. The Asp.net Ajax Automatically handles it. But in case you want to some manual proessing use the JavaScriptSerializer. The DataSet is not supported with the Current Version, Add the January CTP to work with DataSet and DataTable.


I am a little confused. Can you provide an example of how to retrieve a JSON serialized result set? Where do I find the January CTP?

TIA


You can find the January CTP from this linkhttp://www.microsoft.com/downloads/details.aspx?familyid=4CB52EA3-9548-4064-8137-09B96AF97617&displaylang=en

2012年3月24日土曜日

return straight xml rather than dataset

i just deployed my first webservice. it works perfectly. now, however, i am trying to use it in livecycle and i think livecycle can't handle the dataset which is returned by my webservice. how can i convert my dataset into straight xml?

DataSet ds = new DataSet()
StringWriter writer = new StringWriter();
ds.WriteXml(writer);
return writer.ToString();

Returning a dataset from a web service

Say I have a page that calls a web service from a client function to get some data to bind a gridView like this:

-------------------

<scripttype="text/javascript"language="JavaScript">

function GetDataSet(){

var ds = SimpleService.ActiveProcess();

}

</script >

-------------------

And the web service looks something like this:

-------------------

[WebMethod]

public datasetActiveProcess(){

string connectionString ="Connstring stuff here";

DataSet ds = csql.GetActiveProcess(connectionString);

return ds;

}

-------------------

Of course, because I'm posting this problem, this isn't the correct way to implement this kind of solution...

Question is: Is there a better way? Or how do I manage to handle and display what's being returned from this web service in the web page?

thanks.

doug

This issue is associated with an Atlas Web Service call.

Here is a crude solution that I still need to refine:

------------------------

1. JavasScript call to webservice:

------------------------

function fnSetTimeout( ){

//this is the call to the web service.

var ds = SimpleService.ActiveProcess(OnComplete, OnTimeout);

}

------------------------

2. Webservice method

------------------------

[WebMethod]

publicstring ActiveProcess(){

string connectionString ="Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=simple2;Data Source=local

string xmlReader = csql.GetActiveProcess(connectionString);

return xmlReader;

}

------------------------

3 Webservice method

------------------------

publicstring GetActiveProcess(string connString)

{

string strSQL ="Select id, threadid, conversationId from ActiveProcess";

SqlConnection conn =newSqlConnection(connString);

SqlDataAdapter da =newSqlDataAdapter(strSQL, conn);

DataSet ds =newDataSet();

try

{

conn.Open();

da.Fill(ds);

XmlDataDocument xmlDataDocument =newXmlDataDocument(ds);

return xmlDataDocument.OuterXml.ToString();

}

catch (SqlException ex)

{

returnnull;

}

}

------------------------

3 Load the XML into parser

------------------------

/*==============================================================================

function: OnComplete

paramaters: result

Purpose: Gets the results from the ATLAS call and parses into an XML document

Date: 12/21/2005

==============================================================================*/

function OnComplete(result)

{

//Load XML

var XMLDoc =new ActiveXObject("Microsoft.XMLDOM");

try

{

XMLDoc.loadXML(result);

}

catch(err)

{

alert(err.description);

}

//Load XSL

var XSLDoc =new ActiveXObject("Microsoft.XMLDOM");

try

{

XSLDoc.async =false;

XSLDoc.load("ActiveRecords.xsl");

}

catch(err)

{

alert(err.description);

}

//Write the results of the transformation out to the div.

document.all.divTable.innerHTML = XMLDoc.transformNode(XSLDoc);

}

Here's the XSL if you're interested:

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

<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:templatematch="/">

<html>

<head>

<title>Active</title>

<style>

copy { font-family: Arial, Verdana;}

h1 { font-family: Arial, Verdana; font-size: 14pt;}

p { font-family: Arial, Verdana; font-size: 10pt;}

</style>

</head>

<body>

<p><fontface="verdana">Active</font></p>

<tableborder="1">

<tr>

<thclass="copy">ID</th>

<thclass="copy">Thread Id</th>

<thclass="copy">Configuration ID</th>

</tr>

<xsl:for-eachselect ="NewDataSet/Table">

<tr>

<td>

<xsl:value-ofselect="id"/>

</td>

<td>

<xsl:value-ofselect="threadid"/>

</td>

<td>

<xsl:value-ofselect="conversationId"/>

</td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

thanks...

Returning a dataset

I am using the Atlas bits that run on the RC build. I thought that there was the capability to return a dataset. Is that working at this point? I am getting a strange error where the local WebDev.WebServer.exe is generating an error and then exiting when I attempt to call a webservice that returns a dataset. The code is pretty simple, so I have included it here just incase I am doing something stupid. I am able to step thru my code, I exit from the method, and then bang I get the error on the terminal. Thoughts? Do I need to change what I am doing? Did I just make up the thought that Datasets were working at this point?
Wally

[WebMethod]

public System.Data.DataSet ReturnDataSet()

{

DataSet ds =newDataSet();

DataTable dt =newDataTable();

DataRow dr;

dt.Columns.Add(newDataColumn("tblStateId", System.Type.GetType("System.Int32")));

dt.Columns.Add(newDataColumn("State", System.Type.GetType("System.String")));

dr = dt.NewRow();

dr["tblStateId"] = 1;

dr["State"] ="Tennessee";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr["tblStateId"] = 2;

dr["State"] ="Alabama";

dt.Rows.Add(dr);

ds.Tables.Add(dt);

return (ds);

}

Interesting. This looks like a bug in Atlas. When I try your code, I get a StackOverflow exception in the serialization process.

Yes, the StackOverflow Exception is what I got also. Sorry fornot being descriptive enough last night. I was a little tired andI am getting the flu. Sad [:(]

I suspect a bug in the DataSetConverter, which is probably caused by circular references between the DataSet and its DataTables.
What you could do for now is to return a DataTable instead (and not use a DataSet at all).

publicoverridestring Serialize(object o)
{
if (!(ois DataSet))
{
thrownew ArgumentException();
}
DataSet set1= (DataSet) o;
StringBuilder builder1=new StringBuilder();
builder1.Append('[');
bool flag1=true;
IEnumerator enumerator1= set1.Tables.GetEnumerator();
try
{
while (enumerator1.MoveNext())
{
DataTable table1= (DataTable) enumerator1.Current;
if (!flag1)
{
builder1.Append(',');
}
builder1.Append(JavaScriptObjectSerializer.Serialize(set1));
flag1=false;
}
}
finally
{
IDisposable disposable1= enumerator1as IDisposable;
if (disposable1 !=null)
{
disposable1.Dispose();
}
}
builder1.Append(']');
return builder1.ToString();
}


Variable table1 wasn't used.
Serialize(set1) rises Stack Overflow exception

Returning a datatable

Based on trying to return a DataSet, I have decided to try and justreturn a DataTable (fyi. http://forums.asp.net/1077805/ShowPost.aspx).
The problem is that in my javascript callback, I need to get at thedata that is returned. What methods are available on the returneddata? I have a datatable with several datarows, but I don't knowwhat methods are available to actually get at the data. BTW, I amtrying to do this programmatically and am not interested in adeclarative solution to this. When I attempt to alert out to theUI, result.length is coming through a undefined.
Web Service:
[WebMethod]
public System.Data.DataTable ReturnDataSet()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("tblStateId",System.Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("State",System.Type.GetType("System.String")));
dr = dt.NewRow();
dr["tblStateId"] = 1;
dr["State"] = "Tennessee";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["tblStateId"] = 2;
dr["State"] = "Alabama";
dt.Rows.Add(dr);
return (dt);
}
Client Side Javascript:
function LoadTest(){
Samples.AspNet.WebServiceTest.ReturnDataSet(ReturnDataTableCallBack);
}

function ReturnDataTableCallBack(result)
{
var i = 0;
var ddl = document.getElementById("sState");
var optionItem;
iLength = document.getElementById("sState").options.length;
document.getElementById("sState").visible = true;
document.getElementById("txtAreaResult").value = result.get_data();
for(i=0; i<iLength; i++)
{
document.getElementById("sState").options[0] = null;
}

for(i=0; i<result.length; i++)
{
document.getElementById("sState").options.add(newOption(resultIdea [I]["State"],resultIdea [I]["tblStateId"]))
}
}


I think you should be able to write code like:
for (var i = 0; i < results.get_length(); i++) {
alert(results.getItem(i).State);
}
As a side note: there are several ways that you can use to 'discover'the structure of an object. One way is to usedebug.dump(object,name[,recursive[,indentationPadding]]), which shouldadd information about an object to the trace. Another way is to loopthrough the members of an object, like "for (m in myObject) alert(m);".

Great information. It got me on the right track. I foundthat the following pseudo-javascript was what was necessary:
for(i=0; i<result.get_length(); i++)
{
var optAdd = newOption(result.getItem(i).getProperty("State"),result.getItem(i).getProperty("tblStateId"));
document.getElementById("sState").options.add(optAdd);
}
The .State and .tblStateId properties did not return a value, but thegetProperty("State") and getProperty("tblStateId") did what wasnecessary.
Do you have a complete example of the debug.dump() method?
Wally

You could use "get_State()" instead of "getProperty('State')" too (which I think is nicer).
To use debug.dump, try something like "debug.dump(results, "my results", true)".

How would you do this declaratively? For example, if I wanted to bind the first state returned to a label...I've tried this, and it doesn't work:
<binding id="binding1" dataContext="dataSource1" dataPath="data[0].State" property="text" automatic="false" />
I can't figure out what to put in the dataPath. Anyone have some guidance?