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 the code:
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.
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 the code:
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.
Code:
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();
}
}