Click here to Skip to main content
15,894,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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

ASP.NET
<%@ 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
Posted
Updated 25-Oct-15 3:08am
v3
Comments
Krunal Rohit 25-Oct-15 9:29am    
Did you try using Debugger ?

-KR
ShaHam11 25-Oct-15 9:31am    
Yes, it shows the old value only not the new value what i entered in the gridveiw textbox
Krunal Rohit 25-Oct-15 9:37am    
And the data is getting updated in the database ?

-KR
ShaHam11 25-Oct-15 9:41am    
No, its not getting updated in the front end and database as well
Krunal Rohit 25-Oct-15 11:12am    
Then it's an issue for your update statement.

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900