Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Data binding error
Message
 
To
05/04/2008 00:42:03
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01307537
Message ID:
01308707
Views:
11
Bonnie, I added a call to CommitProposedChanges in my UpdateUser and it worked. Thanks!

Mike, Thanks for your help too!
public void UpdateUser(DataSet oUserData)
{

    CommitProposedChanges(oUserData);

    SqlConnection oConnection = _GetConnection();

    SqlDataAdapter oDataAdapter = new SqlDataAdapter("select * from AppUsers", oConnection);
    SqlCommandBuilder oBuilder = new SqlCommandBuilder(oDataAdapter);

    oDataAdapter.Update(oUserData, oUserData.Tables[0].TableName);
}

private void CommitProposedChanges(DataSet oDataSet)
{
    if (oDataSet == null)
        return;

    for (int nTable = 0; nTable < oDataSet.Tables.Count; nTable++)
    {
        for (int nRow = 0; nRow < oDataSet.Tables[nTable].Rows.Count; nRow++)
        {
            if (oDataSet.Tables[nTable].Rows[nRow].HasVersion(DataRowVersion.Proposed))
            {
                oDataSet.Tables[nTable].Rows[nRow].EndEdit();
            }
        }
    }
}
>A couple of things to consider Kevin:
>
>1) Yes, using the CommandBuilder requires the use of a PK.
>2) If you've done a DataSet.AcceptChanges(), the Update will not think there are any changes to Update, and consequently nothing will be saved to the database.
>3) Sometimes a row in a DataTable is left in a Proposed state and needs an EndEdit(). Data in a DataRow has several different versions. First, there's the original version. Then, when it's being edited, it becomes a Proposed version and once it's done being edited it becomes the Current version. Sometimes when editing, the row is left in the Proposed state and needs to be ended.
>
>Here's a method I *always* call before I attempt to check for .HasChanges() before saving data:
>
>protected virtual void CommitProposedChanges(DataSet ds)
>{
>	if (ds == null)
>		return;
>
>	for (int nTable = 0; nTable < ds.Tables.Count; nTable++)
>	{
>		for (int nRow = 0; nRow < ds.Tables[nTable].Rows.Count; nRow++)
>		{
>			if (ds.Tables[nTable].Rows[nRow].HasVersion(DataRowVersion.Proposed))
>			{
>				ds.Tables[nTable].Rows[nRow].EndEdit();
>			}
>		}
>	}
>}
>
>
>If none of these suggestions help, then it's obviously some other problem and maybe we might need to see some code.
>
>Personally, I do not like the CommandBuilder and discourage it's use ... I know it seems like the "easy" way, but easy is not always best.
>
>~~Bonnie
>
>
>
>
>>Ok. In Bonnie's message #1306779 she provided 3 ways to do it. The first said use the Update, which I tried here, and
>>the other 2 use more in depth approaches. I'm still confused on this.
>>
>>Can you provide an example of what you're referring to here?
>>
>>
>>
>>>It's not being saved in the DB because ADO.NET is a disconnected architecture. It is being saved in your dataset, but you will need to write procedures to save that back to the database. Kind of tricky at first, and frustrating, but once you get the hand of it there are definitely benefits.
>>>
>>>>Ok, I see.
>>>>
>>>>Now I see data in the field. But when I change the data in the textbox, it's not being saved to the DB
>>>>
>>>>
>>>>public void UpdateUser(DataSet oUserData)
>>>>{
>>>>
>>>>    SqlConnection oConnection = _GetConnection();
>>>>
>>>>    SqlDataAdapter oDataAdapter = new SqlDataAdapter("select * from AppUsers", oConnection);
>>>>    SqlCommandBuilder oBuilder = new SqlCommandBuilder(oDataAdapter);
>>>>
>>>>    oDataAdapter.Update(oUserData, oUserData.Tables[0].TableName);
>>>>}
>>>>
>>>>
>>>>When I open the DataSet Visualizer at this point, the change is there. It just doesnt get saved to
>>>>the database. I think it was Bonnie who mentioned the dataset needing a PK
>>>>
>>>>Any thoughts on this?
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>>When you bind, you bind directly to the datatable, not the dataset. The dataset is the object, and the datatable is a collection property of the dataset. You are indexing the first datatable in the collection by using 0 (or you could alternatively use your table name - "UserData"). The actual field name is a property of the datatable. When you used "UserData.FullName", it saw that as the complete field name - not the table.fieldname.
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Reply
Map
View

Click here to load this message in the networking platform