Search Asp.Net Here

Insert,Display,Edit,Update,Delete data in FormView control in Asp.Net




How to insert,display,edit,update,delete data in FormView control in asp.net


FormView is a databound control used to to insert,display,edit,update,delete data in asp.net that renders a single record at a time .FormView control is similar to DetailView in asp.net but the only difference is that DetailsView has a built-in tabular rendering whereas FormView requires a user-defined template to insert,display,edit, update and delete data.

FormView define command names for button controls which call the correspondence template of FormView which are define at design time by developers.

Some common template supported by Formview are given below;

EditItemTemplate: EditItemTemplate is used when a record is to be edited.
InsertItemTemplate: InsertItemTemplate is used when a record is to be created.
ItemTemplate: ItemTemplate:is used to display the record.

FormView control uses modes to insert,display and edit data





ReadOnly Mode—call ItemTemplate to display data
Insert Mode-call InsertItemTemplate of FormView and enable it to insert new record
Edit Mode—call EditItemTemplate to edit the selected record

In the example I am using follwing sqlserver database table. Use script to create table in your sqlserver database

CREATE TABLE tbemp
(
eno int NULL ,
ename varchar (50) NULL,
eadd varchar (50) NULL,
esal int NULL
)
GO

Write the code in your .aspx page

<asp:FormView ID="FormView1" runat="server" CellPadding="4" ForeColor="#333333" AllowPaging="True" OnItemInserting="FormView1_ItemInserting" OnModeChanging="FormView1_ModeChanging" OnPageIndexChanging="FormView1_PageIndexChanging" Height="187px" OnItemDeleting="FormView1_ItemDeleting" OnItemUpdating="FormView1_ItemUpdating" Width="347px">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<EditItemTemplate>
Emp No<asp:Label ID="Label5" runat="server" Text='<%# Eval("eno") %>' ></asp:Label><br />
Name   <asp:TextBox ID="TextBox1" Text='<%# Eval("ename") %>' runat="server"></asp:TextBox><br />
Address<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("eadd") %>'></asp:TextBox><br />
Salary  <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("esal") %>'></asp:TextBox><br /><br />
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="update">Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<ItemTemplate>
EmpNo  <asp:Label ID="Label1" runat="server" Text='<%# Eval("eno") %>'></asp:Label><br />
Name     <asp:Label ID="Label2" runat="server" Text='<%# Eval("ename") %>'></asp:Label><br />
Address <asp:Label ID="Label3" runat="server" Text='<%# Eval("eadd") %>'></asp:Label><br />
Salary    <asp:Label ID="Label4" runat="server" Text='<%# Eval("esal") %>'></asp:Label><br />
<br />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">Edit</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="new">New</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="delete">Delete</asp:LinkButton>
</ItemTemplate>
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<EmptyDataTemplate>
No Data Available<br />
Insert Data ?<br />
<br />
<asp:LinkButton ID="LinkButton8" runat="server" CommandName="new">Yes</asp:LinkButton>
<asp:LinkButton ID="LinkButton9" runat="server" CommandName="Cancel">No</asp:LinkButton>
</EmptyDataTemplate>
<InsertItemTemplate>
Emp No<asp:TextBox ID="TextBox4" runat="server" ></asp:TextBox><br />
Name  <asp:TextBox ID="TextBox5" runat="server" ></asp:TextBox><br />
Address<asp:TextBox ID="TextBox6" runat="server" ></asp:TextBox><br />
Salary  <asp:TextBox ID="TextBox7" runat="server" ></asp:TextBox><br /><br />
<asp:LinkButton ID="LinkButton6" runat="server" CommandName="insert">Save</asp:LinkButton>
<asp:LinkButton ID="LinkButton7" runat="server" CommandName="cancel">Cancel</asp:LinkButton>
</InsertItemTemplate>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
</asp:FormView>

Write following code in your page_load event

if (!Page.IsPostBack)
formbind();
Now write following code below your page_load Event in .cs file

//method to bind FormView with database
private void formbind()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from tbemp", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
//change the connection string that matches yours
DataSet ds = new DataSet();
adp.Fill(ds);
FormView1.DataSource = ds;
FormView1.DataBind();
}
//evnent to chage the page index (paging in formview)
protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
FormView1.PageIndex = e.NewPageIndex;
formbind();
}
//Event to Change Modes of FormVew
protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
{
if (e.NewMode == FormViewMode.Edit)
FormView1.ChangeMode(FormViewMode.Edit);
else if (e.NewMode == FormViewMode.Insert)
FormView1.ChangeMode(FormViewMode.Insert);
else
FormView1.ChangeMode(FormViewMode.ReadOnly);
formbind();
}
//FormView Event to insert new record in database
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
Int32 eno, es;
String ea, en;
eno = Convert.ToInt32(((TextBox)(FormView1.FindControl("TextBox4"))).Text);
en = ((TextBox)(FormView1.FindControl("TextBox5"))).Text;
ea = ((TextBox)(FormView1.FindControl("TextBox6"))).Text;
es = Convert.ToInt32(((TextBox)(FormView1.FindControl("TextBox7"))).Text);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("insert into tbemp values(@eno,@en,@ea,@es)", con);
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = eno;
cmd.Parameters.Add("@en", SqlDbType.VarChar,50).Value = en;
cmd.Parameters.Add("@ea", SqlDbType.VarChar, 50).Value = ea;
cmd.Parameters.Add("@es", SqlDbType.Int).Value = es;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
FormView1.ChangeMode(FormViewMode.ReadOnly);
formbind();
}

//Event to update record in databae
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
Int32 eno, es;
String ea, en;
eno = Convert.ToInt32(((Label)(FormView1.FindControl("Label5"))).Text);
en = ((TextBox)(FormView1.FindControl("TextBox1"))).Text;
ea = ((TextBox)(FormView1.FindControl("TextBox2"))).Text;
es = Convert.ToInt32(((TextBox)(FormView1.FindControl("TextBox3"))).Text);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("update tbemp set ename=@en,eadd=@ea,esal=@es where eno=@eno", con);
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = eno;
cmd.Parameters.Add("@en", SqlDbType.VarChar, 50).Value = en;
cmd.Parameters.Add("@ea", SqlDbType.VarChar, 50).Value = ea;
cmd.Parameters.Add("@es", SqlDbType.Int).Value = es;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
//changing mode to read only
FormView1.ChangeMode(FormViewMode.ReadOnly);
formbind();
}

//event deleting record in databae
protected void FormView1_ItemDeleting(object sender, FormViewDeleteEventArgs e)
{
int eno = Convert.ToInt32(((Label)(FormView1.FindControl("Label1"))).Text);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("delete from tbemp where eno=@eno", con);
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = eno;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
formbind();
}

Comments :

0 comments to “Insert,Display,Edit,Update,Delete data in FormView control in Asp.Net”

Post a Comment