0

I'll preface this by giving a bit of an overview of what I'm trying for here.

I'm trying to create a quasi-blog. Something that I can "post" journal type entries directly to a page in my website but without all the luggage that comes with the readily available blog engines and without viewer reply capability.

Essentially it's just going to be an interface where I can enter a post which gets stored on a database and a dynamically loading page that shows the most recent post with the option to navigate to prior posts (and back forward again).

Here's the thing... I can get my dynamic page to show the 'current' or most recent post, I can get it to navigate backwards through posts... but when I try to navigate forward again I get System.IndexOutOfRangeException: There is no row at position 0.

the only difference between the two functions of the page is that the "Previous Post" button is geared to utilize

currentMessageID -= 1;

and the "Next Post" button instead does += 1. currentMessageID is, of course, the MessageID variable that determines which message number I'm attempting to get from the database in my SELECT statement.

Pertinent pieces of the code are below... any help is appreciated.

public partial class blogged : System.Web.UI.Page
{
    bool firstLoader = true;
    int testMessageID = 0;
    int lastMessageID = 0;
    int firstMessageID = 0;
    int currentMessageID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        getFirstMessageID(true);
        getLastMessageID(true);
        if (firstLoader == true)
        {
            currentMessageID = lastMessageID;
        }
        loadMessage();
        firstLoader = false;
    }

    private void getLastMessageID(bool first) // Obtain the current highest message ID from the list
    {
        string adaptString = "";
        if (first == true)
        {
            adaptString = @"SELECT TOP (1) mID, isDeleted FROM blMessages order by mID Desc";
        }
        else
        {
            adaptString = @"SELECT mID, isDeleted FROM blMessages WHERE mID ='" + (testMessageID - 1) + "'";
        }
        blConn connector = new blConn();
        string connStr = @connector.cString;
        SqlConnection latestConn = new SqlConnection(@connStr);
        SqlDataAdapter latestAdapt = new SqlDataAdapter(adaptString, latestConn);
        SqlCommandBuilder latestBuilder = new SqlCommandBuilder(latestAdapt);
        DataSet latestSet = new DataSet();
        latestAdapt.Fill(latestSet, "lastID");
        DataRow latestRow = latestSet.Tables["lastID"].Rows[0];
        if (latestRow["isDeleted"].ToString() == "True")
        {
            testMessageID = Convert.ToInt16(latestRow["mID"].ToString());
            latestConn.Close();
            getLastMessageID(false);
        }
        else
        {
            lastMessageID = Convert.ToInt16(latestRow["mID"].ToString());
            latestConn.Close();
        }
    }
    private void getFirstMessageID(bool first) // Obtain the current lowest message ID from the list
    {
        string adaptString = "";
        if (first == true)
        {
            adaptString = @"SELECT TOP (1) mID, isDeleted FROM blMessages";
        }
        else
        {
            adaptString = @"SELECT mID, isDeleted FROM blMessages WHERE mID ='" + (testMessageID + 1) + "'";
        }
        blConn connector = new blConn();
        string connStr = @connector.cString;
        SqlConnection firstConn = new SqlConnection(@connStr);
        SqlDataAdapter firstAdapt = new SqlDataAdapter(adaptString, firstConn);
        SqlCommandBuilder firstBuilder = new SqlCommandBuilder(firstAdapt);
        DataSet firstSet = new DataSet();
        firstAdapt.Fill(firstSet, "firstID");
        DataRow firstRow = firstSet.Tables["firstID"].Rows[0];
        if (firstRow["isDeleted"].ToString() == "True")
        {
            testMessageID = Convert.ToInt16(firstRow["mID"].ToString());
            firstConn.Close();
            getFirstMessageID(false);
        }
        else
        {
            lastMessageID = Convert.ToInt16(firstRow["mID"].ToString());
            firstConn.Close();
        }
    }
    private void loadMessage() // Load the currently selected message to the screen
    {
        blConn connector = new blConn();
        string connStr = @connector.cString;
        SqlConnection loadMsgConn = new SqlConnection(@connStr);
        SqlDataAdapter loadMsgAdapt = new SqlDataAdapter("SELECT mID, mDate, mSubject, mBody, isDeleted FROM blMessages WHERE mID='" + currentMessageID + "'", loadMsgConn);
        SqlCommandBuilder loadMsgBuilder = new SqlCommandBuilder(loadMsgAdapt);
        DataSet loadMsgSet = new DataSet();
        loadMsgAdapt.Fill(loadMsgSet, "currentMsg");
        loadMsgConn.Close();
        DataRow loadMsgRow = loadMsgSet.Tables["currentMsg"].Rows[0];
        if (loadMsgRow["isDeleted"].ToString() == "True")
        {
            if ((currentMessageID + 1) > lastMessageID)
            {
                currentMessageID -= 1;
                loadMessage();
            }
            else
            {
                currentMessageID += 1;
                loadMessage();
            }
        }
        else
        {
            msgSubject.Text = (loadMsgRow["mSubject"].ToString());
            msgDateTime.Text = (loadMsgRow["mDate"].ToString());
            msgBody.Text = (loadMsgRow["mBody"].ToString());
            currentMessageID = Convert.ToInt16(loadMsgRow["mID"].ToString());
            if (currentMessageID < lastMessageID)
            {
                fwdButton.Visible = true;
            }
            else
            {
                fwdButton.Visible = false;
            }
            if (currentMessageID > firstMessageID)
            {
                backButton.Visible = true;
            }
            else
            {
                backButton.Visible = false;
            }
        }
        loadMsgSet.Clear();
    }
    protected void fwdButton_Click(object sender, EventArgs e)
    {
        currentMessageID += 1;
        loadMessage();
    }
    protected void backButton_Click(object sender, EventArgs e)
    {
        currentMessageID -= 1;
        loadMessage();
    }
}
2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Lusiphur
0

Hi lusiphur

The error message is straight forward, there is no row when you navigate forward

i suggest you to set a break point and debug

Edited by reach_yousuf: n/a

0

Yousuf,

Thanks for the speedy reply but the situation is that there is no reason for there not to be a row in this instance.

My test scenario was as follows:

1. Display most recent message (mID = 5)
2. Navigate to prior message (mID = 4) using currentMessageID -= 1
3. Navigate to next message (mID = 5) using currentMessageID += 1

Logically speaking as the only difference between the functions is + vs - it should have displayed the most recent, 2nd most recent, then most recent again.

Instead, it shows the most recent, 2nd most recent, then gives the indexOutOfRageException error.

I can't for the life of me see the logic error in my code to solve it either...

0

After running extensive debugging I've found that it seems to be working properly for prior message (stepping back by 1 for the value of mID) however for some reason it seems to be reverting the currentMessageID to 5 prior to applying the + 1 for the next message portion...

I'm baffled as to why it would do that when there are at least 2 places in the code where it should be updating the currentMessageID to the messageID that is currently displayed on the page.

0

OK... so... after ALL of that...

I just made the following changes to the code and it works flawlessly now.

public partial class blogged : System.Web.UI.Page
{
    static int testMessageID = 0;
    static int lastMessageID = 0;
    static int firstMessageID = 0;
    static int currentMessageID = 0;
    static int prevMessageID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            firstLoad();
        }
    }

    private void firstLoad()
    {
        getFirstMessageID(true);
        getLastMessageID(true);
        currentMessageID = lastMessageID;
        loadMessage();
    }

Thanks for the attempted help though :D

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.