Table employeeTable contain
EmployeeID int primary key. FirstName varchar(50), LastName varchar(50), Title varchar(50), Country varchar(50)
created a GridView Edit, Delete, and Update functionality. but it donot update when click update. my code:
<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="false"
AlternatingRowStyle-BackColor="#EEEEEE" EditRowStyle-BorderColor="Red"
onrowcancelingedit="GridView1_RowCancelling"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" DataKeyNames="EmployeeID">
<Columns>
<asp:TemplateField Visible="false" HeaderText="EmployeeID">
<ItemTemplate>
<asp:Label runat="server" ID="EmployeeID" Text='<%#Eval("EmployeeID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:Label runat="server" ID="LastName" Text='<%#Eval("LastName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtLastName" Text='<%#Eval("LastName") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfdLastName"
ControlToValidate="txtLastName" ValidationGroup="var1" ErrorMessage="*" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label runat="server" ID="Title" Text='<%#Eval("Title") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtTitle" Text='<%#Eval("Title") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfdTitle"
ControlToValidate="txtTitle" ValidationGroup="var1" ErrorMessage="*" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label runat="server" ID="Country" Text='<%#Eval("country") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCountry" Text='<%#Eval("country") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfdCountry"
ControlToValidate="txtCountry" ValidationGroup="var1" ErrorMessage="*" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
<br />
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace new1
{
public partial class WebForm7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings ["vinkpConnectionString"].ConnectionString);
conn.Open();
SqlCommand comm = new SqlCommand("select EmployeeID,FirstName,LastName,Title,Country from employeeTable ", conn);
comm.ExecuteReader();
conn.Close();
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
Label EmployeeID = GridView1.Rows[e.RowIndex].FindControl("EmployeeID") as Label;
TextBox LastName = GridView1.Rows[e.RowIndex].FindControl("txtLastName") as TextBox;
TextBox Title = GridView1.Rows[e.RowIndex].FindControl("txtTitle") as TextBox;
TextBox Country = GridView1.Rows[e.RowIndex].FindControl("txtCountry") as TextBox;
String UpdateQuery = string.Format("UPDATE employeeTable SET LastName,Title,Country WHERE EmployeeID='"+Convert.ToInt32(EmployeeID.Text)+"'",LastName.Text, Title.Text, Country.Text);
GridView1.EditIndex = -1;
BindGridData(UpdateQuery);
}
private void BindGridData(string Query)
{
string connectionstring = ConfigurationManager.ConnectionStrings["vinkpConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionstring))
{
conn.Open();
using (SqlCommand comm = new SqlCommand(Query +";select EmployeeID,FirstName,LastName,Title,Country from employeeTable ", conn))
{
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
protected void GridView1_RowCancelling(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string EmployeeID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string Query = "delete employeeTable where EmployeeID = " + EmployeeID;
BindGridData(Query);
}
private void BindGridData()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["vinkpConnectionString"].ConnectionString))
{
conn.Open();
using (SqlCommand comm = new SqlCommand("select EmployeeID,FirstName,LastName,Title,Country from employeeTable ", conn))
{
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
why error occur?any mistake in code?when click edit textboxes appeared.but click update no updation show in Gridview.