-5
    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;

    }

please tell me how to remove the null point exception or how to use the new keyword for the foreach loop in case of the columns. It is a dll library class that is to be used to render an HTML page for displaying any particular table as called by the dictionary.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • 3
    Well clearly whatever is populating the `_TableProperty` class isn't populating the `Columns` property, that's outwith the scope of the code you have posted. "*new keyword for the foreach loop in case of the columns*" - are you trying to build up the `Columns` property? – James Jul 08 '13 at 10:23

2 Answers2

3

Before foreach loop check for null value in tableProperty.Columns.

Jack
  • 253
  • 3
  • 8
  • Please go back to http://stackoverflow.com/questions/17553209 and put in an answer. What you added into the question worked. It just needs some explanation as to what is going on. I'll keep an eye on it and help out if you need it. – Paul Sasik Jul 09 '13 at 16:50
0

Ensure that tableProperty is not null before trying to loop round the collection.

if (tableProperty != null) {
  foreach (var column in tableProperty.Columns)
  {
    columnAdd += _reader.GetOrdinal(column.Value);
  }
}

You may also want to consider using StringBuilder rather than concatenating strings.

Darren
  • 68,902
  • 24
  • 138
  • 144