using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace stusystem
{
    public partial class AddStuForm : Form
    {
        String Name1;
        String Number;
        String Clas;
        String Yuanxi;
        String Math;
        String English;
        String Wuli;
        String Soft;
        OleDbDataAdapter  StudentAdapter;
        DataTable StudentTable;                public AddStuForm(ref DataGridView dataGridView , ref OleDbDataAdapter  studentAdapter , ref DataTable  studentTable)
        {
            InitializeComponent();
            this.StudentAdapter = studentAdapter;
            this.StudentTable = studentTable;
        }
        public AddStuForm()
        {
            InitializeComponent();
        }        private void button2_Click(object sender, EventArgs e)
        {
            Name1 = textBox1.Text;
            Number = textBox2.Text;
            Clas = textBox3.Text;
            Yuanxi = textBox4.Text;
            Math = textBox5.Text;
            English = textBox7.Text;
            Wuli = textBox6.Text;
            Soft = textBox8.Text;
            
            if(true)
            {
                this.Validate();
                DataRow row = StudentTable.NewRow();  //未将对象引用设置到对象的实例
                row["姓名"] = Name1;
                row["学号"] = Number;
                row["班级"] = Clas;
                row["院系"] = Yuanxi;
                row["微积分"] = Math;
                row["大学英语"] = English;
                row["大学物理"] = Wuli;
                row["软件课设"] = Soft;
                try
                {
                    StudentAdapter.Update(StudentTable);
                }
                catch(OleDbException err)
                {
                    MessageBox.Show(err.Message , "添加失败");
                }
            }
        }
    }
}
StudentTable指向Null?已经给了值,为何不存在?

解决方案 »

  1.   

    StudentTable—>NULLNULL.NewRow() 未将对象引用设置到对象的实例
      

  2.   

    检查参数studentTable传进来的应该为NULL
      

  3.   

    StudentTable给的是引用
    1 本来传递的就是NULL
    2 原始对象引用被设置成NULL
      

  4.   

    2 原始对象引用被设置成NULL
    说错了
      

  5.   

    public AddStuForm(ref DataGridView dataGridView , ref OleDbDataAdapter studentAdapter , ref DataTable studentTable)
      {
      InitializeComponent();
      this.StudentAdapter = studentAdapter;
      this.StudentTable = studentTable;
      }
    这个构造函数到底调用了没有?
      

  6.   


    看看你给 this.StudentAdapter  赋的是不是空值。哈哈。
      

  7.   

    在StudentTable处打个断点,或直接跟踪参数调试
      

  8.   

    studenttable 打了断点,是NULL,程序被调用但没有操作……数据没有传进来……加MAIN1也没有用,求解?