none
DataAdapter.Update Error RRS feed

  • Question

  • Hi All,

    I have binded the datagrid with the dataset using the fill method of the data adapter. But when I'm updating it after changing a cell's value in the datagrid, its giving me the following exception :


    Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

     

    Please let me where I'm doing wrong. The code snippet is given below.

      

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Windows.forms;

    class DisconnectedDataform : form
    {
     private SqlConnection  conn;
     private SqlDataAdapter daCustomers;

     private DataSet  dsCustomers;
     private DataGrid dgCustomers;

     private const string tableName = "Customers";

     // initialize form with DataGrid and Button
     public DisconnectedDataform()
     {
      // fill dataset
      Initdata();

      // set up datagrid
      dgCustomers = new DataGrid();
      dgCustomers.Location = new Point(5, 5);
      dgCustomers.Size = new Size(   this.Clientrectangle.Size.Width - 10,    this.Clientrectangle.Height - 50);
      dgCustomers.DataSource = dsCustomers;
      dgCustomers.DataMember = tableName;

      // create update button
      Button btnUpdate = new Button();
      btnUpdate.Text = "Update";
      btnUpdate.Location = new Point(
       this.Clientrectangle.Width/2 - btnUpdate.Width/2,
       this.Clientrectangle.Height - (btnUpdate.Height + 10));
      btnUpdate.Click += new EventHandler(btnUpdateClicked);

      // make sure controls appear on form
      Controls.AddRange(new Control[] { dgCustomers, btnUpdate });
     }

     // set up ADO.NET objects
     public void Initdata()
     {
      // instantiate the connection
      conn = new SqlConnection(   "Server=(local);DataBase=Northwind;Integrated Security=SSPI");
      
      // 1. instantiate a new DataSet
      dsCustomers = new DataSet();

      // 2. init SqlDataAdapter with select command and connection
      daCustomers = new SqlDataAdapter(   "select CustomerID, CompanyName from Customers", conn);

      // 3. fill in insert, update, and delete commands
      SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daCustomers);
      
      // 4. fill the dataset
      daCustomers.Fill(dsCustomers, tableName);
     }

     // Update button was clicked
     public void btnUpdateClicked(object sender, EventArgs e)
     {
      // write changes back to DataBase
      daCustomers.Update(dsCustomers, tableName);
     }

     // start the Windows form
     static void Main()
     {
      Application.Run(new DisconnectedDataform());
     }
     }

    Friday, December 28, 2007 6:44 AM

All replies

  •  

    Try this

     

     

    DataSet ds = (DataSet) dgCustomers.DataSource ;

     

    daCustomers.Update(ds, tableName);
    Saturday, December 29, 2007 5:46 AM
  • ADO.NET's DataAdapter is not a black-box technology, it allows you to control the update logic of your dataset. So, it will not automatically generate the UpdateCommand for you. You should provider a UpdateCommand to it.

     

    if you don't want to write the time-wasting UpdateCommand yourself, you can use the SqlCommandBuilder to generate the command for you.
    Saturday, December 29, 2007 8:28 AM
  • He already is using the SqlCommandBuilder.

     

    Anyway, try this instead, it may make a difference:

     

    Code Block
    dgCustomers.DataSource = dsCustomers.Tables[tableName];

     

    Sunday, December 30, 2007 12:52 AM