SlideShare a Scribd company logo
form view
form view
The working of Form View control is same as
Detail View control but the default UI of Form
View control is different.
Detail View control displays the records in tabular
format. Form View control does not have
predefined layout but it depend upon template.
According to the need of application, we can
display the data with the help of template.
 We can insert, update, delete and paging the
record in Form View.
 Adding validation controls to a Form View is
easier than Detail View control. For displaying
data, Item Template is used.
Displaying Data with the Form View
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI;
public partial class FormViewDemo :
System.Web.UI.Page
{
SqlConnection conn;
SqlDataAdapter adapter;
DataSet ds;
SqlCommand cmd;
string cs =
ConfigurationManager.ConnectionStrings["conStri
ng"].ConnectionString;
protected void Page_Load(object sender,
EventArgs e)
{
if (!IsPostBack)
{
PopulateFormView();
}
}
protected void PopulateFormView()
{
try
{
conn = new SqlConnection(cs);
adapter = new SqlDataAdapter("select *
from tblEmps", conn);
ds = new DataSet();
adapter.Fill(ds);
FormView1.DataSource = ds;
FormView1.DataBind();
}
 catch (Exception ex)
{
Label1.Text = "ERROR :: " + ex.Message;
}
}
protected void
FormView1_ModeChanging(object sender,
FormViewModeEventArgs e)
{
FormView1.ChangeMode(e.NewMode);
PopulateFormView();
}
}

The ItemTemplate supports a databinding
expression that is used to bind the database
table column. The Eval() or Bind() method is used
to retrieves the values of these columns.
<ItemTemplate>
<table border="1">
<tr>
<th><b>EmpID:</b></th>
<td ><%# Eval("EmpID") %></td></tr>
<tr>
<td><b>Name:</b></td>
<td ><%# Eval("Name") %></td> </tr>
<tr>
<td><b>Gender:</b></td>
<td ><%#Eval("Gender") %></td></tr>
<tr>
<td><b>Salary:</b></td>
<td><%# Eval("Salary") %></td> </tr>
<tr>
<td><b>Address:</b></td>
<td><%# Eval("Address") %></td>
</tr>
<tr>
<td><b>DepID:</b></td>
<td><%#Eval("DepID") %>
</tr>
</table>
<asp:LinkButton ID="lnkEdit" Text="Edit
" CommandName="Edit" runat="server" />
<asp:LinkButton ID="lnkNew" Text="Ne
w" CommandName="New" runat="server" />
<asp:LinkButton ID="lnkDelete"
Text="Delete" CommandName="Delete"
runat="server" />
</ItemTemplate>
 We can perform paging with Form View
Control by using its AllowPaging property.
 By default Allow Paging property is false. Set
the value of Allow Paging property as true for
performing the paging.
 It will generate the paging link automatically.
Write code in Page Index Changing event as
follows.
protected void
FormView1_PageIndexChanging(object sender,
FormViewPageEventArgs e)
{
FormView1.PageIndex = e.NewPageIndex;
PopulateFormView();
}
OUTPUT:
 The Form View control uses templates to edit
records.
 It enables us to use Edit ItemTemplate and
specify the user interface.
 If we want simply display the record then use
Eval() method otherwise use Bind() method.
 For editing the record you must have editable
control, therefore we have used Text Box control
within Edit Item Template and bind the Text
property of Text Box
 If we click on Edit link, then Update and Cancel
link will display in place of Edit Link.
 By default these links are not available in Form
View control.
 We must provide these links in template fields
explicitly.
 Form View control has a Data Key Names
property that is used to hold the name of the
primary key from the database table.
 We have to specify a primary key explicitly when
editing records.
 This Link Button supports a Command Name
property which has the value Edit.
 When we click on Edit link the Form View mode is
changed from Read Only mode to "Edit" mode.
 We can use Button or Image Button control in
place of Link Button.
 The Bind("EmpID") method binds the Employee ID
column database table to the Text property of
the Text Box control.
<EditItemTemplate>
<table border="1">
<tr>
<th><b>EmpID:</b></th>
<td><asp:TextBox ID="txtEmpID"
Text='<%# Bind("EmpID") %>' runat="Server"
/></td>
</tr>
<tr>
<td><b>Name:</b></td>
<td><asp:TextBox ID="txtName"
Text='<%# Bind("Name") %>' runat="Server" /></td>
</tr>
<tr>
<td><b>Gender:</b></td>
<td><asp:TextBox ID="txtGender"
Text='<%# Bind("Gender") %>' runat="Server"/>
</td>
</tr>
<tr>
 <td><b>Salary:</b></td>
<td><asp:TextBox ID="txtSalary"
Text='<%# Bind("Salary") %>' runat="Server"
/></td>
</tr>
<tr>
<td><b>Address:</b></td>
<td><asp:TextBox
ID="txtAddress" Text='<%# Bind("Address") %>'
runat="Server" /></td>
</tr>
<tr>
<td><b>DepID:</b></td>
<td><asp:TextBox
ID="txtDeptID" Text='<%# Bind("DepID") %>'
runat="Server" /></td>
</tr>
</table>
<asp:LinkButton
ID="lnkUpdate" Text="Update"
CommandName="Update" runat="server" />
<asp:LinkButton
ID="lnkCancel" Text="Cancel"
CommandName="Cancel" runat="server" />
</EditItemTemplate>
 When we click on Update link ItemUpdating event
fires.
 We have already added TextBox in
EditItemTemplate. Access these textboxes with
the help of FindControl method. Write code for
updating the record as given below.
protected void FormView1_ItemUpdating(object sender,
FormViewUpdateEventArgs e)
{
int ID =
Convert.ToInt32((FormView1.FindControl("txtEmpID") as
TextBox).Text);
string empName = (FormView1.FindControl("txtName") as
TextBox).Text;
string empGender = (FormView1.FindControl("txtGender")
as TextBox).Text;
double empSalary =
Convert.ToDouble((FormView1.FindControl("txtSalary") as
TextBox).Text);
string empAddress = (FormView1.FindControl("txtAddress")
as TextBox).Text;
int depID =
Convert.ToInt32((FormView1.FindControl("txtDeptID") as
TextBox).Text);
string updateQuery = "update tblEmps set
name='"+empName+"',
gender='"+empGender+"',salary="+empSalary+",address='
"+empAddress+"',
depid="+depID+" where empid="+ID;
conn = new SqlConnection(cs);
cmd = new SqlCommand(updateQuery, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
FormView1.ChangeMode(FormViewMode.Re
adOnly);
PopulateFormView();
}
OUTPUT:
 We can insert new record into the database with
the help of Form View Control.
 Form View control supports Insert Item Template
that is used to insert new record in database.
 The Item Template contains the "New" Link
Button. When you click on "New" Link Button,
control goes from Read Only mode to Insert
mode. Insert and Cancel Link Button is appeared
in place of "New" Link Button.
<InsertItemTemplate>
<table border="1">
<tr>
<th><b>EmpID:</b></th>
<td><asp:TextBox ID="txtEmpID"
Text='<%# Bind("EmpID") %>' runat="Server" /></td>
</tr>
<tr>
<td><b>Name:</b></td>
<td><asp:TextBox ID="txtName"
Text='<%# Bind("Name") %>' runat="Server" /></td>
</tr>
<tr>
<td><b>Gender:</b></td>
<td><asp:TextBox ID="txtGender"
Text='<%# Bind("Gender") %>' runat="Server" /> </td>
</tr>
<tr>
<td><b>Salary:</b></td>
<td><asp:TextBox ID="txtSalary"
Text='<%# Bind("Salary") %>' runat="Server"/>
</td>
</tr>
<tr>
<td><b>Address:</b></td>
<td><asp:TextBox
ID="txtAddress" Text='<%# Bind("Address") %>'
runat="Server" /> </td>
</tr>
<tr>
<td><b>DepID:</b></td>
<td><asp:TextBox
ID="txtDeptID" Text='<%# Bind("DepID") %>'
runat="Server" /></td>
</tr>
</table>
<asp:LinkButton ID="lnkInsert"
Text="Insert" CommandName="Insert" r
<asp:LinkButton ID="lnkCancel" Text="Cancel"
CommandName="Cancel" runat="server" />
</InsertItemTemplate>
When you click on Insert Link Button, ItemInserting event is fires.
protected void FormView1_ItemInserting(object sender,
FormViewInsertEventArgs e)
{
string ID = (FormView1.FindControl("txtEmpID") as
TextBox).Text;
string empName = (FormView1.FindControl("txtName") as
TextBox).Text;
string empGender = (FormView1.FindControl("txtGender") as
TextBox).Text;
double empSalary =
Convert.ToDouble((FormView1.FindControl("txtSalary") as
TextBox).Text);
string empAddress = (FormView1.FindControl("txtAddress") as
TextBox).Text;
string depID = (FormView1.FindControl("txtDeptID") as
TextBox).Text;
string insertQuery = "insert into tblEmps
values("+ID+",'"+empName+"','"+empGender+"',
"+empSalary+",'"+empAddress+"',"+depID+")";
conn = new SqlConnection(cs);
cmd = new SqlCommand(insertQuery, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
FormView1.ChangeMode(FormViewMode.Re
adOnly);
PopulateFormView();
}
OUTPUT:
form view

More Related Content

PPSX
ADO.NET
Farzad Wadia
 
PPTX
anatomy of a jsp page & jsp syntax.pptx
Sameenafathima4
 
PPTX
IOT DATA MANAGEMENT AND COMPUTE STACK.pptx
MeghaShree665225
 
PPS
Architecture of-dbms-and-data-independence
Anuj Modi
 
PPTX
Design Concepts in Software Engineering-1.pptx
KarthigaiSelviS3
 
PPTX
Ado.Net Tutorial
prabhu rajendran
 
PPTX
Cloud Computing Design Considerations
Mike Kavis
 
ADO.NET
Farzad Wadia
 
anatomy of a jsp page & jsp syntax.pptx
Sameenafathima4
 
IOT DATA MANAGEMENT AND COMPUTE STACK.pptx
MeghaShree665225
 
Architecture of-dbms-and-data-independence
Anuj Modi
 
Design Concepts in Software Engineering-1.pptx
KarthigaiSelviS3
 
Ado.Net Tutorial
prabhu rajendran
 
Cloud Computing Design Considerations
Mike Kavis
 

What's hot (20)

PPTX
Data warehousing
Shruti Dalela
 
PPTX
Distributed Databases
Meghaj Mallick
 
PPT
Analysis modeling in software engineering
MuhammadTalha436
 
PPTX
Version Stamps in NOSQL Databases
Dr-Dipali Meher
 
PPTX
Multidimensional data models
774474
 
PPSX
Functional dependency
Dashani Rajapaksha
 
PPT
Data models
Usman Tariq
 
PDF
Database 2 ddbms,homogeneous & heterognus adv & disadvan
Iftikhar Ahmad
 
PPT
RichControl in Asp.net
Bhumivaghasiya
 
PPTX
Software Design and Modularity
Danyal Ahmad
 
DOC
IOT Reference Model.doc
venui2
 
PPTX
Component Based Software Engineering
SatishDabhi1
 
PPTX
Dbms architecture
Shubham Dwivedi
 
DOCX
Index in sql server
Durgaprasad Yadav
 
PPTX
Degree of relationship set
Megha Sharma
 
PPT
1. Introduction to DBMS
koolkampus
 
PDF
Module 5-cloud computing-SECURITY IN THE CLOUD
Sweta Kumari Barnwal
 
DOCX
Star ,Snow and Fact-Constullation Schemas??
Abdul Aslam
 
PPT
Chapter10 conceptual data modeling
Dhani Ahmad
 
PDF
OLAP in Data Warehouse
SOMASUNDARAM T
 
Data warehousing
Shruti Dalela
 
Distributed Databases
Meghaj Mallick
 
Analysis modeling in software engineering
MuhammadTalha436
 
Version Stamps in NOSQL Databases
Dr-Dipali Meher
 
Multidimensional data models
774474
 
Functional dependency
Dashani Rajapaksha
 
Data models
Usman Tariq
 
Database 2 ddbms,homogeneous & heterognus adv & disadvan
Iftikhar Ahmad
 
RichControl in Asp.net
Bhumivaghasiya
 
Software Design and Modularity
Danyal Ahmad
 
IOT Reference Model.doc
venui2
 
Component Based Software Engineering
SatishDabhi1
 
Dbms architecture
Shubham Dwivedi
 
Index in sql server
Durgaprasad Yadav
 
Degree of relationship set
Megha Sharma
 
1. Introduction to DBMS
koolkampus
 
Module 5-cloud computing-SECURITY IN THE CLOUD
Sweta Kumari Barnwal
 
Star ,Snow and Fact-Constullation Schemas??
Abdul Aslam
 
Chapter10 conceptual data modeling
Dhani Ahmad
 
OLAP in Data Warehouse
SOMASUNDARAM T
 
Ad

Similar to form view (20)

PPTX
Detail view in distributed technologies
jamessakila
 
DOC
Cis407 a ilab 4 web application development devry university
lhkslkdh89009
 
DOCX
This is part 1 of 3STEP 1 Modify the clsDataLayer to Use a Two-St.docx
abhi353063
 
DOCX
Cis 407 i lab 5 of 7
helpido9
 
DOCX
Cis 407 i lab 4 of 7
helpido9
 
PDF
The entity framework 4.0 and asp.net web forms getting started
Steve Xu
 
DOC
Cis407 a ilab 5 web application development devry university
lhkslkdh89009
 
DOCX
Cis 407 i lab 2 of 7
helpido9
 
PPTX
Asp PPT (.NET )
Ankit Gupta
 
PPTX
Application of Insert and select notes.ppt x
doxafo4842
 
DOCX
Grid view control
Paneliya Prince
 
PDF
The entity framework 4 and asp net web forms
Albertz Ace-Red
 
PDF
Windows Forms For Beginners Part - 3
Bhushan Mulmule
 
PPTX
Microsoft dynamics ax2012 : forms and tables methods call sequences, How To?
Mohamed Amine HAMDAOUI
 
PPTX
Microsoft asp.net online training
training3
 
PPTX
Microsoft asp.net online training
training13acutesoft
 
DOC
Cis407 a ilab 2 web application development devry university
lhkslkdh89009
 
DOCX
A Skills Approach Access 2013 Chapter 3 Working with Forms a.docx
ransayo
 
PPTX
Basic Net Online Training in Hyderabad
Ugs8008
 
Detail view in distributed technologies
jamessakila
 
Cis407 a ilab 4 web application development devry university
lhkslkdh89009
 
This is part 1 of 3STEP 1 Modify the clsDataLayer to Use a Two-St.docx
abhi353063
 
Cis 407 i lab 5 of 7
helpido9
 
Cis 407 i lab 4 of 7
helpido9
 
The entity framework 4.0 and asp.net web forms getting started
Steve Xu
 
Cis407 a ilab 5 web application development devry university
lhkslkdh89009
 
Cis 407 i lab 2 of 7
helpido9
 
Asp PPT (.NET )
Ankit Gupta
 
Application of Insert and select notes.ppt x
doxafo4842
 
Grid view control
Paneliya Prince
 
The entity framework 4 and asp net web forms
Albertz Ace-Red
 
Windows Forms For Beginners Part - 3
Bhushan Mulmule
 
Microsoft dynamics ax2012 : forms and tables methods call sequences, How To?
Mohamed Amine HAMDAOUI
 
Microsoft asp.net online training
training3
 
Microsoft asp.net online training
training13acutesoft
 
Cis407 a ilab 2 web application development devry university
lhkslkdh89009
 
A Skills Approach Access 2013 Chapter 3 Working with Forms a.docx
ransayo
 
Basic Net Online Training in Hyderabad
Ugs8008
 
Ad

Recently uploaded (20)

PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
Basics and rules of probability with real-life uses
ravatkaran694
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 

form view

  • 3. The working of Form View control is same as Detail View control but the default UI of Form View control is different. Detail View control displays the records in tabular format. Form View control does not have predefined layout but it depend upon template. According to the need of application, we can display the data with the help of template.
  • 4.  We can insert, update, delete and paging the record in Form View.  Adding validation controls to a Form View is easier than Detail View control. For displaying data, Item Template is used. Displaying Data with the Form View
  • 5. using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Web.UI.WebControls; using System.Web.UI; public partial class FormViewDemo : System.Web.UI.Page { SqlConnection conn; SqlDataAdapter adapter; DataSet ds; SqlCommand cmd; string cs = ConfigurationManager.ConnectionStrings["conStri ng"].ConnectionString; protected void Page_Load(object sender, EventArgs e)
  • 6. { if (!IsPostBack) { PopulateFormView(); } } protected void PopulateFormView() { try { conn = new SqlConnection(cs); adapter = new SqlDataAdapter("select * from tblEmps", conn); ds = new DataSet(); adapter.Fill(ds); FormView1.DataSource = ds; FormView1.DataBind(); }
  • 7.  catch (Exception ex) { Label1.Text = "ERROR :: " + ex.Message; } } protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e) { FormView1.ChangeMode(e.NewMode); PopulateFormView(); } }  The ItemTemplate supports a databinding expression that is used to bind the database table column. The Eval() or Bind() method is used to retrieves the values of these columns.
  • 8. <ItemTemplate> <table border="1"> <tr> <th><b>EmpID:</b></th> <td ><%# Eval("EmpID") %></td></tr> <tr> <td><b>Name:</b></td> <td ><%# Eval("Name") %></td> </tr> <tr> <td><b>Gender:</b></td> <td ><%#Eval("Gender") %></td></tr> <tr> <td><b>Salary:</b></td> <td><%# Eval("Salary") %></td> </tr> <tr> <td><b>Address:</b></td>
  • 9. <td><%# Eval("Address") %></td> </tr> <tr> <td><b>DepID:</b></td> <td><%#Eval("DepID") %> </tr> </table> <asp:LinkButton ID="lnkEdit" Text="Edit " CommandName="Edit" runat="server" /> <asp:LinkButton ID="lnkNew" Text="Ne w" CommandName="New" runat="server" /> <asp:LinkButton ID="lnkDelete" Text="Delete" CommandName="Delete" runat="server" /> </ItemTemplate>
  • 10.  We can perform paging with Form View Control by using its AllowPaging property.  By default Allow Paging property is false. Set the value of Allow Paging property as true for performing the paging.  It will generate the paging link automatically. Write code in Page Index Changing event as follows.
  • 11. protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e) { FormView1.PageIndex = e.NewPageIndex; PopulateFormView(); } OUTPUT:
  • 12.  The Form View control uses templates to edit records.  It enables us to use Edit ItemTemplate and specify the user interface.  If we want simply display the record then use Eval() method otherwise use Bind() method.  For editing the record you must have editable control, therefore we have used Text Box control within Edit Item Template and bind the Text property of Text Box
  • 13.  If we click on Edit link, then Update and Cancel link will display in place of Edit Link.  By default these links are not available in Form View control.  We must provide these links in template fields explicitly.  Form View control has a Data Key Names property that is used to hold the name of the primary key from the database table.  We have to specify a primary key explicitly when editing records.
  • 14.  This Link Button supports a Command Name property which has the value Edit.  When we click on Edit link the Form View mode is changed from Read Only mode to "Edit" mode.  We can use Button or Image Button control in place of Link Button.  The Bind("EmpID") method binds the Employee ID column database table to the Text property of the Text Box control.
  • 15. <EditItemTemplate> <table border="1"> <tr> <th><b>EmpID:</b></th> <td><asp:TextBox ID="txtEmpID" Text='<%# Bind("EmpID") %>' runat="Server" /></td> </tr> <tr> <td><b>Name:</b></td> <td><asp:TextBox ID="txtName" Text='<%# Bind("Name") %>' runat="Server" /></td> </tr> <tr> <td><b>Gender:</b></td> <td><asp:TextBox ID="txtGender" Text='<%# Bind("Gender") %>' runat="Server"/> </td> </tr> <tr>
  • 16.  <td><b>Salary:</b></td> <td><asp:TextBox ID="txtSalary" Text='<%# Bind("Salary") %>' runat="Server" /></td> </tr> <tr> <td><b>Address:</b></td> <td><asp:TextBox ID="txtAddress" Text='<%# Bind("Address") %>' runat="Server" /></td> </tr> <tr> <td><b>DepID:</b></td> <td><asp:TextBox ID="txtDeptID" Text='<%# Bind("DepID") %>' runat="Server" /></td>
  • 17. </tr> </table> <asp:LinkButton ID="lnkUpdate" Text="Update" CommandName="Update" runat="server" /> <asp:LinkButton ID="lnkCancel" Text="Cancel" CommandName="Cancel" runat="server" /> </EditItemTemplate>  When we click on Update link ItemUpdating event fires.  We have already added TextBox in EditItemTemplate. Access these textboxes with the help of FindControl method. Write code for updating the record as given below.
  • 18. protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e) { int ID = Convert.ToInt32((FormView1.FindControl("txtEmpID") as TextBox).Text); string empName = (FormView1.FindControl("txtName") as TextBox).Text; string empGender = (FormView1.FindControl("txtGender") as TextBox).Text; double empSalary = Convert.ToDouble((FormView1.FindControl("txtSalary") as TextBox).Text); string empAddress = (FormView1.FindControl("txtAddress") as TextBox).Text; int depID = Convert.ToInt32((FormView1.FindControl("txtDeptID") as TextBox).Text); string updateQuery = "update tblEmps set name='"+empName+"', gender='"+empGender+"',salary="+empSalary+",address=' "+empAddress+"', depid="+depID+" where empid="+ID; conn = new SqlConnection(cs); cmd = new SqlCommand(updateQuery, conn);
  • 20.  We can insert new record into the database with the help of Form View Control.  Form View control supports Insert Item Template that is used to insert new record in database.  The Item Template contains the "New" Link Button. When you click on "New" Link Button, control goes from Read Only mode to Insert mode. Insert and Cancel Link Button is appeared in place of "New" Link Button.
  • 21. <InsertItemTemplate> <table border="1"> <tr> <th><b>EmpID:</b></th> <td><asp:TextBox ID="txtEmpID" Text='<%# Bind("EmpID") %>' runat="Server" /></td> </tr> <tr> <td><b>Name:</b></td> <td><asp:TextBox ID="txtName" Text='<%# Bind("Name") %>' runat="Server" /></td> </tr> <tr> <td><b>Gender:</b></td> <td><asp:TextBox ID="txtGender" Text='<%# Bind("Gender") %>' runat="Server" /> </td> </tr> <tr>
  • 22. <td><b>Salary:</b></td> <td><asp:TextBox ID="txtSalary" Text='<%# Bind("Salary") %>' runat="Server"/> </td> </tr> <tr> <td><b>Address:</b></td> <td><asp:TextBox ID="txtAddress" Text='<%# Bind("Address") %>' runat="Server" /> </td> </tr> <tr> <td><b>DepID:</b></td> <td><asp:TextBox ID="txtDeptID" Text='<%# Bind("DepID") %>' runat="Server" /></td> </tr> </table> <asp:LinkButton ID="lnkInsert" Text="Insert" CommandName="Insert" r
  • 23. <asp:LinkButton ID="lnkCancel" Text="Cancel" CommandName="Cancel" runat="server" /> </InsertItemTemplate> When you click on Insert Link Button, ItemInserting event is fires. protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e) { string ID = (FormView1.FindControl("txtEmpID") as TextBox).Text; string empName = (FormView1.FindControl("txtName") as TextBox).Text; string empGender = (FormView1.FindControl("txtGender") as TextBox).Text; double empSalary = Convert.ToDouble((FormView1.FindControl("txtSalary") as TextBox).Text); string empAddress = (FormView1.FindControl("txtAddress") as TextBox).Text; string depID = (FormView1.FindControl("txtDeptID") as TextBox).Text; string insertQuery = "insert into tblEmps values("+ID+",'"+empName+"','"+empGender+"', "+empSalary+",'"+empAddress+"',"+depID+")"; conn = new SqlConnection(cs); cmd = new SqlCommand(insertQuery, conn);