In this Article I have
explained how to show record in Form view data control. In this control we can
show data one by one. If there is a requirement to show data one by one then
this is very useful control.
Implementation: Let's create a demo
website to demonstrate the concept.
Create a database “Test”, Then
create one table i.e. Emp_Personal
Column
Name
|
Data
Type
|
EmpPer_Id
|
Int(Primary Key. So set is
identity=true)
|
EmpName
|
varchar(100)
|
Age
|
Int
|
Address
|
varchar(500)
|
Create a stored procedure to
get Employee details to be filled in Form View Data Control.
CREATE PROCEDURE GetEmp_SP
AS
BEGIN
SELECT * FROM dbo.Emp_Personal
END
create the connection string in Webconfig as:
<connectionStrings>
<add name="Empcon" connectionString="Data Source=localhost;Initial
Catalog=test;IntegratedSecurity=True"/>
</connectionStrings>
Source Code:
<fieldset style="width:300px;">
<legend> <strong>Form view example</strong></legend>
<asp:FormView ID="formviewEmp" runat="server" DataKeyNames="EmpPer_ID"
AllowPaging="True" onpageindexchanging="formviewEmp_PageIndexChanging1"
>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
<table style="border:1px solid #c1c1c1; width:250px">
<tr style="background-color:#E5E5FE;font-weight:bold"><td>Employee Detail</td></tr>
<tr><td></td></tr>
<tr> <td><b>Employee Name:-</b></td><td><asp:Label ID="lblstudentname" runat="server" Text='<%# Eval("EmpNAME") %>'></asp:Label></td></tr>
<tr><td><b>Age:-</b></td><td><asp:Label ID="lblstudentaddress"
runat="server"
Text='<%# Eval("age") %>'></asp:Label></td></tr>
<tr><td><b>Address:-</b></td><td><asp:Label ID="lblstudentclass"
runat="server"
Text='<%# Eval("Address") %>'></asp:Label></td></tr>
</table>
</ItemTemplate>
<EmptyDataTemplate>
<table style="border:1px solid #c1c1c1;">
<tr style="background-color:#E5E5FE;font-weight:bold"><td><b>Student Details</b></td></tr>
<tr><td><b> Name:-</b></td><td style="color:Red;">No
Records Available!</td></tr>
<tr><td><b>Age:-</b></td><td style="color:Red;">No
Records Available!</td></tr>
<tr><td><b>Address:-</b></td><td style="color:Red;">No
Records Available!</td></tr>
</table>
</EmptyDataTemplate>
</asp:FormView>
</fieldset>
ASP.Net Code(C#)
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial
class Searchgrid
: System.Web.UI.Page
{
SqlConnection
con = new SqlConnection(ConfigurationManager.ConnectionStrings["Empcon"].ConnectionString);
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BIndFormview();
}
}
private void BIndFormview()
{
try
{
SqlCommand
cmd = new SqlCommand("GetEmp_SP", con);
con.Open();
SqlDataAdapter
adp = new SqlDataAdapter(cmd);
DataTable
dt = new DataTable();
adp.Fill(dt);
if
(dt.Rows.Count > 0)
{
formviewEmp.DataSource = dt;
formviewEmp.DataBind();
con.Close();
cmd.Dispose();
}
else
{
formviewEmp.DataSource = null;
formviewEmp.DataBind();
}
}
catch (Exception ex)
{
}
}
protected void formviewEmp_PageIndexChanging1(object sender, FormViewPageEventArgs
e)
{
formviewEmp.PageIndex = e.NewPageIndex;
BIndFormview();
}
VB.Net Code:
Write the following code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Public
Class Searchgrid
Inherits
System.Web.UI.Page
Dim con As SqlConnection =
New SqlConnection(ConfigurationManager.ConnectionStrings("Empcon").ConnectionString)
Protected Sub Page_Load(ByVal
sender As Object,
ByVal e As EventArgs)
If Not IsPostBack Then
BIndFormview()
End If
End Sub
Private Sub BIndFormview()
Try
Dim
cmd As SqlCommand
= New SqlCommand("GetEmp_SP", con)
con.Open()
Dim
adp As SqlDataAdapter
= New SqlDataAdapter(cmd)
Dim
dt As DataTable
= New DataTable()
adp.Fill(dt)
If
dt.Rows.Count > 0 Then
formviewEmp.DataSource = dt
formviewEmp.DataBind()
con.Close()
cmd.Dispose()
Else
formviewEmp.DataSource = Nothing
formviewEmp.DataBind()
End
If
End Try
End Sub
protected
void formviewEmp_PageIndexChanging1(Object sender, FormViewPageEventArgs e)
{
formviewEmp.PageIndex = e.NewPageIndex
BIndFormview()
End Class
{
Dim con As
SqlConnection = New
SqlConnection(ConfigurationManager.ConnectionStrings("Empcon").ConnectionString)
Protected Sub
Page_Load(ByVal sender As
Object, ByVal e
As EventArgs)
If Not IsPostBack Then
BIndFormview()
End If
End Sub
Private Sub
BIndFormview()
Try
Dim cmd
As SqlCommand
= New SqlCommand("GetEmp_SP", con)
con.Open()
Dim adp
As SqlDataAdapter
= New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
adp.Fill(dt)
If
dt.Rows.Count > 0 Then
formviewEmp.DataSource = dt
formviewEmp.DataBind()
con.Close()
cmd.Dispose()
Else
formviewEmp.DataSource = Nothing
formviewEmp.DataBind()
End If
End Try
End Sub
Protected Sub
formviewEmp_PageIndexChanging1(ByVal sender As Object, ByVal e As FormViewPageEventArgs)
formviewEmp.PageIndex = e.NewPageIndex
BIndFormview()
End Sub
0 comments:
Post a Comment