Question

I'm building a simple ASP.NET application that communicates with a SQL server database. In a separate project, I have the dataset that was generated by Visual Studio. I'm trying to expose an API that will display all users in the database, but I'm getting an exception.

Here is the code:

public class UserController : ApiController {

    public IEnumerable<GWDataSet.usersRow> GetAllUsers() {
        GWDataSet gw = new GWDataSet();
        usersTableAdapter adapter = new usersTableAdapter();
        adapter.Fill(gw.users);

        return gw.users.AsEnumerable();
    }
}

And this is the exception:

Type 'System.Data.DataRow' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

Is there a way around this, other than to manually edit the system-generated code? My fear is that next time I make a change to the dataset, it will overwrite all the datacontract elements I add in. I would think there's a way to do this, but I can't seem to find it.

Thanks!

Was it helpful?

Solution

It is because you cannot share DataRow objects. They are not serializable. If you want to share it, you should create DTO objects. It is a good pratice of shareing objects from services, APIs, etc. Tru to convert your DataRow to a DTO object. Try something like:

Create your class to be this DTO, for sample:

[Serializable]
public class UserDTO
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string Birthday { get; set; }
    /* other properties you need */
}

And in your web api, try this:

public class UserController : ApiController {

    public IEnumerable<UserDTO> GetAllUsers() {
        GWDataSet gw = new GWDataSet();
        usersTableAdapter adapter = new usersTableAdapter();
        adapter.Fill(gw.users);

        List<UserDTO> list = new List<UserDTO>();

        foreach(DataRow row in gw.users.Rows) 
        {
            UserDTO user = new UserDTO();
            user.FirstName = row["Name"].ToString();
            // fill properties

           list.Add(user);
        }

        return list;
    }
}

OTHER TIPS

Going off of Felipe's answer (and after remembering that I need to deal with the dreaded DBNull), here is the final method I built:

    public IEnumerable<GW.Entities.user> GetAllUsers() {
        try {
            GWDataSet gw = new GWDataSet();
            List<GW.Entities.user> users = new List<user>();
            usersTableAdapter adapter = new usersTableAdapter();

            adapter.Fill(gw.users);

            foreach (GWDataSet.usersRow row in gw.users.Rows) {
                users.Add(new GW.Entities.user {
                    UserId = row.IsNull("UserId") ? 0 : row.UserId,
                    UserName = row.IsNull("UserName") ? "" : row.UserName,
                    EmailAddress = row.IsNull("EmailAddress") ? "" : row.EmailAddress,
                    UserPasswordLastChange = row.IsNull("UserPasswordLastChange") ? DateTime.MinValue : row.UserPasswordLastChange,
                    LastLogin = row.IsNull("LastLogin") ? DateTime.MinValue : row.LastLogin,
                    StatusCd = row.IsNull("StatusCd") ? "" : row.StatusCd,
                    LastTimestamp = row.IsNull("LastTimestamp") ? null : row.LastTimestamp
                });
            }

            return users;
        } catch (Exception ex) {
            Debug.WriteLine(ex.ToString());
            return null;
        }
    }

Which gives me an output of:

<ArrayOfuser xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/GW.Entities">
    <user>
        <EmailAddress>seraieis@gmail.com</EmailAddress>
        <LastLogin>2012-12-19T20:48:26.41</LastLogin>
        <LastTimestamp>AAAAAAAARlI=</LastTimestamp>
        <StatusCd>A</StatusCd>
        <UserId>1</UserId>
        <UserName>seraieis</UserName>
        <UserPasswordLastChange>0001-01-01T00:00:00</UserPasswordLastChange>
    </user>
</ArrayOfuser>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top