Hi rajeesh,
You have create onItemUpdating in code behinde but not in detailView so refer below sample i have created onItemUpdating event both places.
Refer below sample.
HTML
<div>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="280px" AutoGenerateRows="False"
OnItemUpdating="DetailsView1_ItemUpdating" DefaultMode="Edit">
<Fields>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label Text='<%# Eval("CustomerId") %>' runat="server" ID="lblId"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<br />
<asp:Label ID="Label1" runat="server" />
</div>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindData();
}
}
private void BindData()
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId,Name,Country FROM CustomerTest", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
DetailsView1.DataSource = dt;
DetailsView1.DataBind();
}
}
}
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
Label id = (Label)DetailsView1.FindControl("lblId");
TextBox name = (TextBox)DetailsView1.FindControl("txtName");
TextBox country = (TextBox)DetailsView1.FindControl("txtCountry");
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE CustomerTest SET Name = @Name, Country = @Country WHERE CustomerId = @Id", con))
{
cmd.Parameters.AddWithValue("@Name", name.Text);
cmd.Parameters.AddWithValue("@Country", country.Text);
cmd.Parameters.AddWithValue("@Id", id.Text);
con.Open();
int a = Convert.ToInt16(cmd.ExecuteNonQuery());
if (a != 0)
{
Label1.Text = "Updated Successfully";
}
}
}
BindData();
}
VB.Net
Private constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindData()
End If
End Sub
Private Sub BindData()
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId,Name,Country FROM CustomerTest", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
DetailsView1.DataSource = dt
DetailsView1.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As DetailsViewUpdateEventArgs)
Dim id As Label = CType(DetailsView1.FindControl("lblId"), Label)
Dim name As TextBox = CType(DetailsView1.FindControl("txtName"), TextBox)
Dim country As TextBox = CType(DetailsView1.FindControl("txtCountry"), TextBox)
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("UPDATE CustomerTest SET Name = @Name, Country = @Country WHERE CustomerId = @Id", con)
cmd.Parameters.AddWithValue("@Name", name.Text)
cmd.Parameters.AddWithValue("@Country", country.Text)
cmd.Parameters.AddWithValue("@Id", id.Text)
con.Open()
Dim a As Integer = Convert.ToInt16(cmd.ExecuteNonQuery())
If a <> 0 Then
Label1.Text = "Updated Successfully"
End If
End Using
End Using
BindData()
End Sub
Screenshot