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" %>
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
Replies
Know the answer? Post it — somebody with the same question will find it here.