【发布时间】:2013-10-03 19:48:38
【问题描述】:
我是 .Net 的新手,不知道如何解决我的大部分问题。这是我的问题。我创建了一个表单来执行脚本列表。数据将来自数据库。我的问题是,当我调试时,我在此代码块上收到 NullReferenceException 错误
private void Load_RefreshForm_ScriptMgmt()
{
try
{
SqlCommand ObjCmd = new SqlCommand();
SqlConnection ObjConn = new SqlConnection();
this.Ds_Settings1.Tables["Settings_RefreshForm_ScriptMgmt_SelectALL"].Clear();
ObjConn.ConnectionString = this.MainConnection;
ObjCmd.CommandType = CommandType.StoredProcedure;
ObjCmd = new SqlCommand(this.da_LoadScripts.SelectCommand.CommandText.ToString(), ObjConn);
this.da_LoadScripts.SelectCommand = ObjCmd;
this.da_LoadScripts.Fill(this.Ds_Settings1);
}
catch (Exception exception)
{
ProjectData.SetProjectError(exception);
this.DisplayOnly_ErrorHandler("ERROR LOADING REFRESH SCRIPTS ", exception.Message, MsgBoxStyle.Critical);
ProjectData.ClearProjectError();
}
}
【问题讨论】:
-
this.da_LoadScripts.SelectCommand在用ObjCmd覆盖之前首先设置在哪里?如果在您第一次创建和设置ObjCmd时它为空,那么您将通过尝试访问CommandText属性得到一个空异常。您应该能够通过调试您的应用来确定错误发生的确切位置。 -
欢迎来到 Stack Overflow!几乎所有
NullReferenceException的情况都是一样的。请参阅“What is a NullReferenceException in .NET?”获取一些提示。
标签: c# winforms nullreferenceexception