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

N

needin4mation

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.
 
M

Mark Fitzpatrick

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
 
N

needin4mation

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?
 
C

Craig Deelsnyder

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.
 
C

Craig Deelsnyder

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 said:
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.
 
N

needin4mation

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top