当前位置:编程学习 > C#/ASP.NET >>

求教c#,未将对象引用设置到对象的实例问题

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?已经给了值,为何不存在?
--------------------编程问答-------------------- StudentTable—>NULL

NULL.NewRow() 未将对象引用设置到对象的实例 --------------------编程问答--------------------

检查参数。。。studentTable

传进来的应该为NULL --------------------编程问答-------------------- StudentTable给的是引用
1 本来传递的就是NULL
2 原始对象引用被设置成NULL --------------------编程问答-------------------- StudentTable是不是空啊 --------------------编程问答-------------------- StudentTable未实例化吧! --------------------编程问答--------------------
引用 3 楼 ghost5216 的回复:
StudentTable给的是引用
1 本来传递的就是NULL
2 原始对象引用被设置成NULL

2 原始对象引用被设置成NULL
说错了 --------------------编程问答--------------------
引用楼主 asver001 的回复:
StudentTable指向Null?已经给了值,为何不存在?

你哪里给StudentTable赋值了? --------------------编程问答-------------------- 应该StudentTable未实例化 --------------------编程问答-------------------- 哪有这种写法,传参有问题。 --------------------编程问答--------------------
public AddStuForm(ref DataGridView dataGridView , ref OleDbDataAdapter studentAdapter , ref DataTable studentTable)
  {
  InitializeComponent();
  this.StudentAdapter = studentAdapter;
  this.StudentTable = studentTable;
  }


这个构造函数到底调用了没有? --------------------编程问答-------------------- 在出问题的代码那,给个判断

if()
{
//=====
}

你再看看有每问题 --------------------编程问答--------------------
引用 10 楼 buyong 的回复:
C# code
public AddStuForm(ref DataGridView dataGridView , ref OleDbDataAdapter studentAdapter , ref DataTable studentTable)
  {
  InitializeComponent();
  this.StudentAdapter = studentAdapter;
  ……


看看你给 this.StudentAdapter  赋的是不是空值。哈哈。 --------------------编程问答--------------------
StudentTable未实例化 --------------------编程问答-------------------- 实例化后,如果有实体的某个参数未赋值或为空,也会报这种错。 --------------------编程问答-------------------- 在StudentTable处打个断点,或直接跟踪参数调试 --------------------编程问答-------------------- studenttable 打了断点,是NULL,程序被调用但没有操作……数据没有传进来……加MAIN1也没有用,求解?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,