I'm having some trouble updating retrieved data in a DataSet, in C#. Here's the code:
(I'm using the Firebird Client btw)
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.
(I'm using the Firebird Client btw)
Code:
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.
[B]command.CommandText = "";[/B]
adapter.UpdateCommand = command;
adapter.Update(data);
}
Any help would be appreciated, since this is driving me nuts.
Comment