SqlDataAdapter sda = null;
        DataSet ds = null;
        SqlCommandBuilder scb = new SqlCommandBuilder();
        private void button1_Click(object sender, EventArgs e)
        {
            dbhlpe.conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from FamilyDB where famName ='" + this.text.Trim() + "' and exists(select * from FamilyDB where famdate between '" + Convert.ToDateTime(this.dateTimePicker1.Text) + "' and '" + Convert.ToDateTime(this.dateTimePicker2.Text) + "')", dbhlpe.conn);
            DataSet ds = new DataSet();
            scb = new SqlCommandBuilder(sda);
            sda.Fill(ds);
            dataGridView1.DataSource = ds;
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
            dbhlpe.conn.Close();
        }        private void update_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = this.text.Trim();
        }        private void button2_Click(object sender, EventArgs e)
        {
           SqlCommandBuilder scb = new SqlCommandBuilder(sda);
            int count =sda.Update(ds);
            if (count > 0)
            {
                MessageBox.Show("修改数据成功。");
            }
   更新的时候提示 未将对象引用设置到对象的实例。
          private void button3_Click(object sender, EventArgs e)
        {
            int index = dataGridView1.SelectedRows[0].Index;
            ds.Tables[0].Rows[index].Delete();
        }
  还有这个删除,我是按照老师的方法写的,删除的时候也出错,麻烦大大看看。

解决方案 »

  1.   

    private void button3_Click(object sender, EventArgs e) 
            { 
                int index = dataGridView1.SelectedRows[0].Index; 
                ds.Tables[0].Rows[e.index].Delete(); 
            } 这个只是内存中的删除,数据那边是没有删除的。
      

  2.   

    private void button2_Click(object sender, EventArgs e) 
            { 
              SqlCommandBuilder scb = new SqlCommandBuilder(sda); 
                int count =sda.Update(ds); 
                if (count > 0) 
                { 
                    MessageBox.Show("修改数据成功。"); 
                } 
    改成这个样子。private void button2_Click(object sender, EventArgs e) 
            { 
              SqlCommandBuilder scb = new SqlCommandBuilder(sda); 
                int count =sda.Update(dataGridView1); 
                if (count > 0) 
                { 
                    MessageBox.Show("修改数据成功。"); 
                } 
      

  3.   

    你犯的错误,是没有弄清楚定义的范围。
    比如你定义的DataSet ds = new DataSet(); 
    仅在这两个大括号之间有用。
      

  4.   

    建议你 从新学习一下 Ado.NEt  http://blog.sina.com.cn/s/blog_5ea9354f0100d9eq.html
    sda.Update方法  有问题不能更新数据库
      

  5.   

    private void button2_Click(object sender, EventArgs e) 
            { 
              SqlCommandBuilder scb = new SqlCommandBuilder(sda); 
                int count =sda.Update(dataGridView1); 
                if (count > 0) 
                { 
                    MessageBox.Show("修改数据成功。"); 
                } 改成这样语法报错了
    错误 1 与“System.Data.Common.DbDataAdapter.Update(System.Data.DataRow[])”最匹配的重载方法具有一些无效参数 E:\资料\未完成的\Familyfunciton\Familyfunciton\update.cs 43 24 Familyfunciton
      

  6.   

    你这是没法改的。你的sda,ds都没有定义。
      

  7.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace Familyfunciton
    {
        public partial class update : Form
        {
            string text;
            public update(string s)
            {
                this.text = s;
                InitializeComponent();
            }
            SqlDataAdapter sda = null;
            DataSet ds = null;
            SqlCommandBuilder scb = new SqlCommandBuilder();
            private void button1_Click(object sender, EventArgs e)
            {
                dbhlpe.conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter("select * from FamilyDB where famName ='" + this.text.Trim() + "' and exists(select * from FamilyDB where famdate between '" + Convert.ToDateTime(this.dateTimePicker1.Text) + "' and '" + Convert.ToDateTime(this.dateTimePicker2.Text) + "')", dbhlpe.conn);
                DataSet ds = new DataSet();
                scb = new SqlCommandBuilder(sda);
                sda.Fill(ds);
                dataGridView1.DataSource = ds;
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
                dbhlpe.conn.Close();
            }        private void update_Load(object sender, EventArgs e)
            {
                toolStripStatusLabel1.Text = this.text.Trim();
            }        private void button2_Click(object sender, EventArgs e)
            {
               SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                int count =sda.Update(ds);
                if (count > 0)
                {
                    MessageBox.Show("修改数据成功。");
                }
                       }        private void button3_Click(object sender, EventArgs e)
            {
                int index = dataGridView1.SelectedRows[0].Index;
                ds.Tables[0].Rows[index].Delete();
            }
        }
    }
      

  8.   

            private void button2_Click(object sender, EventArgs e) 
            { 
              SqlCommandBuilder scb = new SqlCommandBuilder(sda); 
                int count =sda.Update(ds); 
                if (count > 0) 
                { 
                    MessageBox.Show("修改数据成功。"); 
                } 
    这句里的
    SqlCommandBuilder scb = new SqlCommandBuilder(sda); 
    去掉试试,你同一个sda重复SqlCommandBuilder了两次
      

  9.   

            private void button2_Click(object sender, EventArgs e) 
            { 
              SqlCommandBuilder scb = new SqlCommandBuilder(sda); 
                int count =sda.Update(ds); 
                if (count > 0) 
                { 
                    MessageBox.Show("修改数据成功。"); 
                } 告诉你了,红色的地方没有定义。
    你可以在前面加个this
      

  10.   

    代码写得太不清晰了,我估计问题可能出现在sqlCommandBuilder身上.
    SqlDataAdapter sda;
    DataSet da;
    SqlCommandBuilder scb;
    private void button1_Click(object sender, EventArgs e) 
            { 
                
                ds = new DataSet(); 
                scb = new SqlCommandBuilder();
                scb.DataSource="数据服器";
                scb.InitialCatalog="数据库名";
                scb.IntegratedSecurity=true;
                dbhlpe.conn.SqlConnection(scb.ConnectionString);
                dbhlpe.conn.Open();
                sda = new SqlDataAdapter("...sql语句", dbhlpe.conn); 
                sda.fill(ds);
                dataGridView1.DataSource=ds.Tables["表名"];
                dbhlpe.conn.Close(); 
            }         private void button2_Click(object sender, EventArgs e) 
            { 
                sda.UpdateCommand=scb.UpdateCommand;
                int count =sda.Update(ds); 
                if (count > 0) 
                { 
                    MessageBox.Show("修改数据成功。"); 
                } 
            }          private void button3_Click(object sender, EventArgs e) 
            { 
                sda.DeleteCommand=scb.DeleteCommand;
                int index = dataGridView1.SelectedRows[0].Index; 
                ds.Tables[0].Rows[indext].Delete();  
        sda.Update(ds);
            }
    这样你试试看.
    没细看,可能有问题.
    方法有很多种,不能局限于老师所讲的.搞程序就怕在一棵树上吊死.
      

  11.   

    不用dbhlpe.conn.Open(); 和dbhlpe.conn.Close();
    正常这样更新是不需要上边这两句的,你去掉试试,可能就是这两句的关系