Misha Posted November 30, 2004 Posted November 30, 2004 Hi! I have the folloving strange problem with editing data in the DataGrid. I can pull data from the database, show them and put in to the edit mode. Let's say I have to change some record from "green" to "red". When I change value in the text box and click update, data is updated in the database, and it has new "red" value, but on the screen I still see the old value "green". If I click on update again, the newly submited value "red" appears in the text box. Please, take a look at the code below. Thank you in advance! void Page_Load(Object sender, EventArgs e) { SqlConnection myConnection = new SqlConnection("server=**;user=**;password=**;database=**;"); SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * from Interface WHERE ID= " + recID, myConnection); DataSet EditDS = new DataSet(); myCommand.Fill(EditDS, "RoomEdit"); EditDW = new DataView(); EditDW = EditDS.Tables["RoomEdit"].DefaultView; EditGrid.DataSource = EditDW; if (!Page.IsPostBack) { EditGrid.DataBind(); } } void BindGrid() { EditGrid.DataSource = EditDW; EditGrid.DataBind(); } void EditGrid_Update(Object sender, DataGridCommandEventArgs e) { TextBox RoomUseDescTextB = (TextBox)e.Item.Cells[9].Controls[0]; TextBox RoomUseCdTextB = (TextBox)e.Item.Cells[10].Controls[0]; String newRoomUseCd = RoomUseCdTextB.Text; String newRoomUseDesc = RoomUseDescTextB.Text; SqlConnection connUpdate = new SqlConnection("server=**;user=**;password=**;database=**;"); connUpdate.Open(); String sql_edit = "UPDATE Interface " + "SET RoomUseCode = '" + newRoomUseCd + "', " + "RoomUseDesc = '" + newRoomUseDesc + "'" + " WHERE ID = " + e.Item.Cells[0].Text; SqlCommand sqlCommandUpdate = new SqlCommand(sql_edit,connUpdate); sqlCommandUpdate.ExecuteNonQuery(); connUpdate.Close(); EditGrid.EditItemIndex = -1; BindGrid(); } Quote
Cassio Posted December 4, 2004 Posted December 4, 2004 The code on the page_load runs before the update function, so when you call the BindGrid() function the datagrid source has already been defined in the page_load. You should put the data access code in the BindGrid() function instead, and call this function from the page_load too. Quote Stream of Consciousness (My blog)
Misha Posted December 6, 2004 Author Posted December 6, 2004 Thank you! I changed the code and it works fine now. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.