在将记录添加到sharepoint 2010中的列表时检查条件时发生“空引用异常”

时间:2012-07-25 07:32:04

标签: sharepoint sharepoint-2010 sharepoint-2007

我正在尝试将记录添加到sharepoint 2010的“链接”列表中。 我想对添加项目进行验证。我是这样写的。 enter image description here

我正在尝试停止输入包含google关键字的任何链接。以下代码有什么问题吗?我正在为我的练习做准备。 我认为在图像代码中并没有清晰显示。我在这里写下来了。

if (properties.AfterProperties["vti_url"].ToString().Contains("google"))
           {
               properties.ErrorMessage = "You should not enter the google";
                 properties.Cancel = true;
           }

添加properties.AfterProperties["URL"].ToString().Contains("google")后,它运行正常。但我的错误页面看起来不太好。这是屏幕截图。该怎么办这个问题。 enter image description here

2 个答案:

答案 0 :(得分:1)

我会猜测properties.AfterProperties["vti_url"]null,因为没有名为“vti_url”的字段...你试过了吗?

properties.AfterProperties["URL"]

答案 1 :(得分:1)

enter code here Hinek提到的评论很可能是正确的

 /// <summary>
    /// Checks the object to see if it's null, if it isn't it will return the string value of the object.
    /// </summary>
    /// <param name="value">(object) The object you want to check.</param>
    /// <returns>(string) The string value of the object.</returns>
    public static string CheckForNullValue(object value)
    {
        string tmpValue = string.Empty;
        try
        {
            if (value != null)
            {
                if (!string.IsNullOrEmpty(value.ToString()))
                {
                    tmpValue = value.ToString();
                }
            }
        }
        catch (Exception exc)
        {
            Error.Log("Failed to check for null value Value passed in" + value, exc, Error.ErrorType_Error);
            throw exc;
        }
        return tmpValue;
    }

string url = CheckForNullValue(properties.AfterProperties["vti_url"])


if (url.Contains("Google"))   
       {   
           properties.ErrorMessage = "You should not enter the google";   
             properties.Cancel = true;   
       }   

希望这有帮助