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

Discussion in 'ASP .Net' started by needin4mation@gmail.com, Jan 5, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

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

    <> wrote in message
    news:...
    > 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.
    >
     
    Mark Fitzpatrick, Jan 5, 2006
    #2
    1. Advertising

  3. Guest

    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, 2006
    #3
  4. 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
     
    Craig Deelsnyder, Jan 5, 2006
    #4
  5. 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:
    > 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
     
    Craig Deelsnyder, Jan 5, 2006
    #5
  6. Guest

    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 6, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nedu N
    Replies:
    1
    Views:
    1,802
    Chris Taylor
    Oct 31, 2003
  2. JimO

    DataSet and dataSet

    JimO, Mar 1, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    526
    Mary Chipman [MSFT]
    Mar 8, 2006
  3. Ben Turner
    Replies:
    2
    Views:
    9,132
    Ken Cox [Microsoft MVP]
    Jan 2, 2004
  4. thuy_t_bird
    Replies:
    0
    Views:
    531
    thuy_t_bird
    Jun 3, 2009
  5. Replies:
    2
    Views:
    180
    John Bokma
    Aug 30, 2013
Loading...

Share This Page