Asked by:
Problem navigating database records to the next page in web form

Question
-
User53952362 posted
Hello All,
I am new to ASP.NET. I have encountered a problem in navigating the database records that is displayed in the web form , the Next and Previous buttons. I have 4 imageboxes and 2 textboxes under each image box for displaying database records.
I am currently using an Access database with a table "Images" and there are 4 columns in it. "ImageID", "ImageDescription","ImageofPicture","Description".
My Code is below:
OleDbDataAdapter daImage = default(OleDbDataAdapter); DataSet dsImage = null; int MaxRows = 0; int inc = 0; int inc1 = 1; int inc2 = 2; int inc3 = 3; System.Data.OleDb.OleDbConnection cs; System.Data.OleDb.OleDbDataAdapter dta; DataSet ds1; MemoryStream ms; byte[] photo_array;
protected void Page_Load(object sender, EventArgs e) { cs = new System.Data.OleDb.OleDbConnection(); //tell the program where the database is located cs.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source= C:\\Users\\Admin\\Desktop\\Website1\\App_Data\\Database.mdb"; //create new dataset to store database records ds1 = new DataSet();
string sql = "SELECT * From Images ORDER BY ImageDescription ASC";
dta = new System.Data.OleDb.OleDbDataAdapter(sql, cs); cs.Open(); //Fill the records from the database to dataset dta.Fill(ds1, "Images"); NavigateRecords(); //MaxRows equals to the total amount of records from the database MaxRows = ds1.Tables["Images"].Rows.Count; }
private void NavigateRecords() { //get the first record DataRow dtaRow = ds1.Tables["Images"].Rows[inc]; //get column values in the row TextBox3.Text = dtaRow.ItemArray.GetValue(1).ToString(); TextBox7.Text = dtaRow.ItemArray.GetValue(3).ToString(); //get the second record DataRow dtaRow1 = ds1.Tables["Images"].Rows[inc1]; TextBox4.Text = dtaRow1.ItemArray.GetValue(1).ToString(); TextBox2.Text = dtaRow1.ItemArray.GetValue(3).ToString(); //get the third record DataRow dtaRow2 = ds1.Tables["Images"].Rows[inc2]; TextBox5.Text = dtaRow2.ItemArray.GetValue(1).ToString(); TextBox8.Text = dtaRow2.ItemArray.GetValue(3).ToString();
//get the fourth record DataRow dtaRow3 = ds1.Tables["Images"].Rows[inc3]; TextBox6.Text = dtaRow3.ItemArray.GetValue(1).ToString(); TextBox9.Text = dtaRow3.ItemArray.GetValue(3).ToString(); //if 3rd column value is not null if (ds1.Tables[0].Rows[inc][2] != System.DBNull.Value) {
Image2.ImageUrl = "ImageHandler.ashx?ImageDescription=" + TextBox3.Text; } if (ds1.Tables[0].Rows[inc1][2] != System.DBNull.Value) { Image3.ImageUrl = "ImageHandler.ashx?ImageDescription=" + TextBox4.Text; } if (ds1.Tables[0].Rows[inc2][2] != System.DBNull.Value) { Image4.ImageUrl = "ImageHandler.ashx?ImageDescription=" + TextBox5.Text; } if (ds1.Tables[0].Rows[inc3][2] != System.DBNull.Value) { Image5.ImageUrl = "ImageHandler.ashx?ImageDescription=" + TextBox6.Text; } }
protected void nextBtn_Click(object sender, EventArgs e) { if (inc + 4 <= MaxRows - 1) { inc = inc + 4; NavigateRecords(); } if (inc1 + 4 <= MaxRows - 1) { inc1 = inc1 + 4; NavigateRecords(); } else { Image2.ImageUrl = null; TextBox4.Text = null; TextBox2.Text = null; } if (inc2 + 4 <= MaxRows - 1) { inc2 = inc2 + 4; NavigateRecords(); } else { Image3.ImageUrl = null; TextBox5.Text = null; TextBox8.Text = null; }
if (inc3 + 4 <= MaxRows - 1) { inc3 = inc3 + 4; NavigateRecords(); } else { pictureBox4.Image.Dispose(); pictureBox4.Image = null; textBox6.Text = null; textBox9.Text = null; }
Tuesday, December 28, 2010 9:33 AM
All replies
-
User759660354 posted
The problem lies probably in your Page_Load event.
You are always calling them even when there is a postback taking place.
that way you will always get the first page.
You should wrap that into somthing like
if(!this.IsPostBack)
{
NavigateRecords();
}
Tuesday, December 28, 2010 10:16 AM -
User53952362 posted
Hi fdepijper,
Thanks for replying. I have wrapped the NavigateRecords but it can only navigate to the 2nd page of the records at the most. Meaning that i can only view up to 8 records when there are a lot more records in the database with the next button. Which means the next button only work once.
Regards
DreamLiveTuesday, December 28, 2010 8:16 PM