473,385 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Editable DataGrid Shows Old Values "After" Update

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
Nov 18 '05 #1
3 4911
Do you rebind the grid *after* update has been done? Post some code.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
"Kevin Pedersen" <ke************@hotmail.com> wrote in message
news:20************************@posting.google.com ...
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

Nov 18 '05 #2
Kevin Pedersen wrote:
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)
Nov 18 '05 #3
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.AppSett ings[
"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.AppSett ings[
"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

"Teemu Keiski" <jo****@aspalliance.com> wrote in message news:<#i**************@TK2MSFTNGP11.phx.gbl>... Do you rebind the grid *after* update has been done? Post some code.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
"Kevin Pedersen" <ke************@hotmail.com> wrote in message
news:20************************@posting.google.com ...
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

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kashif Mehmood | last post by:
Greeting I am using the following code to delete a row from a datagrid ---------------- Private Sub dgClientTypes_DeleteCommand(ByVal source As Object, ByVal e As...
2
by: Jim Bancroft | last post by:
This may be a no-brainer, but I'm sure I follow what's happening here... I have a DataGrid with one DropDownList per row. If I select a few DropDown items and postback my page, I can't loop...
0
by: Luis Esteban Valencia | last post by:
I have a datagrid with no editable fields except checkboxes at the beginning of each row. Is it possible to make certain columns in a row editable if a user selects the checkbox of that row? I...
2
by: P K | last post by:
I am using server.transfer for a website being developed in vs.net 2005 And I need to get the posted values after server.transfer. For this I set the second parameter "true" in the transfer...
6
by: Dugo | last post by:
I'm trying to use a "keypad" form (with command buttons for numbers 0 thru 9) I developed to allow users to first click a textbox on one form, then click a number on the keypad and have the value...
1
by: Michael | last post by:
Hi, I've got a simple web(asp.net) form that adds a record to the database. That works fine, but I also would like to have the datagrid refreshed with the new record. What is the best way to...
2
by: marty | last post by:
I did some searchs, but can't find the answer to my problems. I have a editable DataGrid. I do an update of one of the rows. Postback "OnUpdateCommand" Call Oracle proc to do that actual...
1
by: HenHouse | last post by:
Hi all; I think I've asked this question before but it looks like I'm going to need a deeper understanding of the problem rather than just a quick fix... I have three tables represented by three...
4
by: injanib via AccessMonster.com | last post by:
I have a combo box called "Recipient" who's row source is a table called "Main" with three columns. The three columns are "Name", "Floor", "Location". Following the combo box are two fields called...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.