MVC3 VB.NET应用程序。我在函数中有以下代码部分。如果handout1不是什么,这个代码应该只会触发。但由于某种原因,当handout1在数据库中没有任何内容时,它会进入then块...我已经尝试了一百种不同的检查方式,但它仍然试图运行该行代码,即使没有任何内容可以分配给它....
If Not _class1.Contains("---") Then
_body = _body.Replace("[[Class1]]", _class1 + " : " + _day1Class.course_title)
Dim _coursesREF As cours = db.courses.Where(Function(f) f.course_ref = class1).First
If Not String.IsNullOrWhiteSpace(_coursesREF.handoutFile1) OrElse Not IsDBNull(_coursesREF.handoutFile1) Then
_class1Handout = New Net.Mail.Attachment((Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\") + _coursesREF.handoutFile1)
End If
Else
_body = _body.Replace("[[NL]][[Class1]]", String.Empty)
End If
任何人都能看到我明显忽视的东西???
答案 0 :(得分:3)
我认为你的行
If Not String.IsNullOrWhiteSpace(_coursesREF.handoutFile1) OrElse Not IsDBNull(_coursesREF.handoutFile1) Then
需要
If Not String.IsNullOrWhiteSpace(_coursesREF.handoutFile1) AndAlso Not IsDBNull(_coursesREF.handoutFile1) Then
DBNulls和Nothing不等同。因此,OrElse正在对Nothing为DBNull进行评估,这不是一个真实的陈述。一旦应用了NOT,您就会得到真正的评估。
答案 1 :(得分:1)
尝试设置一个断点并查看Handout等于什么
If Not _class1.Contains("---") Then
''# Put a breakpoint on the next line
Dim breakVariable = handout1
''# when the breakpoint fires, inspect the value of `handout1`
_body = _body.Replace("[[Class1]]", _class1 + " : " + _day1Class.course_title)
Dim _coursesREF As cours = db.courses.Where(Function(f) f.course_ref = class1).First
If Not String.IsNullOrWhiteSpace(_coursesREF.handoutFile1) OrElse Not IsDBNull(_coursesREF.handoutFile1) Then
_class1Handout = New Net.Mail.Attachment((Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\") + _coursesREF.handoutFile1)
End If
Else
_body = _body.Replace("[[NL]][[Class1]]", String.Empty)
End If
另外,经过进一步检查,你可以修改
If Not String.IsNullOrWhiteSpace(_coursesREF.handoutFile1) OrElse
Not IsDBNull(_coursesREF.handoutFile1) Then
并将其更改为
If Not String.IsNullOrWhiteSpace(_coursesREF.handoutFile1) AndAlso
Not(_coursesREF.handoutFile1 Is Nothing) Then