Problem in providing Navigate Facillities using DataSet in Asp.net? [Resolved]

Posted by gurramsra1-10894 under ASP.NET on 1/28/2012 | Points: 10 | Views : 1407 | Status : [Member] | Replies : 5
I Want to provide Navigate Facillities using DataSet in Asp.net. When we click on Next Button it should be displayed Next Record values in Textboxes. The Following code not working correctly. What is the problem in this? But it is working in WindowsForrms?

public partial class RoI2Test : System.Web.UI.Page
{
SqlConnection con=new SqlConnection("user id=sa;password=hari;database=emp;server=.");
SqlDataAdapter da;
DataSet ds;
int r;
protected void Page_Load(object sender, EventArgs e)
{
string s="select * from Products";
da = new SqlDataAdapter(s, con);
ds = new DataSet();
da.Fill(ds, "x");
}
public void getdata()
{
txtPName.Text = ds.Tables[0].Rows[r][0].ToString();
txtPrice.Text = ds.Tables[0].Rows[r][1].ToString();
txtStatus.Text = ds.Tables[0].Rows[r][2].ToString();
lblDisplay.Text = (r + 1) + " Th row of" + ds.Tables[0].Rows.Count;
}

protected void btnPrevious_Click(object sender, EventArgs e)
{
if (r == 0)
lblDisplay.Text = "This is First Row";
else
{
r = r - 1;
getdata();
}


}
protected void btnNext_Click(object sender, EventArgs e)
{
r = r + 1;
getdata();
}
}

Sra1


Responses

Posted by: Sksamantaray on: 1/30/2012 [Member] Silver | Points: 50

Up
0
Down

Resolved
using System;

using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"uid=sa;password=pwd;database=northwind;server=.");
static SqlDataAdapter da;
static DataSet ds;
static int r;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string s = "select * from Products";
da = new SqlDataAdapter(s, con);
ds = new DataSet();
da.Fill(ds, "x");
getdata();
}
else
{
getdata();
}
}
public void getdata()
{
txtPName.Text = ds.Tables[0].Rows[r][0].ToString();
txtPrice.Text = ds.Tables[0].Rows[r][1].ToString();
txtStatus.Text = ds.Tables[0].Rows[r][2].ToString();
lblDisplay.Text = (r + 1) + " Th row of" + ds.Tables[0].Rows.Count;
}

protected void btnPrevious_Click(object sender, EventArgs e)
{
if (r == 0)
lblDisplay.Text = "This is First Row";
else
{
r = r - 1;
getdata();
}


}
protected void btnNext_Click(object sender, EventArgs e)
{
r = r + 1;
getdata();
}
}


try this

Thanks,
Sanjay

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Avdesh on: 1/28/2012 [Member] Starter | Points: 25

Up
0
Down
what is the error in this code?

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: gurramsra1-10894 on: 1/29/2012 [Member] Starter | Points: 25

Up
0
Down
The Error means it will not give the correct result.

when we click on next button always it displays only second record. It will not go the next record.
That is the problem.


Thank you

Sra1

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 1/30/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Set the default value of r is "0" and try again.

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: gurramsra1-10894 on: 1/30/2012 [Member] Starter | Points: 25

Up
0
Down
Thank You very much.

SRA1

Sra1

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response