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

后端 未结 4 2230
孤独总比滥情好
孤独总比滥情好 2020-12-21 01:20

I am using Paging to show data in datagridview, but when i try to Update any data with updatebutton data should be updated In datagridview

4条回答
  •  难免孤独
    2020-12-21 02:14

    You have Created the OleDbDataAdapter with a Select command only:

    adp1 = new OleDbDataAdapter(cmd1);
    

    OleDbDataAdapter requires valid Update, Insert, Delete commands to be used to save the data like this:

    adp1.Update(dt);//here I am getting error
    

    You just need to use a OleDbCommandBuilder that will generate the commands for you:

    adp1 = new OleDbDataAdapter();
    adp1.SelectCommand = cmd1; // cmd1 is your SELECT command
    OleDbCommandBuilder cb = new OleDbCommandBuilder(adp1);
    

    EDIT

    Since you change the Select command of the OleDbDataAdapter at runtime for paging, what your need is to initialize each time you save data:

    private void button1_Click(object sender, EventArgs e)
        {
            try
            {                
                adp1.SelectCommand = cmd1; // cmd1 is your SELECT command
                OleDbCommandBuilder cb = new OleDbCommandBuilder(adp1);
                adp1.Update(dt); //here I hope you won't get error :-)
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message.ToString());
            }
    
        }
    

提交回复
热议问题