简体   繁体   中英

Error while returning DataSet from XML Web Service

I have created Windows Application that needs to get data from SQL Server. In order to do that easy I also have XML Web Service and here is the code that I use to return DataSet:

[WebMethod]
public DataSet GetDataSet(SqlCommand cmd)
{
    using (SqlConnection Conn = new SqlConnection(ConnString))
    {
        cmd.Connection = Conn;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        try
        {
            //Conn.Open();
            using (DataSet DS = new DataSet())
            {
                da.Fill(DS);
                return DS;
            }
        }
        catch (Exception ex)
        {
            return (new DataSet());
        }
        finally
        {
            if (da != null)
            {
                da.Dispose();
            }
        }
    }
}

And when I want to Add my Web Service into my Win Application Project it gives the following Error:

Cannot serialize member System.ComponentModel.Component.Site of type System.ComponentModel.ISite because it is an interface

You may want to read the following article as I don't think you can Serialise an Interface object :-

http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/bac96f79-82cd-4fef-a748-2a85370a8510/

SqlCommand cannot be serialized, so you will need to instantiate in the function.

 [WebMethod]
 public DataSet GetDataSet()
 {
 SqlCommand cmd = new SqlCommand();
 using (SqlConnection Conn = new SqlConnection(ConnString))
 {
    cmd.Connection = Conn;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    try
    {
        //Conn.Open();
        using (DataSet DS = new DataSet())
        {
            da.Fill(DS);
            return DS;
        }
    }
    catch (Exception ex)
    {
        return (new DataSet());
    }
    finally
    {
        if (da != null)
        {
            da.Dispose();
        }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM