Hi tareq16278,
Check this example. Now please take its reference and correct your code.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:DetailsView ID="dvCustomers" runat="server" AutoGenerateRows="false"
AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging">
<Fields>
<asp:BoundField HeaderText="CustomerId" DataField="CustomerId" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Country" DataField="Country" />
</Fields>
</asp:DetailsView>
<hr />
<asp:Label ID="lblName" runat="server" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void OnPageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
dvCustomers.PageIndex = e.NewPageIndex;
this.BindGrid();
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dvCustomers.DataSource = dt;
dvCustomers.DataBind();
string name = dvCustomers.Rows[1].Cells[1].Text.Replace(" ", "");
if (string.IsNullOrEmpty(name))
{
lblName.Text = "00930";
}
else
{
lblName.Text = "01";
}
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub OnPageIndexChanging(ByVal sender As Object, ByVal e As DetailsViewPageEventArgs)
dvCustomers.PageIndex = e.NewPageIndex
Me.BindGrid()
End Sub
Private Sub BindGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
dvCustomers.DataSource = dt
dvCustomers.DataBind()
Dim name As String = dvCustomers.Rows(1).Cells(1).Text.Replace(" ", "")
If String.IsNullOrEmpty(name) Then
lblName.Text = "00930"
Else
lblName.Text = "01"
End If
End Using
End Using
End Using
End Using
End Sub
Screenshot