C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ADO.NET »

View records one by one from ds on click of buttons


Posted Date: 25 Oct 2008    Resource Type: Code Snippets    Category: ADO.NET
Author: Varun BansalMember Level: Gold    
Rating: 1 out of 5Points: 10



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) ;
}
}



Responses

Author: Gaurav Arora    26 Oct 2008Member Level: Diamond   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


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Bind one record on web page at a time using dataset  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Shuffling Data Values : Make it simple with using newid()
Previous Resource: Get Multiple records using DataTable.Select.
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use