-1

I'm struggling with some System.Data.SqlClient.SqlDataReader problems.

Actually, I made a DatabaseHandler for execute queries more convenient, with IDisposable interface for using statements.

When I use the handler for reading single article, query successfully executed, however, the handler disposed before I use data.

Then, the result is NullReferenceException. Here's the article reading code, and Dispose().

// Article Reading Code
using (var ArticleHandler = new DatabaseHandler())
{
    var ArticleReader = ArticleHandler.ExecuteCommand("ARTICLE_INQUIRY",
        DatabaseHandler.QueryType.ExecuteReader,
        new DatabaseParameter[]
        {
            new DatabaseParameter("seq", Sequence)
        }) as System.Data.SqlClient.SqlDataReader; //Query Successful

    if (!ArticleReader.HasRows)
    {
        Response.Redirect("/articles/list.aspx");
    }
    else
    {
        ArticleReader.Read(); //Data Read Successful

        CurrentArticle.title = (string)ArticleReader["title"]; //NullReferenceException ?
        CurrentArticle.category = (string)ArticleReader["category"];
        CurrentArticle.datetime = (string)ArticleReader["written_datetime"];
        CurrentArticle.content = (string)ArticleReader["content"];
    }
}


/* Database Handler Dispose() */
public void Dispose()
{
    CurrentConnection = null;
}

P.S. I checked column names, and they are match with database information.

Kangjun Heo
  • 773
  • 1
  • 5
  • 15

1 Answers1

0

Check inside your database handler if you are closing the underlying sqlconnection before accessing the reader to access data in the next line. If the sqlconnection is getting closed, the reader will get closes automatically. Paste the handler Execute command code so we can have look at it

Swetha
  • 442
  • 4
  • 7