Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
How to get last record in dataset table in asp.net & that to i want to return that record in string.

please let me know the way how to do.

[Update from OP]

What you provide i m not get as am beginner i tried with below code as your hint but it returns all thr records,here is the code
C#
SqlConnection oCon = new SqlConnection(@"Initial Catalog=master;server=192.168.1.29\SQLEXPRESS;user id=sa;password=Mobile123"); 

protected void Page_Load(object sender, EventArgs e) { } 

protected DataRow getLastRec() 
   {
     SqlDataAdapter oDa = new SqlDataAdapter("select * from Table_Test", oCon); 
     DataSet ds = new DataSet(); 
     oDa.Fill(ds); 
     DataRow dr = (DataRow)ds.Tables[0].Rows[ds.Tables[0].Rows.Count -1]; 
     return dr; 
   } 

protected void btnGet_Click(object sender, EventArgs e) 
{ 
   GridView1.DataSource = getLastRec(); GridView1.DataBind(); 
}
Posted
Updated 6-Aug-16 1:57am
v2

The Only thing you have to do is Change the query to:
"select top 1 id, col2, col3, col4, coln from Table_Test order by id desc"

By doing this, you will get only 1 row in the dataset. This row will be the last one present in your table.
In the query, 'id' is the Primary Key column and you have to write all column names from which you want data - In the above query, col2,col3,...coln are those columns.
Do this and you're done!
 
Share this answer
 
C#
String pid;
           ProductTableAdapter.Fill(MaccDataset);
           DataView dv = new DataView(MaccDataset.Tables[0]);
           pid = dv.Table.Rows[dv.Table.Rows.Count - 1][0].ToString();
 
Share this answer
 
Get the last row with DataSet.Tables(0).Rows(DataTable.Tables(0).Rows.Count -1)
Get the values using DataRow.Item[^]
 
Share this answer
 
Comments
Ramakrishna Alla 28-Jul-11 2:26am    
What you provide i m not get as am beginner

i tried with below code as your hint
but it returns all thr records,here is the code

SqlConnection oCon = new SqlConnection(@"Initial Catalog=master;server=192.168.1.29\SQLEXPRESS;user id=sa;password=Mobile123");
protected void Page_Load(object sender, EventArgs e)
{

}
protected DataRow getLastRec()
{

SqlDataAdapter oDa = new SqlDataAdapter("select * from Table_Test", oCon);
DataSet ds = new DataSet();
oDa.Fill(ds);
DataRow dr = (DataRow)ds.Tables[0].Rows[ds.Tables[0].Rows.Count -1];
return dr;



}

protected void btnGet_Click(object sender, EventArgs e)
{

GridView1.DataSource = getLastRec();

GridView1.DataBind();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900