UPDATE 1:
An exception is thrown on this line:
client_group_details.Add(new ClientGroupDetails(
ORIGINAL QUESTION:
I have the following code, which I split from 30 columns of data from the database into only two columns from the database. I get an error when any of the columns returns NULL:
public class ClientGroupDetails { public String Col2; public String Col3; public ClientGroupDetails(String m_Col2, String m_Col3) { Col2 = m_Col2; Col3 = m_Col3; } public ClientGroupDetails() { } } [WebMethod()] public List<ClientGroupDetails> GetClientGroupDetails(string phrase) { var client_group_details = new List<ClientGroupDetails>(); using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"])) { using (command = new SqlCommand(@"select col2, col3 where col1 = @phrase", connection)) { command.Parameters.Add("@phrase", SqlDbType.VarChar, 255).Value = phrase; connection.Open(); using (reader = command.ExecuteReader()) { int Col2Index = reader.GetOrdinal("col2"); int Col3Index = reader.GetOrdinal("col3"); while (reader.Read()) { client_group_details.Add(new ClientGroupDetails( reader.GetString(Col2Index), reader.GetString(Col3Index))); } } } } return client_group_details; }
The error I am getting is:
Data is null. This method or property cannot be called with Null values.
I'm not sure what to do here to deal with NULL values, since the code above is a stripped-down version.
Does anyone know how to solve this problem?
source share