好的,所以有很多关于此类错误的帖子,但没有足够具体的内容让我缩小问题的范围。
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() 方法在其他地方完美调用,但在尝试将其绑定到对象数据源时中断。……?