2

I'm trying to get my class to return a html string which I then assign to a control on a aspx page as: this.div1.InnerHtml = class.News();

However, my catch code is always firing, returning my 'NoValue' message.

The code works fine outside of the class.

When I comment out the reader lines htmlStr.Append(reader["Title"].ToString()); I can get the string back to my calling code, is there something else I need to do, to use readers in classes?

namespace confonline {

public class conf
{
    private string connectionStringBMP = WebConfigurationManager.ConnectionStrings["ConfBMP"].ConnectionString;

    public string News()
    {

        SqlConnection conn = new SqlConnection(connectionStringBMP);
        SqlCommand cmd = new SqlCommand("spNews ", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@number",2);

        SqlDataReader reader;

        try
        {
            conn.Open();

            reader = cmd.ExecuteReader();
            StringBuilder htmlStr = new StringBuilder("");

            while (reader.Read())
            {
               htmlStr.Append("<div class='news'>");
                htmlStr.Append("<img align='left' title='" + reader["ThumbnailText"].ToString() + "' alt='" + reader["ThumbnailText"].ToString() + "' src='images/news/thumbnails/" + reader["PublishYear"].ToString() + "/" + reader["Photo"].ToString() + "'>");
                htmlStr.Append("<a href=news.aspx?p=" + reader["ID"] + ">");
                htmlStr.Append(reader["Title"].ToString());
                htmlStr.Append("</a>");
                htmlStr.Append("</div>");
            }

             string htmlString = htmlStr.ToString();

             reader.Close();

             return htmlString;

        }
        catch (Exception err)
        {
            HttpContext.Current.Response.Write(err.Message);
            string noVal = "No Value";
            return noVal;

        }
        finally
        {
            conn.Close();
        }

    }
}

}

1
  • You will need to capture the exact exception thrown. If it works outside the class, maybe you need to check how did you call the method in the first place. Commented Dec 7, 2009 at 23:50

2 Answers 2

3

Look closer at the Exception you get. reader["Title"] can be null.

Sign up to request clarification or add additional context in comments.

2 Comments

In fact, all of your access to the reader could be null (unless your fields are declared not null in the database).
Yep, but Melt is getting Exception at that exact line;). There can be a null in the DB or wrong column name or whatever.
1

if it works when you comment out

 htmlStr.Append(reader["Title"].ToString());

as you say, then there is an exception on that line. Maybe "Title" isn't a valid column? or is sometimes null?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.