views:

17

answers:

2

I have third party api's which require string values to be submitted as empty strings. On an asp.net page I can use this code (abbreviated here) and it works fine:

public class Customer
{
    private string addr1 = "";
    public string Addr1
    {
        get {return addr1;}
        set {addr1 = value;}
    }

    private string addr2 = "";
    public string Addr2
    {
        get {return addr2;}
        set {addr2 = value;}
    }

    private string city = "";
    public string City
    {
        get {return city;}
        set {city = value;}

    }
}

Customer cust = new Customer();
cust.Addr1 = "1 Main St.";
cust.City = "Hartford";
int custno = CustomerController.InsertCustomer(cust);

The Addr2 field, which was not initialized is still an empty string when inserted.

However, using the same code but called it through a web service based on Windows Communication Foundation the Addr2 field is null. Is there a way (or setting) where all string fields, even if uninitialized, would return an empty string (unless, of course, a value was set).

A: 

I could not find how to serialize the nulls to empty strings so I changed the Customer class as follows:

public class Customer 
{ 
private string addr1 = ""; 
public string Addr1 
{ 
    get {return addr1;} 
    set {addr1 = value ?? string.Empty; } 
} 

private string addr2 = ""; 
public string Addr2 
{ 
    get {return addr2;} 
    set {addr2 = value ?? string.Empty; } 
} 

private string city = ""; 
public string City 
{ 
    get {return city;} 
    set {city = value ?? string.Empty; } 

} 

This guarantees that all string properties are set to string.Empty and it solves the problem. (If anyone knows of a different solution I would love to hear it.)

Geri Langlois
A: 

Do you have a reference to your dll with the customer class on both the client and the server side?

What I think is happening is that at, when you created the client you have done an add service reference. This has generated a client side class.

You then do a new object on the client side which does not include lines of the type

private string addr1 = ""; 

It then gets set to null on the client side, and deserialized to null on the server side.

If you have control over both the client and the server side you could reference the dll with the object references from both.

Shiraz Bhaiji