Unable to update data adapter - C#

Any expert here pls help me up. Recently i was learning about d C#.net. I

was following d msdn guide in Updating d data adapter wit dataset. When

i try d debug d code line by line, everythings seems fine until d last

statement which is update d data adapter failed(I highlight with red

color in my code). D exception tat is displayed

An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: Update unable to find TableMapping['Table'] or DataTable 'Table'.

This is my code:

string cString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=white";
SqlConnection myConnection = new SqlConnection(cString);

myConnection.Open();
SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT * FROM Suppliers",
myConnection);

myAdapter.UpdateCommand = new SqlCommand("UPDATE Suppliers SET " +
"CompanyName = @CompanyName, " +
"ContactName = @ContactName, ContactTitle = @ContactTitle, " +
"Address = @Address, City= @City, Region = @Region, " +
"PostalCode = @PostalCode, Country = @Country, Phone = @Phone, " +
"Fax =@Fax, HomePage = @Homepage " +
"WHERE SupplierID = @SupplierID ", myConnection);

myAdapter.UpdateCommand.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40);
myAdapter.UpdateCommand.Parameters.Add("@ContactName", SqlDbType.NVarChar, 30);
myAdapter.UpdateCommand.Parameters.Add("@ContactTitle", SqlDbType.NVarChar, 30);
myAdapter.UpdateCommand.Parameters.Add("@Address", SqlDbType.NVarChar, 60);
myAdapter.UpdateCommand.Parameters.Add("@City", SqlDbType.NVarChar, 15);
myAdapter.UpdateCommand.Parameters.Add("@Region", SqlDbType.NVarChar, 15);
myAdapter.UpdateCommand.Parameters.Add("@PostalCode", SqlDbType.NVarChar, 10);
myAdapter.UpdateCommand.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
myAdapter.UpdateCommand.Parameters.Add("@Phone", SqlDbType.NVarChar, 24);
myAdapter.UpdateCommand.Parameters.Add("@Fax", SqlDbType.NVarChar, 24);
myAdapter.UpdateCommand.Parameters.Add("@Homepage", SqlDbType.NText, 16);

SqlParameter myParam = myAdapter.UpdateCommand.Parameters.Add("@SupplierID", SqlDbType.Int, 4);
myParam.SourceColumn = "SupplierID";
myParam.SourceVersion = DataRowVersion.Original;

DataSet myDataSet = new DataSet();
myAdapter.Fill(myDataSet, "Suppliers");

DataRow cRow = myDataSet.Tables["Suppliers"].Rows[0];
cRow["Region"] = "Europe";

myAdapter.Update(myDataSet);

[3070 byte] By [v1nc3ntl1] at [2007-12-22]
# 1
Most likely it is becuse you have not specified SourceColumn for all the parameters and DataAdapter has no idea where to take values from during updating database. You should map each column from the DataTable
VMazur at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 2
Erm, still cannot add after i add in the source column fo all the parameter . The same error prompt out.

This is my code.

string cString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=white";
SqlConnection myConnection = new SqlConnection(cString);

myConnection.Open();

SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT * FROM Suppliers",
myConnection);

myAdapter.UpdateCommand = new SqlCommand("UPDATE Suppliers SET " +
"CompanyName = @CompanyName, " +
"ContactName = @ContactName, ContactTitle = @ContactTitle, " +
"Address = @Address, City= @City, Region = @Region, " +
"PostalCode = @PostalCode, Country = @Country, Phone = @Phone, " +
"Fax =@Fax, HomePage = @Homepage " +
"WHERE SupplierID = @SupplierID ", myConnection);

myAdapter.UpdateCommand.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
myAdapter.UpdateCommand.Parameters.Add("@ContactName", SqlDbType.NVarChar, 30, "ContactName");
myAdapter.UpdateCommand.Parameters.Add("@ContactTitle", SqlDbType.NVarChar, 30, "ContactTitle");
myAdapter.UpdateCommand.Parameters.Add("@Address", SqlDbType.NVarChar, 60, "Address");
myAdapter.UpdateCommand.Parameters.Add("@City", SqlDbType.NVarChar, 15, "City");
myAdapter.UpdateCommand.Parameters.Add("@Region", SqlDbType.NVarChar, 15, "Region");
myAdapter.UpdateCommand.Parameters.Add("@PostalCode", SqlDbType.NVarChar, 10, "PostalCode");
myAdapter.UpdateCommand.Parameters.Add("@Country", SqlDbType.NVarChar, 15, "Country");
myAdapter.UpdateCommand.Parameters.Add("@Phone", SqlDbType.NVarChar, 24, "Phone");
myAdapter.UpdateCommand.Parameters.Add("@Fax", SqlDbType.NVarChar, 24, "Fax");
myAdapter.UpdateCommand.Parameters.Add("@Homepage", SqlDbType.NText, 16, "Homepage");

SqlParameter myParam = myAdapter.UpdateCommand.Parameters.Add("@SupplierID", SqlDbType.Int, 4, "SupplierID");
myParam.SourceColumn = "SupplierID";
myParam.SourceVersion = DataRowVersion.Original;

DataSet myDataSet = new DataSet();
myAdapter.Fill(myDataSet, "Suppliers");

DataRow cRow = myDataSet.Tables["Suppliers"].Rows[0];
cRow["Region"] = "Europe";

myAdapter.Update(myDataSet);

v1nc3ntl1 at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 3

Try doing myAdapter.Update(myDataSet.Tables["Suppliers"]);

MatthijsKrempel at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 4

Or you could use myAdapter.Update(myDataSet, "Suppliers"). Just make sure it's consistent with your Fill() statement.

Another note, for the updated column, you shouldn't set the the sourceversion to original.

BillLin-MSFT at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified