sagarseela says:
ShowData();
da.Fill(ds,
"company"
);
You need to first fill dataset then call showdata() method.
Refer below sample.
HTML
Id :
<asp:TextBox runat="server" ID="txtId" />
<br />
Name:
<asp:TextBox runat="server" ID="txtFName" />
<br />
Country :
<asp:TextBox runat="server" ID="txtCountry" />
<br />
<asp:Button Text="First" runat="server" OnClick="btnFirst_Click" />
<asp:Button Text="Prev" runat="server" OnClick="btnPrev_Click" />
<asp:Button Text="Next" runat="server" OnClick="btnNext_Click" />
<asp:Button Text="Last" runat="server" OnClick="btnLast_Click" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
DataSet ds;
int RowIndex;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("Select CustomerId,Name,Country from Customers", con);
ds = new DataSet();
da.Fill(ds, "Customers");
ShowData();
Session["CDS"] = ds;
Session["RowIndex"] = RowIndex;
}
else
{
ds = (DataSet)Session["CDS"];
RowIndex = (int)Session["RowIndex"];
}
}
private void ShowData()
{
txtId.Text = ds.Tables[0].Rows[RowIndex]["CustomerId"].ToString();
txtFName.Text = ds.Tables[0].Rows[RowIndex]["Name"].ToString();
txtCountry.Text = ds.Tables[0].Rows[RowIndex]["Country"].ToString();
}
protected void btnFirst_Click(object sender, EventArgs e)
{
RowIndex = 0;
ShowData();
Session["RowIndex"] = RowIndex;
}
protected void btnPrev_Click(object sender, EventArgs e)
{
if (RowIndex > 0)
{
RowIndex -= 1;
ShowData();
Session["RowIndex"] = RowIndex;
}
else
{
Response.Write("<script>alert('First Record of table')</script>");
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
if (RowIndex < ds.Tables[0].Rows.Count - 1)
{
RowIndex += 1;
ShowData();
Session["RowIndex"] = RowIndex;
}
else
{
Response.Write("<script>alert('Last Record of table')</script>");
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
RowIndex = ds.Tables[0].Rows.Count - 1;
ShowData();
Session["RowIndex"] = RowIndex;
}
VB.Net
Private ds As DataSet
Private RowIndex As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Dim da As SqlDataAdapter = New SqlDataAdapter("Select CustomerId,Name,Country from Customers", con)
ds = New DataSet()
da.Fill(ds, "Customers")
ShowData()
Session("CDS") = ds
Session("RowIndex") = RowIndex
Else
ds = CType(Session("CDS"), DataSet)
RowIndex = CInt(Session("RowIndex"))
End If
End Sub
Private Sub ShowData()
txtId.Text = ds.Tables(0).Rows(RowIndex)("CustomerId").ToString()
txtFName.Text = ds.Tables(0).Rows(RowIndex)("Name").ToString()
txtCountry.Text = ds.Tables(0).Rows(RowIndex)("Country").ToString()
End Sub
Protected Sub btnFirst_Click(ByVal sender As Object, ByVal e As EventArgs)
RowIndex = 0
ShowData()
Session("RowIndex") = RowIndex
End Sub
Protected Sub btnPrev_Click(ByVal sender As Object, ByVal e As EventArgs)
If RowIndex > 0 Then
RowIndex -= 1
ShowData()
Session("RowIndex") = RowIndex
Else
Response.Write("<script>alert('First Record of table')</script>")
End If
End Sub
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs)
If RowIndex < ds.Tables(0).Rows.Count - 1 Then
RowIndex += 1
ShowData()
Session("RowIndex") = RowIndex
Else
Response.Write("<script>alert('Last Record of table')</script>")
End If
End Sub
Protected Sub btnLast_Click(ByVal sender As Object, ByVal e As EventArgs)
RowIndex = ds.Tables(0).Rows.Count - 1
ShowData()
Session("RowIndex") = RowIndex
End Sub
Screenshot
