S
Steve Murphy
I'm new to .NET, and I'm having problems creating an Adapter update command
for a simple data set. I'm using the following code which compiles and runs,
but does not update the database.
This is just a barebones test using the Northwind database. Since this is a
learning exercies, I'm not worried about concurrency at this time.
I'm updating the database from bound textboxes. The data table contains one
row in the dataset. The connection, adapter, and dataset are already
created.
Thanks in advance.
// Create and configure command object
SqlCommand cmdUpdateCust = cnCust.CreateCommand();
cmdUpdateCust.CommandText = "UPDATE Customers SET CompanyName =
@NewCompanyName " +
"WHERE CustomerID = @OldCustomerID";
cmdUpdateCust.Parameters.Add(new SqlParameter("@OldCustomerID",
SqlDbType.NChar, 5, "CustomerID"));
cmdUpdateCust.Parameters.Add(new SqlParameter("@NewCompanyName",
SqlDbType.NVarChar, 40, "CompanyName"));
cmdUpdateCust.Parameters["@OldCustomerID"].SourceVersion =
DataRowVersion.Original;
cmdUpdateCust.Parameters["@NewCompanyName"].SourceVersion =
DataRowVersion.Proposed;
daCust.UpdateCommand = cmdUpdateCust;
// Push the update to the datebase
cnCust.Open();
int NumRows = daCust.Update(dsCust.Tables["Customers"]);
cnCust.Close();
for a simple data set. I'm using the following code which compiles and runs,
but does not update the database.
This is just a barebones test using the Northwind database. Since this is a
learning exercies, I'm not worried about concurrency at this time.
I'm updating the database from bound textboxes. The data table contains one
row in the dataset. The connection, adapter, and dataset are already
created.
Thanks in advance.
// Create and configure command object
SqlCommand cmdUpdateCust = cnCust.CreateCommand();
cmdUpdateCust.CommandText = "UPDATE Customers SET CompanyName =
@NewCompanyName " +
"WHERE CustomerID = @OldCustomerID";
cmdUpdateCust.Parameters.Add(new SqlParameter("@OldCustomerID",
SqlDbType.NChar, 5, "CustomerID"));
cmdUpdateCust.Parameters.Add(new SqlParameter("@NewCompanyName",
SqlDbType.NVarChar, 40, "CompanyName"));
cmdUpdateCust.Parameters["@OldCustomerID"].SourceVersion =
DataRowVersion.Original;
cmdUpdateCust.Parameters["@NewCompanyName"].SourceVersion =
DataRowVersion.Proposed;
daCust.UpdateCommand = cmdUpdateCust;
// Push the update to the datebase
cnCust.Open();
int NumRows = daCust.Update(dsCust.Tables["Customers"]);
cnCust.Close();