Thursday, March 8, 2012

Data Binding-DataAdapter

Hi i'm a new to ASP.NET and for some reason when i click the Next button in the code below, the pageIndex does not change. Please assist, Basically what i'm trying to do is to use DataAdapter.fill but passing in the start index and the number of records to pull from the dataset table.

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.OleDb;

publicpartialclassHome : System.Web.UI.Page

{

//Connection

OleDbConnection dbConn;

//discount that can be change by user using a gui interface

//CurrentPage

int pageIndex = 0;double discount = 0.15 ;

protectedvoid Page_Load(object sender,EventArgs e)

{

// homeGridView.Visible = true;

BindList();

}

protectedstring getSpecial(string price,object sale)

{

String special ="";if (sale.ToString().CompareTo("True") == 0)

{

special =String.Format("{0:C}",double.Parse(price) * (1-discount));

}

return special;

}

protectedvoid BindList()

{

//Creating an object for the 'PagedDataSource' for holding the data.

//PagedDataSource objPage = new PagedDataSource();

try

{

//open connection

openConnection();

//sql command

string columns ="*";

string SqlCommand ="Select " + columns +" from Books";

//create adapters and DataSet

OleDbDataAdapter myAdapter =newOleDbDataAdapter(SqlCommand, dbConn);DataSet ds =newDataSet("bSet");

//create table

DataTable dt =newDataTable("Books");myAdapter.Fill(ds, pageIndex, 9,"Books");

Response.Write("Page Index: "+pageIndex);

//create table data view

DataView dv =newDataView(ds.Tables["bTable"]);

booksDataList.DataSource = ds;

booksDataList.DataBind();

myAdapter.Dispose();

dbConn.Close();

}

catch (Exception ex)

{

Response.Write("Exception thrown in BindList()");

dbConn.Close();

throw ex;

}

}

publicvoid openConnection()

{

string provider="Microsoft.Jet.OLEDB.4.0";

string dataSource ="C:/Documents and Settings/Owner/My Documents/Visual Studio 2005/WebSites/E-BookOnline/App_Data/BooksDB.mdb";

dbConn =newOleDbConnection("Provider =" + provider +";" +"Data Source =" + dataSource);

dbConn.Open();

}

protectedvoid nextClick(object sender,EventArgs e)

{

pageIndex=pageIndex+1;

Response.Write("In nextClick"+pageIndex);

BindList();

}

protectedvoid prevClick(object sender,EventArgs e)

{

if (pageIndex > 0)

{

pageIndex=pageIndex-1;

BindList();

}

}

}

It is not advisable to use DataAdpater to page records... instead of this you need to use queries for that but anyway you can found this article useful.

http://msdn2.microsoft.com/en-us/library/tx1c9c2f(VS.80).aspx

Satya

No comments:

Post a Comment