Example for formview control by using SqlDataSource.
Can anyone explain the Formview control?with example by using sqlDatasource ?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Jul 23, 2012, 1:46 AM
Please refer the below links
http://www.dotnetfunda.com/tutorials/controls/formview.aspx
http://www.beansoftware.com/ASP.NET-Tutorials/FormView-Control.aspx
Thanks
If this post helps you mark it as answer
Rajkamal SPosted Jul 23, 2012, 12:58 AM
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
DataKeyNames="EmployeeID"
Gridlines="Both" AllowPaging="true"
RunAt="server" onitemdeleting="EmployeeFormView_ItemDeleting"
onpageindexchanging="EmployeeFormView_PageIndexChanging"
onitemupdating="EmployeeFormView_ItemUpdating"
oniteminserted="EmployeeFormView_ItemInserted"
oniteminserting="EmployeeFormView_ItemInserting"
onitemupdated="EmployeeFormView_ItemUpdated"
onmodechanging="EmployeeFormView_ModeChanging" BackColor="#FFFF66"
BorderColor="#FF8080">
forecolor="#999966"/>
Text="Edit"
CommandName="Edit"
RunAt="server"/>
Text="New"
CommandName="New"
RunAt="server"/>
Text="Delete"
CommandName="Delete"
RunAt="server"/>
Text='<%# Bind("EmployeeID") %>'
RunAt="Server" />
Text='<%# Bind("FirstName") %>'
RunAt="Server" />
Text='<%# Bind("LastName") %>'
RunAt="Server" />
Text='<%# Bind("Address") %>'
RunAt="Server" />
Text='<%# Bind("Designation") %>'
RunAt="Server" />
Text="Update"
CommandName="Update"
RunAt="server"/>
Text="Cancel"
CommandName="Cancel"
RunAt="server"/>
Text='<%# Bind("EmployeeID") %>'
RunAt="Server" />
Text='<%# Bind("FirstName") %>'
RunAt="Server" />
Text='<%# Bind("LastName") %>'
RunAt="Server" />
Text='<%# Bind("Address") %>'
RunAt="Server" />
Text='<%# Bind("Designation") %>'
RunAt="Server" />
Text="Insert"
CommandName="Insert"
RunAt="server"/>
Text="Cancel"
CommandName="Cancel"
RunAt="server"/>
Default.aspx.cs code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlDataAdapter sqlda = new SqlDataAdapter();
SqlCommand com = new SqlCommand();
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
private void bindgrid()
{
SqlConnection conn = new SqlConnection(connStr);
dt = new DataTable();
com.Connection = conn;
com.CommandText = "SELECT * FROM Employees";
sqlda = new SqlDataAdapter(com);
sqlda.Fill(dt);
EmployeeFormView.DataSource = dt;
EmployeeFormView.DataBind();
}
protected void EmployeeFormView_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
EmployeeFormView.PageIndex = e.NewPageIndex;
bindgrid();
}
protected void EmployeeFormView_ItemDeleting(object sender, FormViewDeleteEventArgs e)
{
DataKey key = EmployeeFormView.DataKey;
SqlConnection conn = new SqlConnection(connStr);
com.Connection = conn;
com.CommandText = "DELETE FROM Employees WHERE EmployeeID='" + key.Value.ToString() + "'";
conn.Open();
com.ExecuteNonQuery();
conn.Close();
Response.Write( "Record deleted successfully");
bindgrid();
}
protected void EmployeeFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
DataKey key = EmployeeFormView.DataKey;
TextBox txtFirstName = (TextBox)EmployeeFormView.FindControl("txtFirstName2");
TextBox txtLastName = (TextBox)EmployeeFormView.FindControl("txtLastName2");
TextBox txtAddress = (TextBox)EmployeeFormView.FindControl("txtAddress2");
TextBox txtDesignation = (TextBox)EmployeeFormView.FindControl("txtDesignation2");
SqlConnection conn = new SqlConnection(connStr);
com.Connection = conn;
com.CommandText = "UPDATE Employees SET FirstName ='" + txtFirstName.Text + "',LastName ='" + txtLastName.Text + "',Address ='" + txtAddress.Text + "',Designation ='" + txtDesignation.Text + "' WHERE EmployeeID='" + key.Value.ToString() + "'";
conn.Open();
com.ExecuteNonQuery();
Response.Write("Record updated successfully");
bindgrid();
conn.Close();
}
protected void EmployeeFormView_ModeChanging(object sender, FormViewModeEventArgs e)
{
EmployeeFormView.ChangeMode(e.NewMode);
bindgrid();
if (e.NewMode == FormViewMode.Edit)
{
EmployeeFormView.AllowPaging = false;
}
else
{
EmployeeFormView.AllowPaging = true;
}
}
protected void EmployeeFormView_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
EmployeeFormView.ChangeMode(FormViewMode.ReadOnly);
}
protected void EmployeeFormView_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
EmployeeFormView.ChangeMode(FormViewMode.ReadOnly);
}
protected void EmployeeFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
{
TextBox txtEmployeeID = (TextBox)EmployeeFormView.FindControl("txtEmployeeID1");
TextBox txtFirstName = (TextBox)EmployeeFormView.FindControl("txtFirstName1");
TextBox txtLastName = (TextBox)EmployeeFormView.FindControl("txtLastName1");
TextBox txtAddress = (TextBox)EmployeeFormView.FindControl("txtAddress1");
TextBox txtDesignation = (TextBox)EmployeeFormView.FindControl("txtDesignation1");
SqlConnection conn = new SqlConnection(connStr);
com.Connection = conn;
com.CommandText = "INSERT INTO Employees Values('" + txtEmployeeID.Text + "','" + txtFirstName.Text + "', '" + txtLastName.Text + "', '" + txtAddress.Text + "', '" + txtDesignation.Text + "')";
conn.Open();
com.ExecuteNonQuery();
Response.Write("Record inserted successfully");
bindgrid();
conn.Close();
}
}