Cell values are not updated in the update event

All right, I'm stuck!  

When I put a row into edit mode, enter some text, then click Update, the Update event handler should be pulling back that updated text, right? Its not... I get the text from the database and my updates are lost. I'm including the text from my update procedure and the code from my datagrid... can anyone help me out here?
Here is the ASP/HTML that builds the datagrid

<form id="formNewsEdit" runat="server">
<asp:DataGrid id="dgNews" runat="server" CssClass="NewsEditTable" BorderColor="Green" AutoGenerateColumns="False">
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ButtonType="LinkButton" />
<asp:BoundColumn HeaderText="ID" DataField="ID" Visible="False" />
<asp:BoundColumn HeaderText="Subject" DataField="Subject" />
<asp:BoundColumn HeaderText="Body" DataField="Body" />
<asp:BoundColumn HeaderText="Post Date" DataField="DTG" />
<asp:BoundColumn HeaderText="Author ID" DataField="Author" />

Here is the update event handler

private void dgNews_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
string sSql = "";
//System.Data.OleDb.OleDbCommand cmd;
//Need to pull data from 5 controls (1 - 5)
sSql = "UPDATE News SET "
+ "Subject = '" + ((TextBox)e.Item.Cells[2].Controls[0]).Text.Replace("'", "''") + "', "
+ "Body = '" + ((TextBox)e.Item.Cells[3].Controls[0]).Text.Replace("'", "''") + "', "
+ "DTG = '" + ((TextBox)e.Item.Cells[4].Controls[0]).Text.Replace("'", "''") + "', "
+ "Author = " + ((TextBox)e.Item.Cells[5].Controls[0]).Text + " "
+ "WHERE ID = " + ((TextBox)e.Item.Cells[1].Controls[0]).Text;
if (myDBC == null) do_InitDBConn();
if (myDBC.State != System.Data.ConnectionState.Open) do_InitDBConn();
10/22/2003 1:44:15 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

3 Replies

Similar Articles

[PageSpeed] 23


I can't find any code that actually updates the database.
can you please include the page_load code as well as any other code that you are using and feel is related to updating and loading information.
Hey! It compiles! Ship it!


Venugopal Mallarapu.

Visit : My Blog
10/22/2003 2:27:32 AM
I'm sorry... I didn't make it clear, let me explain a bit more.

When I click the Update link on the datagrid it kicks off the dgNews_UpdateCommand event just fine, but when I pull the text out one of the text boxes in the datagrid control it is not the text that I typed in.
1) Click Edit (TextBox1 reads "This is the first news item.")
2) Change TextBox1 to "Welcome to my web site."
3) Click "Update"
4) Get the text from TextBox1 (sTemp = ((TextBox)e.Item.Cells[2].Controls[0]).Text.Replace("'", "''"))
5) At this point sTemp = "This is the first news item." when it should be "Welcome to my web site." Right?
And you're right, I haven't executed the query to update my database in the code I gave, but since the text isn't changing I haven't gotten to that point yet.
10/22/2003 2:39:08 PM
I have figured it out finally... I was rebinding the datagrid in page load (though I know better.. and why I couldn't see it last night I don't know lol), which refreshes the bound textboxes as well... woops!

Thanks anyway!
10/22/2003 3:06:07 PM