Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
iv'e got an assignment witch requires me to update the northwind database , iv'e done every thing like the tutorials say as follows
i fill The DataTable Using The DataAdapter.Fill(table);
i build the Delete,Insert,Update Commands using CommangBuilder

SqlDataAdapter adapter = new SqlDataAdapter(selectStr, conn);
        SqlCommandBuilder builder = new SqlCommandBuilder(adapter); 
        adapter.DeleteCommand = builder.GetDeleteCommand(true);
        adapter.UpdateCommand = builder.GetUpdateCommand(true);
        adapter.InsertCommand = builder.GetInsertCommand(true);
        adapter.Fill(employees_table);


i also set a primary key for the table :
DataColumn[] employees_keys = new DataColumn[2];
employees_keys[0] = employees.Columns["EmployeeID"];
employees_table.PrimaryKey = employees_keys;

now iv'e attempted to delete and add a row :
//accepts an employee object and creates a new new row with the appropriate values for 
// an employee table row 
DataRow row = ConvertEmployeeToRow(employeeToAdd);
employee_table.Rows.Add(row);`


and deleting a row :
DataRow row = employees.Rows.Find(employeeToDismiss.ID);
employees.Rows.Remove(row); 

i should also point out that iv'e attempted to use row.SetAdded() and row.Delete()
any ways at the end when i try to update the database
int k = employees_adapter.Update(employees_table);

on added rows sometimes k get valued , on remove never , and in either case nothing really gets updated at all in the data base it self .
any insight of what i'm doing wrong would be most appreciated.
Posted

1 solution

To add a row to a DataTable try using NewRow() method of your "employees" DataTable and then assign new values columns of this new row

Remove method of DataTable does'n delete a row . it just remove a row from DataTable . try using Delete() method of datatable .

the pseudo code could be something like :

// Adding a row<br />
DataRow row = employees.NewRow();<br />
row["someField1"] = someText1 ;<br />
row["someField2"] = someText2<br />
employees.Rows.Add(row);<br />
<br />
// Deleteing a row<br />
employees.Rows.Find(employeeToDismiss.ID).Delete();<br />
<br />
int k = employees_adapter.Update(employees_table);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900