rino raju

rino raju

  • NA
  • 2
  • 0

Cannot update Datagrid does not reflect changes to database

Jul 16 2011 7:31 PM

Please hlep as i cannot update the datagid in asp.net below is the code ,m not geeting where m a goin wrong.

After editing the Datagrid when i click on update the database is not updated.There is no error message.

 

Please help 

 

>>>ASP.net Code:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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>

 

 

    </div>

 

 

<table>

 <tr>

 <td> 

 <asp:Label ID="id" runat="server" Text="ID"></asp:Label>

 <asp:TextBox ID="t1"  runat="server" ></asp:TextBox>

 </td>

 

 <td> 

 <asp:Label ID="nm" runat="server" Text="Name"></asp:Label>

 <asp:TextBox ID="t2"  runat="server" ></asp:TextBox>

 </td>

 

 <td> 

 <asp:Label ID="lnm" runat="server" Text="Last Name"></asp:Label>

 <asp:TextBox ID="t3"  runat="server" ></asp:TextBox>

 </td>

 

 

 </tr>

 

 

  </table>

 

 

    <asp:DataGrid ID="Dg1" runat="server" oncancelcommand="Dg1_CancelCommand" 

        oneditcommand="Dg1_EditCommand" onupdatecommand="Dg1_UpdateCommand">

 

 

  <Columns>

 

  <asp:BoundColumn HeaderText="ID" DataField="id">

  </asp:BoundColumn>

  <asp:BoundColumn HeaderText="NAME" DataField="name">

  </asp:BoundColumn>

  <asp:BoundColumn HeaderText="LAST NAME" DataField="last_name">

  </asp:BoundColumn>

 

  <asp:EditCommandColumn EditText="EDIT" CancelText="CANCEL" UpdateText="UPDATE" HeaderText="EDIT">

  </asp:EditCommandColumn>

 

  <asp:ButtonColumn CommandName="delete" HeaderText="DELETE" Text="DELETE">

  </asp:ButtonColumn>

 

  </Columns>

  </asp:DataGrid>

 

 

    </form>

</body>

</html>

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.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con1;
    SqlDataAdapter da;
    DataSet ds;
    SqlCommand com;
    DataTable dt;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        string str= ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        con1 = new SqlConnection(str);
        com = new SqlCommand("select * from details", con1);
        ds = new DataSet();
        da = new SqlDataAdapter(com);
    
        da.Fill(ds, "details");
        dt = ds.Tables["details"];
        con1.Open();
        com.ExecuteNonQuery();
        Dg1.DataSource = dt;
        Dg1.DataBind();
        con1.Close();
    }
    
    
    static int num;
    protected void Dg1_EditCommand(object source, DataGridCommandEventArgs e)
    {
        num = Convert.ToInt16(e.Item.Cells[0].Text.ToString());
        Dg1.EditItemIndex = e.Item.ItemIndex;
        databind();
    }
    protected void Dg1_CancelCommand(object source, DataGridCommandEventArgs e)
    {
        Dg1.EditItemIndex = -1;
        databind();
    }
    public void databind()
    {
        string str = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        con1 = new SqlConnection(str);
        com = new SqlCommand("select * from details", con1);
        ds = new DataSet();
        da = new SqlDataAdapter(com);
    
        da.Fill(ds, "details");
        
        dt = ds.Tables["details"];
    
        con1.Open();
        com.ExecuteNonQuery();
        Dg1.DataSource = dt;
        Dg1.DataBind();
        con1.Close();
    }
    protected void Dg1_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
        string str = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        con1 = new SqlConnection(str);
        com.Parameters.Add("@EmpId", SqlDbType.Int).Value = ((TextBox)e.Item.Cells[0].Controls[0]).Text;
        com.Parameters.Add("@F_Name", SqlDbType.VarChar).Value = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
        com.Parameters.Add("@L_Name", SqlDbType.VarChar).Value = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
        //TextBox t1, t2, t3;
        //t1 = (TextBox)e.Item.Cells[0].Controls[0];
        //t2 = (TextBox)e.Item.Cells[1].Controls[0];
        //t3 = (TextBox)e.Item.Cells[2].Controls[0];
        com.CommandText = "update details set id=@EmpId,name=@F_Name,last_name=@L_Name where id=@EmpId";
        con1.Open();
        com.Connection = con1;
        
        com.ExecuteNonQuery();
        com.Connection.Close();
        Dg1.EditItemIndex=-1;
        databind();
    }
}
Thankyou,
Rino