View records one by one from ds on click of buttons

The following code-snippet tells how one can get a record on button click using DataSet:

On default.aspx page


<asp:label id="Label2" runat="server">Product ID</asp:label>
<asp:label id="Label1" runat="server">Product Name</asp:label>
<asp:textbox id="txtProductName" runat="server" OnDataBinding="txtDataBind"></asp:textbox>
<asp:textbox id="txtProductid" runat="server">
<asp:button id="btnPrevious" runat="server" Text="Previous" OnClick ="PrevBtn"></asp:button>
<asp:button id="btnNext" runat="server" Text="Next" OnClick ="NextBtn"></asp:button>

There is one label, two textbox for showing records and two button previous (for previous record) and next (for next record).
Assume that there is five record in dataset (index is 0,1,2,3,4)..ok..

first time CurrentPos is 0 and call to databind function...
databind function show the first record in label and textbox according to current position which is 0.. (see below Page_load event )

when u click on previous button then
currentPos retrieve from viewstate into local variable...
if current position is greater then 0 that means current record is not a first record..
then less 1 in current position if position is greate then 0 otherwise not less 1 from currentpos
store new currentpos in view state..
the call databind function for show record according current position..
(see below PrevBtn_Click event)

when u click on next button then..
currentPos retrieve from viewstate into local variable...
increment of one into currentPos..
if new currentPos is greater then the no. of records in dataset that means currentPos is not exist in dataset. so less 1 from currentPos..
now store new currentpos in view state..
and call databind function...(see below NextBtn_Click event)


DataSet ds;
private void Page_Load(object sender, System.EventArgs e)
{
OleDbConnection myConnection = new OleDbConnection ( "Provider=SQLOLEDB.1;Data Source = localhost ; database = northwind ; User Id=sa;Password=;");
OleDbDataAdapter myda= new OleDbDataAdapter ("Select * from Orders", myConnection);
myda.Fill (ds,"table");
if (!Page.IsPostBack )
{
ViewState["CurrentPos"] = 0 ;
this.DataBind() ;
}
}



protected void PrevBtn_Click(object sender, System.EventArgs e)
{
try
{
int CurrentPos = (int)ViewState["CurrentPos"] ;
if (CurrentPos > 0 )
{
CurrentPos = CurrentPos - 1 ;
}
ViewState["CurrentPos"] = CurrentPos ;
this.DataBind() ;
}
catch (Exception ex)
{
Response.Write(ex.Message) ;
}
}

protected void NextBtn_Click(object sender, System.EventArgs e)
{
try
{
int CurrentPos = (int)ViewState["CurrentPos"] ;
CurrentPos = CurrentPos + 1 ;
if( CurrentPos > ds.Tables[0].Rows.Count)
{
CurrentPos = CurrentPos - 1 ;
}
ViewState["CurrentPos"] = CurrentPos ;
this.DataBind() ;
}
catch (Exception ex)
{
Response.Write(ex.Message) ;
}
}

protected void DataBind(Object sender , System.EventArgs e )
{
try
{
// int CurrentPos = (int) ViewState["CurrentPos"];
ViewState["CurrentPos"] = CurrentPos ;
txtProductid.Text = ds.Tables[0].Rows[CurrentPos]["productid"].ToString();
txtProductName.Text = ds.Tables[0].Rows[CurrentPos]["productname"].ToString ();
}
catch (Exception ex)
{
Response.Write(ex.Message) ;
}
}


Comments

Author: Gaurav Aroraa26 Oct 2008 Member Level: Gold   Points : 1

Hi varun!

Its a nice snippet. But please try to descrive the working of your code including step instructions. So, one can easily learn things from code.

Thanks & regards,
Gaurav Arora



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: