Hi , I have written simple grid view(I have not used boundfeild or TemplateFied). I have written a Rowupdating function the row is not getting updated but I get a success message row updated successfully. The fact is row remains with old value itself its not getting updated.
Code Behind 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.Sql;
using System.Data.SqlClient;
using System.Data;
namespace GridView
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
SqlConnection con = new SqlConnection("server=.;database=exampledb;integrated security=true");
SqlDataAdapter da = new SqlDataAdapter("Select * from employeeinfo", con);
DataSet ds = new DataSet();
da.Fill(ds);
gvEmpdata.DataSource = ds;
gvEmpdata.DataBind();
}
protected void gvEmpdata_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvEmpdata.EditIndex = -1;
BindData();
}
protected void gvEmpdata_RowEditing(object sender, GridViewEditEventArgs e)
{
gvEmpdata.EditIndex = e.NewEditIndex;
}
protected void gvEmpdata_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvEmpdata.PageIndex = e.NewPageIndex;
}
protected void gvEmpdata_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int rowIndex = e.RowIndex;
GridViewRow gvrows = gvEmpdata.Rows[rowIndex];
int empId = int.Parse((gvrows.Cells[1].Controls[0] as TextBox).Text);
string empName = (gvrows.Cells[2].Controls[0] as TextBox).Text;
int salary = int.Parse((gvrows.Cells[3].Controls[0] as TextBox).Text);
string location = (gvrows.Cells[4].Controls[0] as TextBox).Text;
SqlConnection con = new SqlConnection("server=.;database=exampledb;integrated security=true");
string updatequery = "update employeeinfo set EmpName=@empName,Salary=@salary,Location=@location where Empid=@empId";
SqlCommand cmd = new SqlCommand(updatequery, con);
cmd.Parameters.Add(new SqlParameter("@empId", empId));
cmd.Parameters.Add(new SqlParameter("@empName", empName));
cmd.Parameters.Add(new SqlParameter("@salary", salary));
cmd.Parameters.Add(new SqlParameter("@location", location));
con.Open();
int noOfRows = cmd.ExecuteNonQuery();
if (noOfRows > 0)
{
Label1.Text = "Upadated Succefully";
gvEmpdata.EditIndex = -1;
BindData();
}
else
{
Label1.Text = "Something went wrong!";
}
}
}
}
Design Page Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridView._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvEmpdata" runat="server" AllowPaging="True"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" EnableModelValidation="True"
onpageindexchanging="gvEmpdata_PageIndexChanging"
onrowcancelingedit="gvEmpdata_RowCancelingEdit"
onrowediting="gvEmpdata_RowEditing" onrowupdating="gvEmpdata_RowUpdating"
PageSize="4">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerSettings Mode="NextPrevious" PageButtonCount="1" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Sql Table Structure( Table Names as follows)
EmpID EmpName Salary Location