I was just wondering about the correct way to get the reader back from the class?
My code below works, but I'm not sure if this is correct.
Besides. I cannot close the connection in the class method and still access it from my ascx page,
what is OK?
// In my class, I have the following method for returning a record / read - this is the only record in this case.
public SqlDataReader GetPost()
{
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("con_spPost", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@blogid", blogid);
try
{
conn.Open();
return cmd.ExecuteReader();
}
finally
{
}
}
// Then I call the GetPost method on my ascx page as follows:
protected void Page_Load(object sender, EventArgs e)
{
MyClass DB = new MyClass();
DB.PostID = Int32.Parse(Request.QueryString["p"]);
SqlDataReader reader = DB.GetPost();
reader.Read();
this.viewpost.InnerHtml = "<span id='post1_CreatedDate'>" + reader["CreatedDate"].ToString() + "</span><br>";
this.viewpost.InnerHtml += "<span class='blogheads'>" + reader["BlogTitle"].ToString() + "</span><p><p>";
this.viewpost.InnerHtml += reader["BlogText"].ToString();
reader.Close();
}
I would appreciate any comments on my code or advice, thanks.
melt
source
share