Getting “This method or property cannot be called on Null values” error

时光总嘲笑我的痴心妄想 提交于 2019-12-22 09:00:14

问题


UPDATE 1:

The exception is being thrown on this line:

client_group_details.Add(new ClientGroupDetails(

ORIGINAL QUESTION:

I have the following code which I have stripped down from 30 columns of data from the database to just 2 columns from the database. I get an error whenever any of the columns return a NULL value:

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 on Null values.

I'm not sure what to do here to deal with NULL values as the code above is a stripped down version.

Anyone know how to solve this issue?


回答1:


This is because reader.GetString should not be called on DBNull values. Try changing your code as follows:

client_group_details.Add(new ClientGroupDetails(
    reader.IsDbNull(Col2Index) ? null : reader.GetString(Col2Index),
    reader.IsDbNull(Col3Index) ? null : reader.GetString(Col3Index)));



回答2:


You need to use IsDbNull to check if the column is null before calling GetString, something like:

string s1, s2;

if (reader.IsDbNull(Col1Index) == false)
{
   s1 = reader.GetString(Col1Index);
}

if (reader.IsDbNull(Col2Index) == false)
{
   s2 = reader.GetString(Col2Index);
}

client_group_details.Add(new ClientGroupDetails(s1, s2));



回答3:


There are a couple ways you can do this, but I think the best approach for your code is to add a simple function call to your SQL text - namely, the IsNull function.

Here's a link to the manual page for this: IsNull MSDN reference

Basically, you'll change your SQL text to look similar to this:

"select IsNull(col2, ''), IsNull(col3, '') where col1 = @phrase"

And now if the column in the DB is null, it will instead return a blank string.

You can also set default values on your columns, or you can check for System.DBNull.Value on your code side.

Good luck!



来源:https://stackoverflow.com/questions/9397960/getting-this-method-or-property-cannot-be-called-on-null-values-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!