473,320 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I convert a DataSet to a String from a WebService

Hi, I have a webservice that just returns a count:

public DataSet HelloWorld()
{
OdbcConnection conn = new OdbcConnection("DSN=xxx");
String sqlString = "select count(*) as employee from employees";
DataSet myResults = new DataSet();
OdbcDataAdapter myAdapter = new OdbcAdapter(sqlString, conn);
myAdapter.Fill(myResults);
return myResults;
}

In my page that consumes the web service I have this:

on a button click

localhost.Service1 GetCount = new locahost.Service1();
Label1.Text = Convert.ToString((DataSet) GetCount.HelloWorld());
Now that returns is this:

System.Data.DataSet

in my label. How do I get the data in the label? If I run the
webservice itself I get my count, so I know that part is working.

Thank you for any help.

Jan 5 '06 #1
5 6820
The data in a dataset is in XML format. You'll need to use the WriteXml
method to directly get the contents. You can write it to a string,
textwriter, etc. and than dump it.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

<ne***********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi, I have a webservice that just returns a count:

public DataSet HelloWorld()
{
OdbcConnection conn = new OdbcConnection("DSN=xxx");
String sqlString = "select count(*) as employee from employees";
DataSet myResults = new DataSet();
OdbcDataAdapter myAdapter = new OdbcAdapter(sqlString, conn);
myAdapter.Fill(myResults);
return myResults;
}

In my page that consumes the web service I have this:

on a button click

localhost.Service1 GetCount = new locahost.Service1();
Label1.Text = Convert.ToString((DataSet) GetCount.HelloWorld());
Now that returns is this:

System.Data.DataSet

in my label. How do I get the data in the label? If I run the
webservice itself I get my count, so I know that part is working.

Thank you for any help.

Jan 5 '06 #2
Thank you. I used GetXML() and it returned what I wanted, but I am not
sure that was the right way to do it. What if it had had more than one
result?

Jan 5 '06 #3
ne***********@gmail.com wrote:
Thank you. I used GetXML() and it returned what I wanted, but I am not
sure that was the right way to do it. What if it had had more than one
result?


Actually, you should just bind the Text to a certain value in the
DataSet. Since that's the type you're working with. GetXML is actually
creating XML on the fly and is not an efficient way of getting values
out of the DataSet (the DataSet is not stored as XML in memory).

In other words, do something like
DataSet myDataSet = (DataSet)GetCount.HelloWorld();
Label1.Text = myDataSet.Tables[0].Rows[0][0];

Assuming this is the first column, of the first row, of the first table
in the dataset (the only value in the DataSet). BTW, if this is the
only value being returned in this WebMethod, I'd recommend not using a
DataSet. It has alot of overhead; use a primitive type like int for the
return type of HelloWorld instead.

--
craig
Microsoft MVP - ASP/ASP.NET
Jan 5 '06 #4
I also noticed your 'more than one result' comment just now...see the
properties of the DataSet, DataTable, DataRow, etc. I alluded to in my
code. Once you understand these objects and how they tie together and
represent data, then you'll be on your way to a better understanding of
how to do this and actual databinding with DataSets in more complex
cases down the road....

A DataSet is made up of 0 or many DataTables, which is made up of 0 or
many DataRows. So think of a DataSet as a snapshot of a portion of a
database (a collection of tables), the DataTable is one of those tables,
and a DataRow is a row in one of the tables.

Notice in my code I drilled down using numbered indexes, since it seemed
you only had one value (a DataSet with one table with one row with one
value); for most of these indexes you should find values by using names
(e.g. if I have a column "foo" on a table called "table1", use the names
to access values, not how the data is ordered structurally).

Example:

Label1.Text = myDataSet.Tables["table1"].Rows[0]["foo"];

Craig Deelsnyder wrote:
ne***********@gmail.com wrote:
Thank you. I used GetXML() and it returned what I wanted, but I am not
sure that was the right way to do it. What if it had had more than one
result?


Actually, you should just bind the Text to a certain value in the
DataSet. Since that's the type you're working with. GetXML is actually
creating XML on the fly and is not an efficient way of getting values
out of the DataSet (the DataSet is not stored as XML in memory).

In other words, do something like
DataSet myDataSet = (DataSet)GetCount.HelloWorld();
Label1.Text = myDataSet.Tables[0].Rows[0][0];

Assuming this is the first column, of the first row, of the first table
in the dataset (the only value in the DataSet). BTW, if this is the
only value being returned in this WebMethod, I'd recommend not using a
DataSet. It has alot of overhead; use a primitive type like int for the
return type of HelloWorld instead.

--
craig
Microsoft MVP - ASP/ASP.NET
Jan 6 '06 #5
Thanks.
I changed a little. I am trying to put the data in a datagrid or just
print the xml on the screen. Client:

DataSet ds = new DataSet();
ds = GetCount.HelloWorld();
DataGrid1.DataSource=ds;
DataGrid1.DataBind();

When I compile I get this:

Cannot implicitly convert type 'System.Xml.XmlNode' to
'System.Data.DataSet'

But I returned a DataSet from my [WebMethod], so I don't understand.
I changed from above from just getting a count to several rows. All I
want to do read the xml returned from the web service (or is it
webservice) and display it on the screen. Again, thank you.

Jan 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Programatix | last post by:
Hi, I'm working on a project which includes WebServices and Windows Form application. The Windows Form application will call the WebServices to retrieve data from database. The data will be...
8
by: Programatix | last post by:
Hi, I'm working on a project which includes XML WebServices and Windows Form application. The Windows Form application will call the XML WebServices to retrieve data from database. The data...
0
by: TK | last post by:
Hi, I need help on datagrid. I have a dataset exposed as a webservice. I do have a web application that is supposed to consume the service on a datagrid.
2
by: Ben Turner | last post by:
I'm querying a web service to get some global weather data (http://www.webservicex.net/globalweather.asmx?WSDL) which works really well, however I'm getting stuck on the returned data for the...
1
by: J. Askey | last post by:
I am implementing a web service and thought it may be a good idea to return a more complex class (which I have called 'ServiceResponse') in order to wrap the original return value along with two...
0
by: Michael Kugler | last post by:
I call a webservice written in Visual FoxPro which returns an xml string like this <?xml version = "1.0" encoding="Windows-1252"...
0
by: Michael Kugler | last post by:
I call a webservice written in Visual FoxPro which returns an xml string like this <?xml version = "1.0" encoding="Windows-1252"...
1
by: shakthi | last post by:
Hi all,....i am developing an pocket PC application which consumes USZip Webservice and display the data. USZip has a GetInfoByZip(String zipcode) method which retuns an XML data...... For...
1
by: Cezus | last post by:
Hello, I cannot convert the following query in the dataset to a string. It says it cannot get more then 2034 chars long... the string just ends at 2034 characters... this is where it goes...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.