nandan gopalan

nandan gopalan

  • NA
  • 2
  • 3.2k

Update codes for DataGrid in ASP.NET?

Apr 4 2011 9:08 AM
Hello sir/madam,

  I am a final year student and have to finish my project
  I am getting error when i run my project.
Kindly give me the solution.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;

namespace CRIME_STOPPER
{
  public partial class AdminPage1 : System.Web.UI.Page
  {
  SqlConnection con = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=crimestopper;Integrated Security=True");
  SqlDataAdapter ad;
  DataSet ds = new DataSet();
  static DataTable dt = new DataTable();

  protected void Page_Load(object sender, EventArgs e)
  {
  if(IsPostBack==false)
  {
  ad=new SqlDataAdapter("select * from postinfo",con);
  ad.Fill(ds,"s");
  dt=ds.Tables["s"];
  DataGrid1.DataSource=dt;
  DataGrid1.DataBind();
  }
  }

 
  protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
  {
 
  DataGrid1.EditItemIndex = e.Item.ItemIndex;
  DataGrid1.DataSource = dt;
  DataGrid1.DataBind();


  }
 

  protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
  {
  DataGrid1.EditItemIndex = -1;
  DataGrid1.DataSource = dt;
  DataGrid1.DataBind();

  }

  protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
  {
  TextBox t1, t2, t3, t4, t5;
  t1 = (TextBox)e.Item.Cells[1].Controls[0];
  t2 = (TextBox)e.Item.Cells[2].Controls[0];
  t3 = (TextBox)e.Item.Cells[3].Controls[0];
  t4 = (TextBox)e.Item.Cells[4].Controls[0];
  t5 = (TextBox)e.Item.Cells[5].Controls[0];
 
  SqlCommand cmd=new SqlCommand("update postinfo set Gender="+t1.Text+",State='"+t2.Text+"',City='"+t3.Text+"',Locality='"+t4.Text+"',Info='"+t5.Text+"' where Gender="+t1.Text,con);
  con.Open();
  cmd.ExecuteNonQuery();
  ad=new SqlDataAdapter("select * from postinfo",con);
  ad.Fill(ds,"s");
  dt=ds.Tables["s"];
  DataGrid1.EditItemIndex = -1;
  DataGrid1.DataSource=dt;
  DataGrid1.DataBind();
  }
  }

}

Answers (1)