473,695 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(sql String, conn);
myAdapter.Fill( myResults);
return myResults;
}

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

on a button click

localhost.Servi ce1 GetCount = new locahost.Servic e1();
Label1.Text = Convert.ToStrin g((DataSet) GetCount.HelloW orld());
Now that returns is this:

System.Data.Dat aSet

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 6842
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.goo glegroups.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(sql String, conn);
myAdapter.Fill( myResults);
return myResults;
}

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

on a button click

localhost.Servi ce1 GetCount = new locahost.Servic e1();
Label1.Text = Convert.ToStrin g((DataSet) GetCount.HelloW orld());
Now that returns is this:

System.Data.Dat aSet

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***********@g mail.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)GetCou nt.HelloWorld() ;
Label1.Text = myDataSet.Table s[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.Table s["table1"].Rows[0]["foo"];

Craig Deelsnyder wrote:
ne***********@g mail.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)GetCou nt.HelloWorld() ;
Label1.Text = myDataSet.Table s[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.HelloW orld();
DataGrid1.DataS ource=ds;
DataGrid1.DataB ind();

When I compile I get this:

Cannot implicitly convert type 'System.Xml.Xml Node' to
'System.Data.Da taSet'

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
2017
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 returned as DataSet. Now, here's the problem. On .NET Framework 1.1, if any rows in the dataset returned contain errors (marked by calling the SetColumnError() method or
8
2399
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 will be returned as DataSet. Now, here's the problem. On .NET Framework 1.1, if any rows in the dataset returned contain errors (marked by calling the SetColumnError() method or
0
1391
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
2049
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 GetWeather call. The data returned is presented as a string but actually contains XML data. I'd like to treat the data as XML, and then save it to a file, append other results etc. so I can re-use the data later without having to requery the server (to...
1
5550
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 other properties... bool error; string lastError; My whole class looks like this... using System;
0
1005
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" standalone='yes'?><VFPData><curartikel><art_nr>12045</art_nr><farb_nr>26</farb_nr></curartikel></VFPData> Im unable to convert the XML string into a DataSet.This is what i've tried: Dim strResulFromWebService As String = ws.GetItems("12345") Dim MyStream = New System.IO.StringReader(strResulFromWebService)
0
856
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" standalone='yes'?><VFPData><curartikel><art_nr>12045</art_nr><farb_nr>26</farb_nr></curartikel></VFPData> Im unable to convert the XML string into a DataSet.This is what i've tried: Dim strResulFromWebService As String = ws.GetItems("12345") Dim MyStream = New System.IO.StringReader(strResulFromWebService)
1
4795
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 Example.... USZip u = new USZip();
1
2633
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 wrong in the code: string xml = Convert.ToString(mijnQTA.GetDataXMLForAuto());
0
8649
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8586
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9004
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8864
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4592
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3025
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2289
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1986
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.