Editable DataGrid Shows Old Values "After" Update

  • Thread starter Thread starter Kevin Pedersen
  • Start date Start date
K

Kevin Pedersen

Hello, I am using an editable datagrid. After I save the changes the
datagrid shows the old values.

I've read the posts about the Page_Load and not binding the datagrid
each time. The SQL that is being sent to the database is correct and
the changes are eventually being made. If I refresh the page after the
update then the new values appear.

I noticed that when I put a breakpoint in my update handler everything
works fine. When I take out the breakpoint, I get the old values. It's
like the update isn't happening fast enough. I'm updating then
selecting before the update is finished. Or am I way off base and I'm
doing something else wrong?

Help.

Thanks in advance,
Kevin
 
Kevin said:
Hello, I am using an editable datagrid. After I save the changes the
datagrid shows the old values.

Once you've acquired the row to be updated, follow these steps:

1) Call DatRow.BeginEdit()
2) Change the values in the DataRow
3) Call DataRow.EndEdit()
4) Connect to the database
5) Call DataAdapter.Update(DataTable)
6) Call DataTable.AcceptChanges()
7) Rebind your DataGrid

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Here's the code. At least the stuff I thought was relevant.

<asp:datagrid id="details" runat="server"
visible="False"
datakeyfield="entity_id"
autogeneratecolumns="False"
headerstyle-font-bold="True"
font-size="10pt"
font-name="Verdana"
oneditcommand="onEdit"
oncancelcommand="onCancel"
onupdatecommand="onUpdate"<columns>
<asp:boundcolumn headertext="ID" datafield="entity_id"
readonly="True" />

<asp:templatecolumn headertext="Name">
<itemtemplate>
<asp:label runat="server" id="lblName"
text='<%# DataBinder.Eval( Container.DataItem, "entity_name"
) %>' />
</itemtemplate>

<edititemtemplate>
<asp:textbox id="txtName" runat="server" font-name="Verdana"
text='<%# DataBinder.Eval( Container.DataItem, "entity_name"
) %>' />
</edititemtemplate>
</asp:templatecolumn>

<asp:templatecolumn headertext="Number">
<itemtemplate>
<asp:label id="lblNumber" runat="server"
text='<%# DataBinder.Eval( Container.DataItem,
"street_number" ) %>' />
</itemtemplate>

<edititemtemplate>
<asp:dropdownlist id="ddlNumber" runat="server"
datasource='<%# getStreetNumbers() %>'
datatextfield="street_number"
datavaluefield="id"
onprerender="setItemIndex" />
</edititemtemplate>
</asp:templatecolumn>

<asp:templatecolumn runat="server" headertext="Street">
<itemtemplate>
<asp:label runat="server" text='<%#
DataBinder.Eval( Container.DataItem, "street_name" ) + " "
+
DataBinder.Eval( Container.DataItem, "street_type" ) %>' />
</itemtemplate>
</asp:templatecolumn>

<asp:templatecolumn runat="server" headertext="Phone 1">
<itemtemplate>
<asp:label id="lblPhone1" runat="server"
text='<%# DataBinder.Eval( Container.DataItem, "phone1" )
%>' />
</itemtemplate>

<edititemtemplate>
<asp:textbox id="txtPhone1" runat="server"
text='<%# DataBinder.Eval( Container.DataItem, "phone1" )
%>'
width="80" />
</edititemtemplate>
</asp:templatecolumn>

<asp:templatecolumn runat="server" headertext="Phone 2">
<itemtemplate>
<asp:label id="lblPhone2" runat="server"
text='<%# DataBinder.Eval( Container.DataItem, "phone2" )
%>' />
</itemtemplate>

<edititemtemplate>
<asp:textbox id="txtPhone2" runat="server"
text='<%# DataBinder.Eval( Container.DataItem, "phone2" )
%>'
width="80" />
</edititemtemplate>
</asp:templatecolumn>

<asp:templatecolumn runat="server" headertext="Type">
<itemtemplate>
<asp:label id="lblType" runat="server"
text='<%# DataBinder.Eval( Container.DataItem, "type" ) %>'
/>
</itemtemplate>

<edititemtemplate>
<asp:dropdownlist id="ddlType" runat="server"
datasource='<%# getTypes() %>'
datatextfield="type"
datavaluefield="id"
onprerender="setItemIndex"
/>
</edititemtemplate>
</asp:templatecolumn>

<asp:templatecolumn runat="server" headertext="Sub Type 1">
<itemtemplate>
<asp:label id="lblSubType1" runat="server"
text='<%# DataBinder.Eval( Container.DataItem, "subtype1" )
%>' />
</itemtemplate>

<edititemtemplate>
<asp:dropdownlist id="ddlSubType1" runat="server"
datasource='<%# getSubTypes() %>'
datatextfield="type"
datavaluefield="id"
onprerender="setItemIndex"
/>
</edititemtemplate>
</asp:templatecolumn>

<asp:templatecolumn runat="server" headertext="Sub Type 2">
<itemtemplate>
<asp:label id="lblSubType2" runat="server"
text='<%# DataBinder.Eval( Container.DataItem, "subtype2" )
%>' />
</itemtemplate>

<edititemtemplate>
<asp:dropdownlist id="ddlSubType2" runat="server"
datasource='<%# getSubTypes() %>'
datatextfield="type"
datavaluefield="id"
onprerender="setItemIndex"
/>
</edititemtemplate>
</asp:templatecolumn>

<asp:editcommandcolumn
edittext="Edit"
canceltext="Cancel"
updatetext="Save" />

<asp:buttoncolumn
buttontype="LinkButton"
commandname="Delete"
text="Delete" />
</columns>
</asp:datagrid>

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if( !IsPostBack )
{
streetId = "1";
bindGrid();
}
else {
// Get the current selection from the list box
streetId = streetList.selectedItemValue == "" ? "1" :
streetList.selectedItemValue;

// Show results
details.Visible = true;
street_image.Visible = true;
}
}

private void bindGrid()
{
string connectString =
System.Configuration.ConfigurationSettings.AppSettings[
"connectString" ];

// Create connection and set connection string
OleDbConnection cnn = new OleDbConnection( connectString );

string sql = "SELECT entity.id AS entity_id, entity.name AS
entity_name, " +
"block_address.id AS block_address_id,
block_address.street_number, street.name AS street_name, " +
"street.type AS street_type, entity.phone1, entity.phone2, "
+
"(SELECT type FROM entity_type WHERE id = entity.type ) AS
type, " +
"(SELECT type FROM entity_sub_type WHERE id =
entity.sub_type1 ) AS subtype1, " +
"(SELECT type FROM entity_sub_type WHERE id =
entity.sub_type2 ) AS subtype2, " +
"block_address.block_id " +
"FROM entity INNER JOIN " +
"( block_address INNER JOIN street " +
"ON block_address.street_id = street.id) " +
"ON entity.block_address = block_address.id " +
"WHERE street.id = " + streetId;

OleDbCommand cmd = new OleDbCommand( sql, cnn );
cmd.CommandType = CommandType.Text;
cnn.Open();

details.DataSource = cmd.ExecuteReader();
details.DataBind();
cnn.Close();
}

public void onUpdate( Object source, DataGridCommandEventArgs e )
{
string name = ((TextBox)e.Item.Cells[1].Controls[1]).Text;
string addressId = ((DropDownList)(e.Item.FindControl( "ddlNumber"
))).SelectedItem.Value;
string phone1 = ((TextBox)e.Item.Cells[4].Controls[1]).Text;
string phone2 = ((TextBox)e.Item.Cells[5].Controls[1]).Text;
string type = ((DropDownList)(e.Item.FindControl( "ddlType"
))).SelectedItem.Value;
string subType1 = ((DropDownList)(e.Item.FindControl( "ddlSubType1"
))).SelectedItem.Value;
string subType2 = ((DropDownList)(e.Item.FindControl( "ddlSubType2"
))).SelectedItem.Value;

int entityId = e.Item.ItemIndex;
string sql = "UPDATE entity SET " +
"name = '" + name + "', " +
"block_address = " + addressId + ", " +
"phone1 = '" + phone1 + "', " +
"phone2 = '" + phone2 + "', " +
"type = " + type + ", " +
"sub_type1 = " + subType1 + ", " +
"sub_type2 = " + subType2 + " " +
"WHERE id = " + details.DataKeys[entityId];

string connectString =
System.Configuration.ConfigurationSettings.AppSettings[
"connectString" ];

// Create connection and set connection string
OleDbConnection cnn = new OleDbConnection( connectString );

OleDbCommand cmd = new OleDbCommand( sql, cnn );
cnn.Open();
cmd.ExecuteNonQuery();

details.EditItemIndex = -1;
bindGrid();
}

Thanks,
Kevin
 
Back
Top