Datagrid Row Update
-
I have made a function to update data edited in a datagrid row. Here is the code: private void DiagnosisListDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { int DiagnosisNo = (int) DiagnosisListDataGrid.DataKeys[(int) e.Item.ItemIndex]; string nino = ((TextBox)e.Item.Cells[3].Controls[0]).Text; string datetime = ((TextBox)e.Item.Cells[4].Controls[0]).Text; string details = ((TextBox)e.Item.Cells[5].Controls[0]).Text; string currentstatus = ((TextBox)e.Item.Cells[6].Controls[0]).Text; string sql = "UPDATE DIAGNOSIS SET NINo=?, [DateTime]=?, Details=?, currentStatus=? WHERE DiagnosisNo= ?"; DiagnosisListDataGrid.EditItemIndex = -1; OleDbConnection conn = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("GeneralPractice/GeneralPractice.mdb")); OleDbCommand cmd = new OleDbCommand(sql, conn); cmd.Parameters.Add("NINo", nino); cmd.Parameters.Add("DateTime", datetime); cmd.Parameters.Add("Details", details); cmd.Parameters.Add("CurrentStatus", currentstatus); cmd.Parameters.Add("DiagnosisNo",DiagnosisNo); //ReadRecords(); conn.Open(); try { cmd.ExecuteNonQuery(); } finally { DiagnosisListDataGrid.DataBind(); if (conn != null) conn.Close(); } } The problem is that when the update button is cicked the datagrid disappears from the page. If it is loaded up again the info has been updated. Anyone know what am i'm missing out here? Rory
-
I have made a function to update data edited in a datagrid row. Here is the code: private void DiagnosisListDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { int DiagnosisNo = (int) DiagnosisListDataGrid.DataKeys[(int) e.Item.ItemIndex]; string nino = ((TextBox)e.Item.Cells[3].Controls[0]).Text; string datetime = ((TextBox)e.Item.Cells[4].Controls[0]).Text; string details = ((TextBox)e.Item.Cells[5].Controls[0]).Text; string currentstatus = ((TextBox)e.Item.Cells[6].Controls[0]).Text; string sql = "UPDATE DIAGNOSIS SET NINo=?, [DateTime]=?, Details=?, currentStatus=? WHERE DiagnosisNo= ?"; DiagnosisListDataGrid.EditItemIndex = -1; OleDbConnection conn = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("GeneralPractice/GeneralPractice.mdb")); OleDbCommand cmd = new OleDbCommand(sql, conn); cmd.Parameters.Add("NINo", nino); cmd.Parameters.Add("DateTime", datetime); cmd.Parameters.Add("Details", details); cmd.Parameters.Add("CurrentStatus", currentstatus); cmd.Parameters.Add("DiagnosisNo",DiagnosisNo); //ReadRecords(); conn.Open(); try { cmd.ExecuteNonQuery(); } finally { DiagnosisListDataGrid.DataBind(); if (conn != null) conn.Close(); } } The problem is that when the update button is cicked the datagrid disappears from the page. If it is loaded up again the info has been updated. Anyone know what am i'm missing out here? Rory
Have you set the datasource property of the datagrid ? This is something I think is missing. My Blog
-
I have made a function to update data edited in a datagrid row. Here is the code: private void DiagnosisListDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { int DiagnosisNo = (int) DiagnosisListDataGrid.DataKeys[(int) e.Item.ItemIndex]; string nino = ((TextBox)e.Item.Cells[3].Controls[0]).Text; string datetime = ((TextBox)e.Item.Cells[4].Controls[0]).Text; string details = ((TextBox)e.Item.Cells[5].Controls[0]).Text; string currentstatus = ((TextBox)e.Item.Cells[6].Controls[0]).Text; string sql = "UPDATE DIAGNOSIS SET NINo=?, [DateTime]=?, Details=?, currentStatus=? WHERE DiagnosisNo= ?"; DiagnosisListDataGrid.EditItemIndex = -1; OleDbConnection conn = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("GeneralPractice/GeneralPractice.mdb")); OleDbCommand cmd = new OleDbCommand(sql, conn); cmd.Parameters.Add("NINo", nino); cmd.Parameters.Add("DateTime", datetime); cmd.Parameters.Add("Details", details); cmd.Parameters.Add("CurrentStatus", currentstatus); cmd.Parameters.Add("DiagnosisNo",DiagnosisNo); //ReadRecords(); conn.Open(); try { cmd.ExecuteNonQuery(); } finally { DiagnosisListDataGrid.DataBind(); if (conn != null) conn.Close(); } } The problem is that when the update button is cicked the datagrid disappears from the page. If it is loaded up again the info has been updated. Anyone know what am i'm missing out here? Rory
Try binding the data again to the datagrid after updating.
-
Have you set the datasource property of the datagrid ? This is something I think is missing. My Blog
Would you be able to tell me how I can set the datasource property of my datagrid? Thanks
-
Try binding the data again to the datagrid after updating.
When I enter the bind statement it doesn't help the problem?
-
When I enter the bind statement it doesn't help the problem?
Jus check if after updating the data, did u bind the updated data to the datagrid.
-
Jus check if after updating the data, did u bind the updated data to the datagrid.
After update I entered this statement to bind the data: DiagnosisListDataGrid.DataBind();
-
After update I entered this statement to bind the data: DiagnosisListDataGrid.DataBind();
Just before
DiagnosisListDataGrid.DataBind();
addDiagnosisListDataGrid.DataSource = _dataset or datatable_;
My Blog -
Just before
DiagnosisListDataGrid.DataBind();
addDiagnosisListDataGrid.DataSource = _dataset or datatable_;
My BlogThe datagrid still disappears when I run the command. I have a read records method which before I was trying to call, but when I called it it took two attempts two attempt of the update button click for the grid to be updated. Below is that method, if it can be used? private void ReadRecords() { OleDbConnection conn = null; OleDbDataReader reader = null; try { conn = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0; " + @"Data Source=" + Server.MapPath("GeneralPractice/GeneralPractice.mdb")); conn.Open(); OleDbCommand cmd = new OleDbCommand("Select * FROM DIAGNOSIS", conn); reader = cmd.ExecuteReader(); DiagnosisListDataGrid.DataSource = reader; DiagnosisListDataGrid.DataKeyField = "DiagnosisNo"; DiagnosisListDataGrid.DataBind(); } // catch (Exception e) // { // Response.Write(e.Message); // Response.End(); // } finally { if (reader != null) reader.Close(); if (conn != null) conn.Close(); } } Thanks
-
Just before
DiagnosisListDataGrid.DataBind();
addDiagnosisListDataGrid.DataSource = _dataset or datatable_;
My BlogThe datagrid still disappears when I run the command. I have a read records method which before trying the code I gave, I was trying to call in the update method, but when I called it it took two attempts of the update button click for the grid to be updated. Below is that method, if it can be used? private void ReadRecords() { OleDbConnection conn = null; OleDbDataReader reader = null; try { conn = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0; " + @"Data Source=" + Server.MapPath("GeneralPractice/GeneralPractice.mdb")); conn.Open(); OleDbCommand cmd = new OleDbCommand("Select * FROM DIAGNOSIS", conn); reader = cmd.ExecuteReader(); DiagnosisListDataGrid.DataSource = reader; DiagnosisListDataGrid.DataKeyField = "DiagnosisNo"; DiagnosisListDataGrid.DataBind(); } // catch (Exception e) // { // Response.Write(e.Message); // Response.End(); // } finally { if (reader != null) reader.Close(); if (conn != null) conn.Close(); } } Thanks Rory