Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear code project members i need your kind attention towards one on the problem I am facing:-

i need to display a database table records in gridview and update a choosen record dynamically. when i edit the record and press update nothing is happening.

the complate code of the page is:

SQL
CREATE TABLE [dbo].[tbeng](
    [engid] [int] IDENTITY(1,1) NOT NULL,
    [engnam] [varchar](50) NULL,
    [engeml] [varchar](50) NULL,
)


XML
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

            SqlConnection con;
            SqlCommand com ;
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable taskTable = new DataTable("TaskList");

    protected void Page_Load(object sender, EventArgs e)
    {
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);

            com = new SqlCommand("SELECT [engid],[engnam],[engeml] FROM [dbCMS].[dbo].[tbeng]", con);
            com.Connection = con;
            da.SelectCommand = com;

            var builder = new SqlCommandBuilder(da);
            da.UpdateCommand = builder.GetUpdateCommand(true);

            da.Fill(taskTable);

            Session["TaskTable"] = taskTable;

            BindData();
    }

    protected void TaskGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        TaskGridView.PageIndex = e.NewPageIndex;
        BindData();
    }

    protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        TaskGridView.EditIndex = e.NewEditIndex;
        BindData();
    }

    protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        TaskGridView.EditIndex = -1;
        BindData();
    }

    protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DataTable dt = (DataTable)Session["TaskTable"];

        GridViewRow row = TaskGridView.Rows[e.RowIndex];

        dt.Rows[row.DataItemIndex]["engid"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
        dt.Rows[row.DataItemIndex]["engnam"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
        dt.Rows[row.DataItemIndex]["engeml"] = ((TextBox)(row.Cells[3].Controls[0])).Text;

        da.Update(dt);

        TaskGridView.EditIndex = -1;

        BindData();
    }

    private void BindData()
    {
        TaskGridView.DataSource = Session["TaskTable"];
        TaskGridView.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
     <div>

      <asp:GridView ID="TaskGridView" runat="server"

        AutoGenerateEditButton="True"
        AllowPaging="true"
        OnRowEditing="TaskGridView_RowEditing"
        OnRowCancelingEdit="TaskGridView_RowCancelingEdit"
        OnRowUpdating="TaskGridView_RowUpdating"
        OnPageIndexChanging="TaskGridView_PageIndexChanging" >
      </asp:GridView>

    </div>

    </form>
</body>
</html>
Posted

I think data is updating but you are not taking updated data. You are binding grid from session. This session should be updated or select part from Page_Load should be moved to BindData method.
 
Share this answer
 
Hi,

C#
private void BindData()
    {
        TaskGridView.DataSource = Session["TaskTable"];
        TaskGridView.DataBind();
    }


Instead of above code write the code below.

C#
private void BindData()
    {
        TaskGridView.DataSource =(DataTable) Session["TaskTable"];
        TaskGridView.DataBind();
    }


Regards,
Raghu
 
Share this answer
 

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