By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,966 Members | 1,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,966 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.