简体   繁体   中英

How to allow NULL values from SQL database to be presented as empty string in WPF DataGrid control (using WCF service)?

I'm practicing using Northwind database and I created a service that allows me to browse Northwind employees from Employees table. The code for interface is:

[ServiceContract]
public interface IEmployeesService
{
    [OperationContract]
    List<EmployeesCon> FetchEmployees();

}

[DataContract]
public class EmployeesCon
{
    [DataMember]
    public int EmployeeID { get; set; }
    [DataMember]
    public string LastName { get; set; }
    [DataMember]
    public string FirstName { get; set; }
    [DataMember]
    public string Title { get; set; }
    [DataMember]
    public string Address { get; set; }
    [DataMember]
    public string City { get; set; }
    [DataMember]
    public string PostalCode { get; set; }
    [DataMember]
    public string HomePhone { get; set; }
}

I haven't included all the properties from the database, just some of them.

The code in the service class is:

public class EmployeesService : IEmployeesService
{
    private string cnnNorthwind = Properties.Settings.Default.NORTHWNDConnectionString;

    public List<EmployeesCon> FetchEmployees()
    {
        SqlConnection con = new SqlConnection(cnnNorthwind);

        SqlCommand command = new SqlCommand("SELECT EmployeeID, LastName, FirstName, Title, Address, City, PostalCode, HomePhone FROM Employees", con);

        try
        {
            con.Open();
            SqlDataReader dr = command.ExecuteReader();
            List<EmployeesCon> employeesList = new List<EmployeesCon>();

            while (dr.Read())
            {
                EmployeesCon z1 = new EmployeesCon();
                z1.EmployeeID = dr.GetInt32(0);
                z1.LastName = dr.GetString(1);
                z1.FirstName = dr.GetString(2);
                z1.Title = dr.GetString(3);
                z1.Address = dr.GetString(4);
                z1.City = dr.GetString(5);
                z1.PostalCode = dr.GetString(6);
                z1.HomePhone = dr.GetString(7);

                employeesList.Add(z1);
            }
            return employeesList;
        }

        catch (Exception)
        {
            return null;
        }

        finally
        {
            con.Close();
        }
    }
}

And after all of this I created WPF project as a service client with a datagrid to show me employees' information.

The problem is, some of the fields in SQL Employees table are null (and they are allowed to be null). When I start the service WCF test client it gives me null value (from the exception).

Can anyone assist me with adjusting the code so the null values from database fields can be included and represented as empty strings so that the code can work?

Thank you in advance.

在您的分配中,您可以使用空合并运算符(??) 来提供默认值(如果值为空)。

z1.Title = dr.GetString(3) ?? string.Empty;

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