MartGriff
MartGriff

Reputation: 2851

Return Object From Webservice

How can I retuen a Object from a web service:

[WebMethod] public DataSet GetVendors(string Database) { SqlConnection sqlConn = new SqlConnection();

        sqlConn.ConnectionString = GetConnString(Database);

        // build query
        string strSQL = @"  SELECT      [No_] AS [VendorNo],
                                        LTRIM([Name]) AS [VendorName]

                            FROM        [********_$Vendor]

                            WHERE       LEN(RTRIM([Name])) > 0 /* avoid blank names */

                            AND         [Vendor Posting Group] = 'VEND'

                            ORDER BY    LTRIM([Name]) ASC; /* LTRIM fixes spaces before name */ ";

        SqlDataAdapter da = new SqlDataAdapter(strSQL, sqlConn);

        DataSet ds = new DataSet();

        da.Fill(ds, "Vendors");

        return (ds);
    }

Upvotes: 0

Views: 1465

Answers (6)

Alex Fort
Alex Fort

Reputation: 18821

If you're asking how to return any kind of object, not an Object, then you're going to need to serialize that object. If you're trying to serialize a DataSet, I would suggest making it into a List or other data structure first.

Upvotes: 0

Perpetualcoder
Perpetualcoder

Reputation: 13571

IMO returning datasets from your web services is not such a great idea. It assumes that the clients understand the Dataset, DataTable etc data structures and classes. Instead I recommend you use plain CLR object i.e. Data Transfer Objects or if you want you may use XmlNode as your datatype.You can secure your web service using WSE.

Upvotes: 0

duffymo
duffymo

Reputation: 308753

Another reason not to return DataSet is "leaky abstraction": why expose the client to anything having to do with the persistence tier? Rethink it.

Upvotes: 0

duffymo
duffymo

Reputation: 308753

Seems brittle to me. It presumes that the client will know how to deserialize that object, which locks the client into a particular platform (e.g. Java EE or .NET).

XML serialization is less brittle because it's platform-agnostic and leaves unmarshalling quirks to be resolved by the client. I'd recommend that over returning an object.

Upvotes: 0

Austin Salonen
Austin Salonen

Reputation: 50215

An alternative would be to return the dataset xml as a string and create a dataset from it on the client side.

Although I'm sure encrypting the object would be fairly straightforward, this approach helped me out when my web services needed encryption (serialize everything, encrypt that string, return string, decrypt, deserialize).

Upvotes: 0

Wes P
Wes P

Reputation: 9870

If I'm interpreting your question properly, populate an object on your end with your information in the DataSet and set your return type to object. Or just return the object you populate as that object.

Upvotes: 1

Related Questions