Reputation: 935
I've created an DbDataAdapter from a DbConnection, filled a DataTable, and put it into a DataGridView.
When I addapt/insert/delete data in the DataGridView, I want to save it and update the database.
What do I exactly have to do?
(I've created the DbCommands myself; an example of the Update-command (is it correct?):
DbCommand updateCmd = connection.CreateCommand();
DbParameter param1 = updateCmd.CreateParameter();
DbParameter param2 = updateCmd.CreateParameter();
DbParameter param3 = updateCmd.CreateParameter();
param1.Direction = ParameterDirection.Input;
param1.ParameterName = "@firstname";
param1.SourceColumn = "firstname";
param1.DbType = (DbType.String);
param2.Direction = ParameterDirection.Input;
param2.ParameterName = "@lastname";
param2.SourceColumn = "lastname";
param2.DbType = (DbType.String);
param3.Direction = ParameterDirection.Input;
param3.ParameterName = "@mail";
param3.SourceColumn = "mail";
param3.DbType = (DbType.String);
updateCmd.CommandText = "UPDATE Personen SET fistname=@firstname,lastname=@lastname,mail=@mail";
dbAdapter.UpdateCommand = updateCmd;
I think, the "save" button has this as code:
adapt.Update(table);
But I get an error:
Must declare the scalar variable "@firstname".
Upvotes: 0
Views: 637
Reputation: 2884
CreateParameter
method only creates a SqlParameter
instance, but does not add the parameter to the SqlCommand
. You have to add the parameters to the command using updateCmd.Parameters.Add
method.
Upvotes: 1