为什么此事件处理程序代码会导致对象引用未设置到对象的实例。错误(仅出现在实时服务器上,在开发版上可以)?
这是按钮单击事件的代码;
Protected Sub CompetenciesButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CompetenciesButton.Click
Dim con As String = WebConfigurationManager.ConnectionStrings("foo").ToString()
Dim selectedrow As String = Competencies.SelectedValue.ToString
Dim s As String = "SELECT * from foo WHERE (" & selectedrow & " =1)"
Dim DataAdapter As New SqlDataAdapter(s, con)
Dim Result As New DataTable
If Not IsNothing(DataAdapter) Then
DataAdapter.Fill(Result)
DataAdapter.Dispose()
End If
GV.DataSource = Result
GV.DataBind()
GV2.DataSource = Result
GV2.DataBind()
End Sub
这是堆栈:
[NullReferenceException: Object reference not set to an instance of an object.]
administration_Search.CompetenciesButton_Click(Object sender, EventArgs e) +28
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746
在 VS2008 中,一切都可以离线运行,但在发布后出现此错误。
谢谢。
Here is the code for the button click event;
Protected Sub CompetenciesButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CompetenciesButton.Click
Dim con As String = WebConfigurationManager.ConnectionStrings("foo").ToString()
Dim selectedrow As String = Competencies.SelectedValue.ToString
Dim s As String = "SELECT * from foo WHERE (" & selectedrow & " =1)"
Dim DataAdapter As New SqlDataAdapter(s, con)
Dim Result As New DataTable
If Not IsNothing(DataAdapter) Then
DataAdapter.Fill(Result)
DataAdapter.Dispose()
End If
GV.DataSource = Result
GV.DataBind()
GV2.DataSource = Result
GV2.DataBind()
End Sub
Here is the stack:
[NullReferenceException: Object reference not set to an instance of an object.]
administration_Search.CompetenciesButton_Click(Object sender, EventArgs e) +28
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746
Everything works fine offline in VS2008 but I get this error after publishing.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 WebConfiguration 在开发版和上线版上是否相同?
如果实时服务器上缺少 foo 连接字符串,则
con
对象将设置为 null。如果您仍然无法捕获它,请尝试包含一个 try/catch 块,在 try 之外设置一个字符串变量
progress
,更新逻辑中的每一行,并记录错误以及进度
的当前值。这应该有助于缩小问题发生的范围。Is your WebConfiguration the same on both dev and live?
If you are missing the foo connection string on the live server, then the
con
object would be set to null.And if you are still having trouble catching it, then try including a try/catch block, setting a string variable
progress
outside of try, updating it every line within the logic, and logging the error along with the current value ofprogress
. This should help to narrow down where it is happening.