【发布时间】:2013-07-08 10:20:49
【问题描述】:
public string getReportHTML(_TableProperty tableProperty, Stream stream)
{
string sql = "select ";
string columnAdd="<table><tr><td>";
foreach (var column in tableProperty.Columns)//nullreferencepointexception
{
sql += column.Key + " as [" + column.Value + "],";
columnAdd += "<th> column.Value </th>";
}
columnAdd += "</table></tr></td>";
sql=sql.TrimEnd(',');
sql += " from" + tableProperty.ReportTable;
sql = sql + " where 1=1 " + (string.IsNullOrEmpty(tableProperty.ReportCondition) ? "" : "and " + tableProperty.ReportCondition);
SqlConnection _con = new SqlConnection(@"server=mausam-pc\sqlexpress;uid=***;pwd=***;initial catalog=HRMSN");
SqlCommand _cmd = new SqlCommand(sql, _con);
_con.Open();
SqlDataReader _reader = _cmd.ExecuteReader();
while (_reader.Read())
{
foreach (var column in tableProperty.Columns)
{
columnAdd += _reader.GetOrdinal(column.Value);
}
}
columnAdd += "</table></tr></td>";
string htmlread = "<html><title>General</title><body> columnAdd </body></html>";
if (_reader != null)
{
_reader.Close();
}
_con.Close();
return htmlread;
}
请告诉我如何删除空点异常或如何在列的情况下为 foreach 循环使用 new 关键字。它是一个 dll 库类,用于呈现 HTML 页面,以显示字典调用的任何特定表。
【问题讨论】:
-
很明显,填充
_TableProperty类的任何内容都没有填充Columns属性,这超出了您发布的代码的范围。 “foreach 循环的新关键字在列的情况下” - 您是否尝试构建Columns属性?