这个是界面,方便理解问题
我已经完成如下功能了,点查询后底下的LISTVIEW显示数据,然后随便点哪行,右上上的明细框会显示,
因为我是绑定了数据的,但现在问题来了,我想修改后点更新回传回数据库,但总是显示有问题,我把相关代码贴在底下,
看下哪有问题//这个段是从LISTVIEW选一行以后,在明细框中进行数据版定的代码
private DataSet bingds;
private SqlConnection con;
private SqlDataAdapter bingda;
private void listView1_Click(object sender, EventArgs e)
        {
            string str_name = listView1.SelectedItems[0].SubItems[0].Text;
            if (str_name != null)
            {
                getScoure(str_name);
            }
   
        }
        public void getScoure(string str_name)
        {
            con = new SqlConnection("server=(local);uid=sa;pwd=sa;database=assetsManagement");
            
            bingda=new SqlDataAdapter("select * from View_Assets where Assets_No= '" + str_name + "'", con);
            bingds = new DataSet();
            bingda.Fill(bingds,"View_Assets");
               textBox7.DataBindings.Clear();//资产编号
            textBox6.DataBindings.Clear();//资产名称
            comboBox4.DataBindings.Clear();//资产类型
            textBox14.DataBindings.Clear();//规格
           
            textBox13.DataBindings.Clear();//单价
            dateTimePicker3.DataBindings.Clear();//购置时间
            textBox10.DataBindings.Clear();//使用年限
            comboBox6.DataBindings.Clear();//供货商
            textBox17.DataBindings.Clear();//型号
            comboBox5.DataBindings.Clear();//所在部门
            textBox18.DataBindings.Clear();//保管人
            textBox15.DataBindings.Clear();//使用状况
            textBox8.DataBindings.Clear();//计量单位
         
                        // Add a new binding, with a new data source: the selected row.
            textBox7.DataBindings.Add("Text", bingds, "View_Assets.Assets_No", true);
            textBox6.DataBindings.Add("Text", bingds, "View_Assets.Assets_Apellaton", true);
            comboBox4.DataBindings.Add("Text", bingds, "View_Assets.AssetsStyle_Apellation", true);
            textBox14.DataBindings.Add("Text", bingds, "View_Assets.Assets_Spec", true);
            textBox13.DataBindings.Add("Text", bingds, "View_Assets.Assets_UnitPrice", true);
            dateTimePicker3.DataBindings.Add("Text", bingds, "View_Assets.Assets_PurchaseTime", true);
            textBox10.DataBindings.Add("Text", bingds, "View_Assets.Assets_UseTime", true);
            comboBox6.DataBindings.Add("Text", bingds, "View_Assets.Supplier_Unit", true);
            textBox17.DataBindings.Add("Text", bingds, "View_Assets.Assets_Model", true);
            comboBox5.DataBindings.Add("Text", bingds, "View_Assets.Department_Apellation", true);
            textBox18.DataBindings.Add("Text", bingds, "View_Assets.Assets_PerName", true);
            textBox15.DataBindings.Add("Text", bingds, "View_Assets.AssetsStatus_Apellation");
            textBox8.DataBindings.Add("Text", bingds, "View_Assets.Measure_Apellation", true);
           
            
        }
//这段是关键的了,问题可能出在这里,是点更新键以后的操作
private void button2_Click(object sender, EventArgs e)
        {
            try
            {
             
                this.UpdateDataSet();
        
            }
            catch (System.Exception eUpdate)
            {
           
           
                System.Windows.Forms.MessageBox.Show(eUpdate.Message);
            }
        }
        public void UpdateDataSet()//把数据更新回数据库
        {
     
            // Create a new dataset to hold the changes that have been made to the main dataset.
            DataSet DataSetChanges = new DataSet();
            // Stop any current edits.
            this.BindingContext[bingds,"View_Assets"].EndCurrentEdit();
           
            // Get the changes that have been made to the main dataset.
            DataSetChanges = ((DataSet)(bingds.GetChanges()));
            // Check to see if any changes have been made.
            if ((DataSetChanges != null))
            {
                try
                {
                    // There are changes that need to be made, so attempt to update the datasource by
                    // calling the update method and passing the dataset and any parameters.
               
                    this.UpdateDataSource(DataSetChanges);
                    bingds.Merge(DataSetChanges);
                    bingds.AcceptChanges();
                }
                catch (System.Exception eUpdate)
                {
              
                    // Add your error handling code here.
                    throw eUpdate;
                }
                // Add your code to check the returned dataset for any errors that may have been
                // pushed into the row object's error.
            }        }
        public void UpdateDataSource(DataSet ChangedRows)
        {
            
            try
            {
                // The data source only needs to be updated if there are changes pending.
                if ((ChangedRows != null))
                {
                    // Open the connection.
                    con.Open();
                    
                    // Attempt to update the data source.
                    bingda.Update(ChangedRows);//每次是在这里跳到catch 里面的
                    MessageBox.Show("看下能不能执行到这步");
                }
            }
            catch (System.Exception updateException)
            {
        
                // Add your error handling code here.
                throw updateException;
            }
            finally
            {
                // Close the connection whether or not the exception was thrown.
                con.Close();
            }        }

解决方案 »

  1.   

    以前我出过类似问题,貌似是没有制定dataset 中的那个一个表。用dataset时候最好指定tables[0]什么的。。^_^粘贴下你的代码,方便别人阅读
    //这个段是从LISTVIEW选一行以后,在明细框中进行数据版定的代码 
    private DataSet bingds; 
    private SqlConnection con; 
    private SqlDataAdapter bingda; 
    private void listView1_Click(object sender, EventArgs e) 
            { 
                string str_name = listView1.SelectedItems[0].SubItems[0].Text; 
                if (str_name != null) 
                { 
                    getScoure(str_name); 
                } 
        
            } 
            public void getScoure(string str_name) 
            { 
                con = new SqlConnection("server=(local);uid=sa;pwd=sa;database=assetsManagement"); 
                 
                bingda=new SqlDataAdapter("select * from View_Assets where Assets_No= '" + str_name + "'", con); 
                bingds = new DataSet(); 
                bingda.Fill(bingds,"View_Assets"); 
                    textBox7.DataBindings.Clear();//资产编号 
                textBox6.DataBindings.Clear();//资产名称 
                comboBox4.DataBindings.Clear();//资产类型 
                textBox14.DataBindings.Clear();//规格 
                
                textBox13.DataBindings.Clear();//单价 
                dateTimePicker3.DataBindings.Clear();//购置时间 
                textBox10.DataBindings.Clear();//使用年限 
                comboBox6.DataBindings.Clear();//供货商 
                textBox17.DataBindings.Clear();//型号 
                comboBox5.DataBindings.Clear();//所在部门 
                textBox18.DataBindings.Clear();//保管人 
                textBox15.DataBindings.Clear();//使用状况 
                textBox8.DataBindings.Clear();//计量单位 
              
                             // Add a new binding, with a new data source: the selected row. 
                textBox7.DataBindings.Add("Text", bingds, "View_Assets.Assets_No", true); 
                textBox6.DataBindings.Add("Text", bingds, "View_Assets.Assets_Apellaton", true); 
                comboBox4.DataBindings.Add("Text", bingds, "View_Assets.AssetsStyle_Apellation", true); 
                textBox14.DataBindings.Add("Text", bingds, "View_Assets.Assets_Spec", true); 
                textBox13.DataBindings.Add("Text", bingds, "View_Assets.Assets_UnitPrice", true); 
                dateTimePicker3.DataBindings.Add("Text", bingds, "View_Assets.Assets_PurchaseTime", true); 
                textBox10.DataBindings.Add("Text", bingds, "View_Assets.Assets_UseTime", true); 
                comboBox6.DataBindings.Add("Text", bingds, "View_Assets.Supplier_Unit", true); 
                textBox17.DataBindings.Add("Text", bingds, "View_Assets.Assets_Model", true); 
                comboBox5.DataBindings.Add("Text", bingds, "View_Assets.Department_Apellation", true); 
                textBox18.DataBindings.Add("Text", bingds, "View_Assets.Assets_PerName", true); 
                textBox15.DataBindings.Add("Text", bingds, "View_Assets.AssetsStatus_Apellation"); 
                textBox8.DataBindings.Add("Text", bingds, "View_Assets.Measure_Apellation", true); 
                
                 
            } 
    //这段是关键的了,问题可能出在这里,是点更新键以后的操作 
    private void button2_Click(object sender, EventArgs e) 
            { 
                try 
                { 
                  
                    this.UpdateDataSet(); 
             
                } 
                catch (System.Exception eUpdate) 
                { 
                
                
                    System.Windows.Forms.MessageBox.Show(eUpdate.Message); 
                } 
            } 
            public void UpdateDataSet()//把数据更新回数据库 
            { 
          
                // Create a new dataset to hold the changes that have been made to the main dataset. 
                DataSet DataSetChanges = new DataSet(); 
                // Stop any current edits. 
                this.BindingContext[bingds,"View_Assets"].EndCurrentEdit(); 
                
                // Get the changes that have been made to the main dataset. 
                DataSetChanges = ((DataSet)(bingds.GetChanges())); 
                // Check to see if any changes have been made. 
                if ((DataSetChanges != null)) 
                { 
                    try 
                    { 
                        // There are changes that need to be made, so attempt to update the datasource by 
                        // calling the update method and passing the dataset and any parameters. 
                    
                        this.UpdateDataSource(DataSetChanges); 
                        bingds.Merge(DataSetChanges); 
                        bingds.AcceptChanges(); 
                    } 
                    catch (System.Exception eUpdate) 
                    { 
                   
                        // Add your error handling code here. 
                        throw eUpdate; 
                    } 
                    // Add your code to check the returned dataset for any errors that may have been 
                    // pushed into the row object's error. 
                }         } 
            public void UpdateDataSource(DataSet ChangedRows) 
            { 
                 
                try 
                { 
                    // The data source only needs to be updated if there are changes pending. 
                    if ((ChangedRows != null)) 
                    { 
                        // Open the connection. 
                        con.Open(); 
                         
                        // Attempt to update the data source. 
                        bingda.Update(ChangedRows);//每次是在这里跳到catch 里面的 
                        MessageBox.Show("看下能不能执行到这步"); 
                    } 
                } 
                catch (System.Exception updateException) 
                { 
             
                    // Add your error handling code here. 
                    throw updateException; 
                } 
                finally 
                { 
                    // Close the connection whether or not the exception was thrown. 
                    con.Close(); 
                }         } 
      

  2.   

    datatable tb=ds.table[“表名”];
    类似这样 建个datatable然后更新里面的row 试试
      

  3.   

    哎,哥哥些,别怕麻烦,主要是看我点更新键后激发的那个事件,我注明了的,前面的是绑定到明细框中的过程还有我把、 bingda.Update(ChangedRows);改成  bingda.Update(ChangedRows,"View_Assets");以后,它会抛出:“当传递具有已修改行的 DataRow 集合时,更新要求有效的 UpdateCommand。”异常
      

  4.   


    我没有见到你设置updatecommand啊 ?参考这个//
    //创建数据库链接对象;
    //
    SqlConnection conn = new SqlConnection(Config.ConnectionString); // Config.ConnectionString 为数据库连接语句
    //
    // 创建 SqlDataAdapter,并与sql 添加, 删除, 修改语句绑定;
    //
    SqlDataAdapter sda = new SqlDataAdapter();
    SqlCommand cmdSelect = new SqlCommand(User.dbUserSelect, conn);
    SqlCommand cmdInsert = new SqlCommand(User.dbUserInsert, conn);
    SqlCommand cmdDelete = new SqlCommand(User.dbUserDelete, conn);
    SqlCommand cmdUpdate = new SqlCommand(User.dbUserUpdate, conn);
    sda.SelectCommand = cmdSelect;
    sda.DeleteCommand = cmdDelete;
    sda.InsertCommand = cmdInsert;
    sda.UpdateCommand = cmdUpdate;
    //
    //插入 SQL 过程参数;
    //
    sda.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar, 50, "name");
    sda.InsertCommand.Parameters.Add("@code", SqlDbType.NVarChar, 20, "code");
    sda.InsertCommand.Parameters.Add("@phone", SqlDbType.NVarChar, 20, "phone");
    sda.InsertCommand.Parameters.Add("@email", SqlDbType.NVarChar, 30, "email");
    sda.InsertCommand.Parameters.Add("@address", SqlDbType.NVarChar, 50, "address");
    SqlParameter spInsert = sda.InsertCommand.Parameters.Add("@id", SqlDbType.Int, 4);
    spInsert.SourceColumn = "id";
    spInsert.SourceVersion = DataRowVersion.Original;
    //
    //修改 SQL 过程参数;
    //
    SqlParameter spUpdate1 = sda.UpdateCommand.Parameters.Add("@name", SqlDbType.NVarChar, 50);
    spUpdate1.SourceColumn = "name";
    SqlParameter spUpdate2 = sda.UpdateCommand.Parameters.Add("@code", SqlDbType.NVarChar, 20);
    spUpdate2.SourceColumn = "code";
    SqlParameter spUpdate3 = sda.UpdateCommand.Parameters.Add("@phone", SqlDbType.NVarChar, 20);
    spUpdate3.SourceColumn = "phone";
    SqlParameter spUpdate4 = sda.UpdateCommand.Parameters.Add("@email", SqlDbType.NVarChar, 30);
    spUpdate4.SourceColumn = "email";
    SqlParameter spUpdate5 = sda.UpdateCommand.Parameters.Add("@address", SqlDbType.NVarChar, 50);
    spUpdate5.SourceColumn = "address";
    SqlParameter spUpdate6 = sda.UpdateCommand.Parameters.Add("@id", SqlDbType.Int, 4);
    spUpdate6.SourceColumn = "id";
    spUpdate6.SourceVersion = DataRowVersion.Original;
    //
    // 删除 SQL 过程参数;
    //
    SqlParameter spDelete = sda.DeleteCommand.Parameters.Add("@id", SqlDbType.Int, 4);
    spDelete.SourceColumn = "id";
    spDelete.SourceVersion = DataRowVersion.Original;
    try
    {
    conn.Open();
    sda.Update(ds, "User");
    conn.Close();
    }
    catch(SqlException se)
    {
    MessageBox.Show(se.Message);
    }
    }
      

  5.   

    楼主,你这个方法是使用强类型的DataSet做了数据实体,这是需要制定相应的command的,如selectcommand,updatecommand,deletecommand.
      

  6.   

    楼上的是正解,后来才发现我是自己定义的sqldataapter,里面根本就没有updatacommand