3

if I have a DataSet called myDs and I edit a field in it by direct access in a loop like the following:

for (int i = 0; i < myDS.Tables[TableName].Rows.Count; i++)
{

    //some function or web method to get the id value of the record being updated
    int n = getNewNumber();

    //updating the dataset record according to some condition
    if (n == 0)
    {
        myDS.Tables[TableName].Rows[i]["id"] = n;
        myDS.Tables[TableName].Rows[i]["description"] = "some data";
    }
    else
    {
        myDS.Tables[TableName].Rows[i]["id"] = n;
        myDS.Tables[TableName].Rows[i]["description"] = "new data";
    }
}

How I make these changes done in the database as I could see it in the GridView when I do databind() but the database is not affected and I try using the fill & update methods of OdbcDataAdapter and OdbcCommandBuilder?

Jaroslav Kadlec
  • 2,505
  • 4
  • 32
  • 43
TopDeveloper
  • 542
  • 2
  • 14
  • 43

3 Answers3

2

Try the TableAdapter.Update process shown in this article: How to: Update Records in a Database.

Also, please make sure you not only have the necessary access to the database you are trying to connect to, but also permission to update records in the desired table. You may have a SQL Server configuration problem that is preventing your code from updating.

John Tobler
  • 404
  • 2
  • 14
  • This link is good but I discovered that the problem in the Update Statement as I supposed not to change the Primary key as I used it in both the set clause and where clause. – TopDeveloper Aug 17 '11 at 11:57
0

You should type two line after loop

myDS.AcceptChanges();
adapter.Update(myDS.Tables[TableName]);
Kumar Saurabh
  • 2,297
  • 5
  • 29
  • 43
  • But where is the adapter? As I can see in code the OP only uses DataSet. What if adapter is not available, or perhaps internal? (DataSet might be auto-generated by Visual Studio, for example when binging a ListBox to a new Data source). How is the database saved if ONLY the DataSet object is available? – Dexter Jun 29 '16 at 16:48
0

Since it is odbc, you might use an OdbcDataAdapter; then call:

adapter.Update(myDS.Tables[TableName]);

disclaimer: personally, I never use DataSet for.... anything. YMMV.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I did this but the database did not change according to the new updates and my update can not be written as Update Command as the data should be updated in the loop as the primary key (id) also is updated and I can not use it in the Where clause. – TopDeveloper Aug 14 '11 at 09:44