好的,所以关于这类错误的帖子很多,但没有什么特别的东西让我能够缩小这个问题。
SqlConnection con = null;
List<State> list = new List<State>();
try
{
con = Common.openConnection(Common.connectionString);
string sql = "Select * from State order by state";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
State m = new State(int.Parse(reader.GetValue(0).ToString()),reader.GetString(1), reader.GetString(2), int.Parse(reader.GetValue(3).ToString()));
list.Add(m);
}
reader.Close();
}
catch (Exception ex)
{
throw new Exception("Error:[DALStateSQL] in selectAll : " + Common.NEWLINE + ex.ToString());
}
finally
{
Common.closeConnection(con);
}
return list;
这是导致问题的方法。我只是试图将它绑定到对象数据源并将其显示在列表框中。我得到了这个堆栈跟踪:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance) +138
System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1953
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +97
System.Web.UI.WebControls.ListControl.PerformSelect() +34
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +29
System.Web.UI.Control.PreRenderRecursiveInternal() +103
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496
这告诉我一切,因为跟踪中没有任何东西是我自己的代码。
我有另一个相同的方法,它有效,它只是一个行为不端的方法的复制粘贴。州级本身很简单:
private int stateId;
private string stateCode;
private string name;
private int countryCode;
public int StateId
{
get { return stateId; }
set { stateId = value; }
}
public string StateCode
{
get { return stateCode; }
set { stateCode = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public int CountryCode
{
get { return countryCode; }
set { countryCode = value; }
}
public State()
{
this.stateId = 0;
this.stateCode = "";
this.name = "";
this.countryCode = 0;
}
public State(int stateId, string stateCode, string name, int countryCode)
{
this.stateId = stateId;
this.stateCode = stateCode;
this.name = name;
this.countryCode = countryCode;
}
编辑:我忘了在其他地方完全提到selectAll()方法调用,但在尝试将其绑定到对象数据源时会中断。 .....?