Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp.Net Class - problem returning html string value
    text
    copied!<p>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();</p> <p>However, my catch code is always firing, returning my 'NoValue' message.</p> <p>The code works fine outside of the class. </p> <p>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?</p> <p>namespace confonline {</p> <pre><code>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("&lt;div class='news'&gt;"); htmlStr.Append("&lt;img align='left' title='" + reader["ThumbnailText"].ToString() + "' alt='" + reader["ThumbnailText"].ToString() + "' src='images/news/thumbnails/" + reader["PublishYear"].ToString() + "/" + reader["Photo"].ToString() + "'&gt;"); htmlStr.Append("&lt;a href=news.aspx?p=" + reader["ID"] + "&gt;"); htmlStr.Append(reader["Title"].ToString()); htmlStr.Append("&lt;/a&gt;"); htmlStr.Append("&lt;/div&gt;"); } 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(); } } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload