rra

rra

  • NA
  • 2
  • 0

DataGrid Update command

Oct 6 2003 2:38 PM
I am trying to develop an update command for a DataGrid. The update statement is on a CDd.css and the DataGrid is on AddEvent.aspx.css. When I call the update event nothing happens. Could you please help me find out what’s wrong with my code. Thank you. AddEvent.aspx.css code: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace calendario { /// /// Summary description for WebForm2. /// public class AddEvent : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DataGrid1; protected System.Web.UI.WebControls.Button btnNew; protected System.Web.UI.WebControls.Button btnBack; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.TextBox txtDate; protected System.Web.UI.WebControls.TextBox txtName; protected System.Web.UI.WebControls.TextBox txtSubject; protected System.Web.UI.WebControls.TextBox txtDescription; //Database connection, update, delete, add CDb db = new CDb(); #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand); this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand); this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand); this.btnBack.Click += new System.EventHandler(this.btnBack_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Page_Load(object sender, System.EventArgs e) { DataSet ds = new DataSet(); ds = db.Myds(); DataGrid1.DataSource = ds; DataGrid1.DataBind(); } private void btnBack_Click(object sender, System.EventArgs e) { Response.BufferOutput = true; Response.Redirect ( "Calendar.aspx" ); } private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { DataGrid1.EditItemIndex = -1; DataGrid1.DataBind(); } private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { DataGrid1.EditItemIndex = e.Item.ItemIndex; DataGrid1.DataBind(); } //update command private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { // Gets get the value of the controls (textboxes) that the user // updated. The DataGrid columns are exposed as the Cells collection. // Each cell has a collection of controls. TextBox tb; // Gets the value the TextBox control in the 2 column tb = (TextBox)(e.Item.Cells[2].Controls[0]); //pass the changed value to the CDb class (public properties db.Id = tb.Text; // Gets the value the TextBox control in the 3 column tb = (TextBox)(e.Item.Cells[3].Controls[0]); db.EventDate = tb.Text; // Gets the value the TextBox control in the 4 column tb = (TextBox)(e.Item.Cells[4].Controls[0]); db.EventName = tb.Text; // Gets the value the TextBox control in the 5 column tb = (TextBox)(e.Item.Cells[5].Controls[0]); db.EventSubject = tb.Text; // Gets the value the TextBox control in the 6 column tb = (TextBox)(e.Item.Cells[6].Controls[0]); db.EventDescription = tb.Text; // Calls a SQL statement to update the database from the dataset db.Update(); // Takes the DataGrid row out of editing mode DataGrid1.EditItemIndex = -1; // Refreshes the grid DataGrid1.DataBind(); } } } CDb.css Code: using System; using System.Collections; using System.Data; using System.Web; using System.Configuration; using System.Data.SqlClient; using System.Text; namespace calendario { public class CDb { private SqlDataAdapter adptr = new SqlDataAdapter(); private SqlCommandBuilder cmdBldr; public CDb() { } // Private Variables ----------------------------------------------- private string m_Id; private string m_EventDate; private string m_EventName; private string m_EventSubject; private string m_EventDescription; // Public Properties ----------------------------------------------- public string Id { get {return m_Id; } set {m_Id=value; } } public string EventDate { get {return m_EventDate; } set {m_EventDate=value; } } public string EventName { get {return m_EventName; } set {m_EventName=value; } } public string EventSubject { get {return m_EventSubject; } set {m_EventSubject=value; } } public string EventDescription { get {return m_EventDescription; } set {m_EventDescription=value; } } public static string DsnEvents { get { string m_dbConnection = ConfigurationSettings.AppSettings["EVENTS"]; if (m_dbConnection==null) throw new Exception("DsnEvents not set in Config.web"); return m_dbConnection; } } public DataSet Getds(string ahora) { string strAhora = ahora; DataSet ds = new DataSet(); string sql = "SELECT * FROM aEvents WHERE EventDate='" +strAhora+"'"; SqlConnection m_SqlConnection = new SqlConnection(CDb.DsnEvents); try { if (m_SqlConnection.State == ConnectionState.Closed) { m_SqlConnection.Open(); } cmdBldr = new SqlCommandBuilder(adptr); adptr.SelectCommand = new SqlCommand(sql, m_SqlConnection); adptr.Fill(ds, "aEvents"); m_SqlConnection.Close(); } catch (Exception e) { throw e; } m_SqlConnection.Close(); return ds; } public DataSet Myds() { DataSet ds2 = new DataSet(); string sql = "SELECT * FROM aEvents"; SqlConnection m_SqlConnection = new SqlConnection(CDb.DsnEvents); try { if (m_SqlConnection.State == ConnectionState.Closed) { m_SqlConnection.Open(); } cmdBldr = new SqlCommandBuilder(adptr); adptr.SelectCommand = new SqlCommand(sql, m_SqlConnection); adptr.Fill(ds2, "aEvents"); m_SqlConnection.Close(); } catch (Exception e) { throw e; } m_SqlConnection.Close(); return ds2; } public void Update() // Update { string updateCmd = "UPDATE aEvents SET EventDate=@m_EventDate, EventName=@m_EventName, EventSubject=@m_EventSubject, EventDescription=@m_EventDescription WHERE Id= @m_Id"; SqlConnection m_SqlConnection = new SqlConnection(CDb.DsnEvents); SqlCommand m_SqlCommand = new SqlCommand(updateCmd, m_SqlConnection); m_SqlCommand.Parameters.Add(new SqlParameter("@m_Id", SqlDbType.Int)); m_SqlCommand.Parameters["@m_Id"].Value = m_Id; m_SqlCommand.Parameters.Add(new SqlParameter("@m_EventDate", SqlDbType.NChar)); m_SqlCommand.Parameters["@m_EventDate"].Value = m_EventDate; m_SqlCommand.Parameters.Add(new SqlParameter("@m_EventName", SqlDbType.Char,15)); m_SqlCommand.Parameters["@m_EventName"].Value = m_EventName; m_SqlCommand.Parameters.Add(new SqlParameter("@m_EventSubject", SqlDbType.Char,15)); m_SqlCommand.Parameters["@m_EventSubject"].Value = m_EventSubject; m_SqlCommand.Parameters.Add(new SqlParameter("@m_EventDescription", SqlDbType.NText)); m_SqlCommand.Parameters["@m_EventDescription"].Value = m_EventDescription; try { m_SqlCommand.Connection.Open(); m_SqlCommand.ExecuteNonQuery(); } catch (Exception e) { throw new Exception("Error in CDb:Update()-> " + e.ToString()); } finally { m_SqlConnection.Close(); m_SqlCommand.Dispose(); } } // end Update } }