I'm having some trouble updating retrieved data in a DataSet, in C#. Here's the code:
(I'm using the Firebird Client btw) - static void Main(string[] args)
-
{
-
-
DbProviderFactory f = DbProviderFactories.GetFactory("FirebirdSql.Data.FirebirdClient");
-
DbConnection conn = f.CreateConnection();
-
conn.ConnectionString = "user=admin;password=password;database=TEST.FDB;ServerType=1";
-
DbCommand command = f.CreateCommand();
-
command.Connection = conn;
-
DbDataAdapter adapter = f.CreateDataAdapter();
-
-
//Query database. Get the first row of the result.
-
command.CommandText = "SELECT * FROM testtable WHERE testid = 2";
-
adapter.SelectCommand = command;
-
DataSet data = new DataSet();
-
adapter.Fill(data);
-
DataRow row = data.Tables[0].Rows[0];
-
-
//Modify that row.
-
row.BeginEdit();
-
row.ItemArray[1] = "TEST"; //change the value in the second column.
-
row.EndEdit();
-
-
//Apparently the PK needs to be set prior to updates... shouldn't matter.
-
DataColumn[] columns = new DataColumn[1];
-
columns[0] = data.Tables[0].Columns[0]; //first column is PK.
-
data.Tables[0].PrimaryKey = columns;
-
-
//Perform the update.
-
command.CommandText = "";
-
adapter.UpdateCommand = command;
-
adapter.Update(data);
-
}
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.
7 5626
You need to write a SQL UPDATE command. Write one that will update all values in the query, and use parameters.
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?
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 ."
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: - UPDATE table SET field1=@field1
So that it is a generic update statement.
The adapter uses that statement to do the update for you.
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.
try DbCommandBuilder!!
DbCommandBuilder can autocreate insertCommand,updateCommand,deleteCommand
-
DbProviderFactory f = DbProviderFactories.GetFactory("FirebirdSql.Data.FirebirdClient");
-
DbConnection conn = f.CreateConnection();
-
conn.ConnectionString = "user=admin;password=password;database=TEST.FDB;Ser verType=1";
-
DbCommand command = f.CreateCommand();
-
command.Connection = conn;
-
DbDataAdapter adapter = f.CreateDataAdapter();
-
-
//Query database. Get the first row of the result.
-
command.CommandText = "SELECT * FROM testtable WHERE testid = 2";
-
adapter.SelectCommand = command;
-
DataSet data = new DataSet();
-
adapter.Fill(data);
-
DataRow row = data.Tables[0].Rows[0];
-
-
//Modify that row.
-
row.BeginEdit();
-
row.ItemArray[1] = "TEST"; //change the value in the second column.
-
row.EndEdit();
-
-
//Apparently the PK needs to be set prior to updates... shouldn't matter.
-
DataColumn[] columns = new DataColumn[1];
-
columns[0] = data.Tables[0].Columns[0]; //first column is PK.
-
data.Tables[0].PrimaryKey = columns;
-
-
//Perform the update.
-
//command.CommandText = "";
-
//adapter.UpdateCommand = command;
-
//dbCommandBuilder can autocreate updatecommand
-
DbCommandBuilder dbCmdBuilder = f.CreateCommandBuilder();
-
dbCmdBuilder.DataAdapter = adapter;
-
adapter.Update(data);
-
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!!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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,...
|
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.
...
|
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...
|
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...
|
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...
|
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)...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
| |