Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Update requires a valid UpdateCommand when passed DataRow collection with modified rows. [modified]

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

Scheduled Pinned Locked Moved C#
helpdatabaseannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    astrovirgin
    wrote on last edited by
    #1

    Hi, I am inserting and updating the database using the DataGridView. But when I insert or update the table in the database I got this error:"Update requires a valid UpdateCommand when passed DataRow collection with modified rows." I am writing the code here.

            private void btnInsert\_Click(object sender, EventArgs e)
            {
                sqlDataAdapter.Update((DataTable)bindingSource.DataSource);//Here I am getting the error.
            }
    
            private void Form1\_Load(object sender, EventArgs e)
            {
                dataGridView1.DataSource = bindingSource;
                PopulateDataGridView();
               
            }
    
            private void PopulateDataGridView()
            {
                try
                {
                   String sqlCommand = "select \* from " + tableName;
    
                   sqlDataAdapter = new SqlDataAdapter(sqlCommand, connString);
    
                   SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sqlDataAdapter);
    
                   DataSet dsData = new DataSet();
                   dsData.Locale = CultureInfo.InvariantCulture;
                       
                   sqlDataAdapter.Fill(dsData);
    
                   bindingSource.DataSource=dsData.Tables\[0\];
                        
                        
                }
                catch (SqlException sqlException)
                {
                    MessageBox.Show(sqlException.Message.ToString());
                }
                
            }
    

    Everything is working fine but when I click on the Insert Button it gives me error. Please Help.

    modified on Wednesday, October 8, 2008 6:19 AM

    G N 2 Replies Last reply
    0
    • A astrovirgin

      Hi, I am inserting and updating the database using the DataGridView. But when I insert or update the table in the database I got this error:"Update requires a valid UpdateCommand when passed DataRow collection with modified rows." I am writing the code here.

              private void btnInsert\_Click(object sender, EventArgs e)
              {
                  sqlDataAdapter.Update((DataTable)bindingSource.DataSource);//Here I am getting the error.
              }
      
              private void Form1\_Load(object sender, EventArgs e)
              {
                  dataGridView1.DataSource = bindingSource;
                  PopulateDataGridView();
                 
              }
      
              private void PopulateDataGridView()
              {
                  try
                  {
                     String sqlCommand = "select \* from " + tableName;
      
                     sqlDataAdapter = new SqlDataAdapter(sqlCommand, connString);
      
                     SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sqlDataAdapter);
      
                     DataSet dsData = new DataSet();
                     dsData.Locale = CultureInfo.InvariantCulture;
                         
                     sqlDataAdapter.Fill(dsData);
      
                     bindingSource.DataSource=dsData.Tables\[0\];
                          
                          
                  }
                  catch (SqlException sqlException)
                  {
                      MessageBox.Show(sqlException.Message.ToString());
                  }
                  
              }
      

      Everything is working fine but when I click on the Insert Button it gives me error. Please Help.

      modified on Wednesday, October 8, 2008 6:19 AM

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      Try setting UpdateCommand property of SqlDataAdapter class manually.

      Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion

      1 Reply Last reply
      0
      • A astrovirgin

        Hi, I am inserting and updating the database using the DataGridView. But when I insert or update the table in the database I got this error:"Update requires a valid UpdateCommand when passed DataRow collection with modified rows." I am writing the code here.

                private void btnInsert\_Click(object sender, EventArgs e)
                {
                    sqlDataAdapter.Update((DataTable)bindingSource.DataSource);//Here I am getting the error.
                }
        
                private void Form1\_Load(object sender, EventArgs e)
                {
                    dataGridView1.DataSource = bindingSource;
                    PopulateDataGridView();
                   
                }
        
                private void PopulateDataGridView()
                {
                    try
                    {
                       String sqlCommand = "select \* from " + tableName;
        
                       sqlDataAdapter = new SqlDataAdapter(sqlCommand, connString);
        
                       SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sqlDataAdapter);
        
                       DataSet dsData = new DataSet();
                       dsData.Locale = CultureInfo.InvariantCulture;
                           
                       sqlDataAdapter.Fill(dsData);
        
                       bindingSource.DataSource=dsData.Tables\[0\];
                            
                            
                    }
                    catch (SqlException sqlException)
                    {
                        MessageBox.Show(sqlException.Message.ToString());
                    }
                    
                }
        

        Everything is working fine but when I click on the Insert Button it gives me error. Please Help.

        modified on Wednesday, October 8, 2008 6:19 AM

        N Offline
        N Offline
        nelsonpaixao
        wrote on last edited by
        #3

        listen, i can´t debug that, because the way i code is different. :doh: Try this routine, may work out for you.

        // connection string
        public static string connection_string = "..."
        // store procedure
        public static string SP_whatever = "...";

        // database connection (no parameters)
        public static void DataGridView_Fill(DataGridView dgv, string store_procedure)
        {
        SqlConnection sql_conn = new SqlConnection(connection_string);
        SqlCommand sql_cmd = new SqlCommand();
        sql_cmd.Connection = sql_conn;
        sql_cmd.CommandText = store_procedure;
        sql_cmd.CommandType = CommandType.StoredProcedure;
        sql_conn.Open();
        SqlDataAdapter sql_da = new SqlDataAdapter();
        sql_da.SelectCommand = sql_cmd;
        DataTable dt = new DataTable();
        sql_da.Fill(dt);
        sql_conn.Close();
        dgv.DataSource = dt;
        }

        nelsonpaixao@yahoo.com.br trying to help & get help

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups