473,387 Members | 1,925 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,387 software developers and data experts.

DbDataAdapter.Update() not working?

TheMan1
19
I'm having some trouble updating retrieved data in a DataSet, in C#. Here's the code:

(I'm using the Firebird Client btw)

Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2.         {
  3.  
  4.             DbProviderFactory f = DbProviderFactories.GetFactory("FirebirdSql.Data.FirebirdClient");
  5.             DbConnection conn = f.CreateConnection();
  6.             conn.ConnectionString = "user=admin;password=password;database=TEST.FDB;ServerType=1";    
  7.             DbCommand command = f.CreateCommand();
  8.             command.Connection = conn;
  9.             DbDataAdapter adapter = f.CreateDataAdapter();
  10.  
  11.             //Query database. Get the first row of the result.
  12.             command.CommandText = "SELECT * FROM testtable WHERE testid = 2";
  13.             adapter.SelectCommand = command;
  14.             DataSet data = new DataSet();
  15.             adapter.Fill(data);
  16.             DataRow row = data.Tables[0].Rows[0];
  17.  
  18.             //Modify that row.
  19.             row.BeginEdit();
  20.             row.ItemArray[1] = "TEST";  //change the value in the second column.
  21.             row.EndEdit();
  22.  
  23.             //Apparently the PK needs to be set prior to updates... shouldn't matter.
  24.             DataColumn[] columns = new DataColumn[1];
  25.             columns[0] = data.Tables[0].Columns[0];     //first column is PK.
  26.             data.Tables[0].PrimaryKey = columns;
  27.  
  28.             //Perform the update.
  29.             command.CommandText = "";
  30.             adapter.UpdateCommand = command;
  31.             adapter.Update(data);
  32. }
Line 29 generates an exception, because the CommandText is null. What would I put as the CommandText to allow my small row change above in the code to be executed and committed? If I try to do adapter.Update(data) by itself it says I need to define an UpdateCommand, so I need to have it.

Any help would be appreciated, since this is driving me nuts.
Oct 8 '08 #1
7 5626
Curtis Rutland
3,256 Expert 2GB
You need to write a SQL UPDATE command. Write one that will update all values in the query, and use parameters.
Oct 8 '08 #2
TheMan1
19
You need to write a SQL UPDATE command. Write one that will update all values in the query, and use parameters.
But doesn't that defeat the purpose of modifying the row at line 20, if I'm just going to write SQL that does the same thing? What's the point of having Update() then?
Oct 8 '08 #3
mldisibio
190 Expert 100+
insertAlias is correct, and often writing your own UPDATE command is the most efficient. You can also specify a stored procedure for the UPDATE command.
However, there is also the possibility of letting the Framework generate the commands for you. If you ever look at the final output, it basically writes a sql statement that reads like a congressional bailout plan...covers every single row and column but you can't tell what it does!

In any case (MSDN):

"If INSERT, UPDATE, or DELETE statements have not been specified, the Update method generates an exception. However, you can create a SqlCommandBuilder or OleDbCommandBuilder object to automatically generate SQL statements for single-table updates if you set the SelectCommand property of a .NET Framework data provider. Then, any additional SQL statements that you do not set are generated by the CommandBuilder. This generation logic requires key column information to be present in the DataSet. For more information see
Automatically Generating Commands ."
Oct 8 '08 #4
Curtis Rutland
3,256 Expert 2GB
But doesn't that defeat the purpose of modifying the row at line 20, if I'm just going to write SQL that does the same thing? What's the point of having Update() then?
No, because you use parameters in your statement:
Expand|Select|Wrap|Line Numbers
  1. UPDATE table SET field1=@field1
So that it is a generic update statement.

The adapter uses that statement to do the update for you.
Oct 8 '08 #5
mldisibio
190 Expert 100+
The DataSet is a container for your data. It knows nothing about the source of that data.
The DataAdapater facilitates communication between the DataSet and the actual database.

When you 'update' a field in a DataSet, the DataSet tracks those changes for you in a way that makes you think you are dealing directly with a database: it keeps track of the RowState with states such as "Modified, Deleted, Inserted".
So when you change a field, it knows it is "updated", and if somethings asks it for "give me all your updated fields" it knows what to return.

The DataAdapter is the one asking "Give me all your updated fields." However, the DataAdapter doesn't necessarily know what to do with it. If something asks it to "run the update" it knows what to do IF it has been configured.

Your code is the one asking the DataAdapter to run its update. It has its connection configured, but it also needs the actual UPDATE statement configured, just as you configured the SELECT statement.

What confuses many coders at first is that if you drag and drop Adapters and DataSets into the designer, much of that 'configuration' is auto-generated.
As I mentioned in the previous post, you can even have the actual sql update statements auto-generated, and some tutorials lead developers down that mudslide.
Oct 8 '08 #6
try DbCommandBuilder!!
DbCommandBuilder can autocreate insertCommand,updateCommand,deleteCommand
Oct 9 '08 #7
Expand|Select|Wrap|Line Numbers
  1. DbProviderFactory f = DbProviderFactories.GetFactory("FirebirdSql.Data.FirebirdClient");
  2.             DbConnection conn = f.CreateConnection();
  3.             conn.ConnectionString = "user=admin;password=password;database=TEST.FDB;Ser  verType=1";
  4.             DbCommand command = f.CreateCommand();
  5.             command.Connection = conn;
  6.             DbDataAdapter adapter = f.CreateDataAdapter();
  7.  
  8.             //Query database. Get the first row of the result.
  9.             command.CommandText = "SELECT * FROM testtable WHERE testid = 2";
  10.             adapter.SelectCommand = command;
  11.             DataSet data = new DataSet();
  12.             adapter.Fill(data);
  13.             DataRow row = data.Tables[0].Rows[0];
  14.  
  15.             //Modify that row.
  16.             row.BeginEdit();
  17.             row.ItemArray[1] = "TEST";  //change the value in the second column.
  18.             row.EndEdit();
  19.  
  20.             //Apparently the PK needs to be set prior to updates... shouldn't matter.
  21.             DataColumn[] columns = new DataColumn[1];
  22.             columns[0] = data.Tables[0].Columns[0];     //first column is PK.
  23.             data.Tables[0].PrimaryKey = columns;
  24.  
  25.             //Perform the update.
  26.             //command.CommandText = "";
  27.             //adapter.UpdateCommand = command;
  28.             //dbCommandBuilder can autocreate updatecommand
  29.             DbCommandBuilder dbCmdBuilder = f.CreateCommandBuilder();
  30.             dbCmdBuilder.DataAdapter = adapter;
  31.             adapter.Update(data);
  32.  
Bold font is revised


//Perform the update.
//command.CommandText = "";
//adapter.UpdateCommand = command;
//dbCommandBuilder can autocreate updatecommand
DbCommandBuilder dbCmdBuilder = f.CreateCommandBuilder();
dbCmdBuilder.DataAdapter = adapter;
adapter.Update(data);


sorry my english is poor!!
Oct 9 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
3
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
5
by: HydroSan | last post by:
Having a bit of a problem getting UPDATE working. The project in question is a simple MySQL VB.NET frontend, allowing Insertion, Selection, and others. Well, I've gotten Drop and Insert working,...
8
by: Zorpiedoman | last post by:
I keep getting a concurrency exception the second time I make a change and attempt to update a dataadapter. It appears this is by design, so there must be something I can do to avoid it. ...
2
by: Greg Young | last post by:
I found this while answerring a question yesterday .. I must be missing something. 2.0 DbDataAdapter::Dispose() ..method public hidebysig newslot virtual final instance void Dispose() cil...
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
2
by: travhale | last post by:
in a new project using .net 2005, c#. getting err message "Update requires a valid UpdateCommand when passed DataRow collection with modified rows." source RDBMS is oracle 8i. I add a new...
6
by: Nuzzi | last post by:
Hello All, I have two pages that are very similar. One is working, one is not. Here is the code for both: Page 1 (Working): protected void btn_update_Click(object sender, EventArgs e)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.