Resources » Code Snippets » Visual Studio

Code for next,previous,first,last records...


Posted Date: 17-Jun-2008  Last Updated:   Category: Visual Studio    
Author: Member Level: Silver    Points: 10



This code is for to get the first,last,previous,next records.....
==================================================================
If we want to see the next,previous,first,last means we will use code like ,

int n=0;//global declaration
datatable dt;
form_load()
{
con...............
................//connection whatever you want...
sqldataadapter da=new sqldataadapter("select * from ",con);
dataset ds=new dataset();
da.fill(ds,"ali");//ali is ur table name or whatever your table
dt=ds.tables["ali"];
display();
}

first_click() //first record button(<<)
{
n=0;
display();
}
previous_click() //previous record button(<)
{
n=n-1;
if(n==-1)
{
n=0;
messagebox.show("first record);
}
else
{
display();
}
}
next_click() //next record button(>)
{
n=n+1
if(n>dt.rows.count-1)
}
else
{
display();
}
}
last_click() //last record button(>>)
{
n=dt.rows.count-1;
display();
}

display() //display function
{
datarow dr=dt.rows[n];
textbox1.text=dr[0].tostring();
textbox2.text=dr[1].tostring();
}


Happy Coding,
BY,
R.A.Gladwin


Did you like this resource? Share it with your friends and show your love!

Responses to "Code for next,previous,first,last records..."
Author: javier    09 Dec 2008Member Level: Bronze   Points : 2
the code has a few errors, like "int n=o;" on the first line, which is an easy fix, just replace the letter "o" with the number "0".

But in the last few lines it refers to "dr" textbox2.text=dr[1].tostring();

dr is not declared anywhere within the code.

I'd like to see this fix as I'm looking forward to understand how the whole thing works and I haven't been able to find a lot of information about this specific topic.



Author: antogladwin    10 Dec 2008Member Level: Silver   Points : 1
hi javier,

i updated the code..thank you for your valuable comments..


by,
R.A.Gladwin



Author: antogladwin    10 Dec 2008Member Level: Silver   Points : 1
hi javier,

if you can't understand hereafter also means...im ready to explain, friend.thank you..

//FYI(For your information)

* display() is a function..

* first,last....are buttons

by,
R.A.Gladwin



Author: Sachin    02 Jul 2009Member Level: Gold   Points : 0
Its very use full article..Thanks a lot


Author: Anilkumar    24 Aug 2009Member Level: Bronze   Points : 2
When am using this code display error like this
Class, struct, or interface method must have a return type

I wrote the code like
public partial class RegisterPatient : Form
{
int n = 0;
datatable dt;
public RegisterPatient()
{
InitializeComponent();
}

private void RegisterPatient_Load(object sender, EventArgs e)
{
SqlConnection Con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=HMS1;Data Source=ALIGNSOFT9\\SQLEXPRESS");
Con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from RegisterAdmitPatient_details", Con);
DataSet ds = new DataSet();
da.Fill(ds,"RegisterAdmitPatient_details");
dt=ds.tables["RegisterAdmitPatient_details"];
display ();
}


private void btnfirst_Click(object sender, EventArgs e)
{
n = 0;
display();
}

private void btnprevious_Click(object sender, EventArgs e)
{
n=n-1;
if(n==-1)
{
n=0;
messagebox.show("first record");
}
else
{
display();
}
}

private void btnNext_Click(object sender, EventArgs e)
{
n = n + 1;
if(n>dt.rows.count-1)
{
}
else
{
display();
}
}

private void btnlast_Click(object sender, EventArgs e)
{
n = dt.rows.count - 1;
display();
}
display() //display function
{
datarow dr=dt.rows[n];
textbox1.text=dr[0].tostring();
textbox2.text=dr[1].tostring();
}
}



Guest Author: pandu     04 Apr 2012
hi
i have datagridview with filled data when i cell double clicked
the perticular row record will go to another form of textboxes.
in that form i have next and previous buttons.
how to get next record by incrementing already existing record and
by decrementing previous record



Feedbacks      

Post Comment:




  • 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Priya jain
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India