I have a GridView with several TextBox TemplateFields. When a value is entered into one of them, I want to postback, update one of the SQL Tables that it's pulling data from, then pull that new value into the GridView after the postback. In the markup I have:
<asp:BoundField DataField = "AtmId" HeaderText ="Atm Id"/>
<asp:TemplateField HeaderText ="Cassette 2 $">
<ItemTemplate>
<asp:TextBox ID="Cassette2" OnTextChanged="Cassette2_TextChanged" runat="server" Text="" AutoPostBack ="true"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
Then in the code behind when text is written
protected void Page_Load(object sender, EventArgs e)
{
RoutesTableAdapter adapter = new RoutesTableAdapter();
DataTable dt = adapter.GetDataBy(148);
System.Diagnostics.Debug.WriteLine(adapter.GetDataBy(148).Rows[0]["Cassette 2 $"]);
if (!IsPostBack)
{
GridView1.DataSource = dt;
GridView1.DataBind();
for (int j = 0; j < GridView1.Rows.Count; j++) {
GridViewRow row = GridView1.Rows[j];
TextBox Cass2 = row.FindControl("Cassette2") as TextBox;
Cass2.Text = dt.Rows[j]["Cassette 2 $"].ToString();
Cass2.ReadOnly = false;
} }
protected void Cassette2_TextChanged(object sender, EventArgs e)
{
try
{
TextBox txt = sender as TextBox;
GridViewRow row = txt.NamingContainer as GridViewRow;
RoutesTableAdapter adapter = new RoutesTableAdapter();
int AtmId = int.Parse(row.Cells[0].Text);
adapter.UpdateCass2(int.Parse(txt.Text), AtmId);
}catch(Exception exc)
{
txt.Text = "0";
}
}
In Page_Load, regardless of if it's on postback or not, new RoutesTableAdapter().GetData()
will give a table with values that don't contain the changes made. I can print out a line and view that same line in SSMS, and they don't match until the page is refreshed. I thought this might be intended behavior, but everywhere I've ready online makes it seem like it's not supposed to need a refresh to update. Most other questions with this problem that I've seen had to do with the DataSet TableAdapters being pointed to a different database, but there's only one DB on the server with this data, and once the page is refreshed they agree. For additional context, the SQL statements are:
CREATE PROCEDURE [dbo].UpdateCass2
(
@Cass2 int,
@AtmId int
)
AS
SET NOCOUNT OFF;
UPDATE ForecastRoute SET Cass2 = @Cass2 WHERE AtmId = @AtmId
And GetDataBy() is:
SELECT AtmId, Cass2 AS [Cassette 2 $] FROM ForecastRoute WHERE AtmId = @AtmId
I'm able to see immediately any changes in Cass2 after postback in SSMS, using the exact query for GetDataBy(), but the debug output still writes the old value. I thought maybe DataSet did something that I don't understand, like making an intermediate table with the schema defined in the SELECT statement, but that wouldn't explain why changes are reflected in SSMS immediately, right?
Edit: minimum reproducible example including page_load. There are other columns in the table and other tables that it's pulling from, but for just a ForecastRoute table with two columns and a row where AtmId = 148 I'd expect to be able to see the changes immediately, but I'm still only able to see it in SSMS.