运行以下代码后提示:未将对象的引用应用到对象的实例. 实在不知道哪里错了.请大家指教!
谢谢.
代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace student
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.DataGrid dataGrid2;
private System.Windows.Forms.DataGrid dataGrid3;
private System.Windows.Forms.DataGrid dataGrid4;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItem10;
private ListBox lstclassmsg;
private ListBox lstlessonmsg;
private IContainer components;
private SqlDataAdapter adapter;
private DataSet ds;
public Form1()
{
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent() //以下代码自动生成可略过
{
this.components = new System.ComponentModel.Container();
this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.lstclassmsg = new System.Windows.Forms.ListBox();
this.dataGrid3 = new System.Windows.Forms.DataGrid();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.lstlessonmsg = new System.Windows.Forms.ListBox();
this.dataGrid4 = new System.Windows.Forms.DataGrid();
this.dataGrid2 = new System.Windows.Forms.DataGrid();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGrid3)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGrid4)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4,
this.menuItem5,
this.menuItem6,
this.menuItem7,
this.menuItem8,
this.menuItem9});
this.menuItem1.Text = "文件(&F)";
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Text = "新建班级(&C)";
//
// menuItem5
//
this.menuItem5.Index = 1;
this.menuItem5.Text = "新建学生档案(&N)";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem6
//
this.menuItem6.Index = 2;
this.menuItem6.Text = "科目管理(&S)";
//
// menuItem7
//
this.menuItem7.Index = 3;
this.menuItem7.Text = "建立课程(&R)";
//
// menuItem8
//
this.menuItem8.Index = 4;
this.menuItem8.Text = "-";
//
// menuItem9
//
this.menuItem9.Index = 5;
this.menuItem9.Text = "退出(&Q)";
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "视图(&V)";
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem10});
this.menuItem3.Text = "帮助(&H)";
//
// menuItem10
//
this.menuItem10.Index = 0;
this.menuItem10.Text = "关于学员管理系统(&A)";
this.menuItem10.Click += new System.EventHandler(this.label2_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.lstclassmsg);
this.groupBox1.Controls.Add(this.dataGrid3);
this.groupBox1.Controls.Add(this.dataGrid1);
this.groupBox1.Location = new System.Drawing.Point(0, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(792, 280);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
//
// lstclassmsg
//
this.lstclassmsg.FormattingEnabled = true;
this.lstclassmsg.ItemHeight = 12;
this.lstclassmsg.Location = new System.Drawing.Point(6, 36);
this.lstclassmsg.Name = "lstclassmsg";
this.lstclassmsg.Size = new System.Drawing.Size(185, 244);
this.lstclassmsg.TabIndex = 5;
this.lstclassmsg.SelectedIndexChanged += new System.EventHandler(this.lstclassmsg_SelectedIndexChanged);
//
// dataGrid3
//
this.dataGrid3.CaptionText = "班成绩";
this.dataGrid3.DataMember = "";
this.dataGrid3.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid3.Location = new System.Drawing.Point(3, 8);
this.dataGrid3.Name = "dataGrid3";
this.dataGrid3.Size = new System.Drawing.Size(189, 272);
this.dataGrid3.TabIndex = 4;
//
// dataGrid1
//
this.dataGrid1.CaptionText = "学生信息";
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(192, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(600, 272);
this.dataGrid1.TabIndex = 3;
this.dataGrid1.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.lstlessonmsg);
this.groupBox2.Controls.Add(this.dataGrid4);
this.groupBox2.Controls.Add(this.dataGrid2);
this.groupBox2.Location = new System.Drawing.Point(0, 280);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(792, 248);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
//
// lstlessonmsg
//
this.lstlessonmsg.FormattingEnabled = true;
this.lstlessonmsg.ItemHeight = 12;
this.lstlessonmsg.Location = new System.Drawing.Point(0, 31);
this.lstlessonmsg.Name = "lstlessonmsg";
this.lstlessonmsg.Size = new System.Drawing.Size(192, 208);
this.lstlessonmsg.TabIndex = 4;
// this.lstlessonmsg.SelectedIndexChanged += new System.EventHandler(this.lstlessonmsg_SelectedIndexChanged);
//
// dataGrid4
//
this.dataGrid4.CaptionText = "课程信息";
this.dataGrid4.DataMember = "";
this.dataGrid4.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid4.Location = new System.Drawing.Point(3, 8);
this.dataGrid4.Name = "dataGrid4";
this.dataGrid4.Size = new System.Drawing.Size(192, 240);
this.dataGrid4.TabIndex = 3;
//
// dataGrid2
//
this.dataGrid2.CaptionText = "成绩信息";
this.dataGrid2.DataMember = "";
this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid2.Location = new System.Drawing.Point(192, 8);
this.dataGrid2.Name = "dataGrid2";
this.dataGrid2.Size = new System.Drawing.Size(600, 240);
this.dataGrid2.TabIndex = 2;
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 529);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(792, 24);
this.statusBar1.TabIndex = 2;
this.statusBar1.Text = "statusBar1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(792, 553);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "学员管理系统";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid3)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid4)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void label2_Click(object sender, System.EventArgs e)
{
//
AboutForm af=new AboutForm();
af.Show();
}
private void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
{
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
//
StudentArchivesForm saf=new StudentArchivesForm();
saf.Show();
}
private void Form1_Load(object sender, EventArgs e) //手工写入代码=====================
{
this.lstclassmsg.Items.Add("应用电子系一班");
this.lstclassmsg.Items.Add("外语系");
this.lstclassmsg.Items.Add("计算机系");
this.lstlessonmsg.Items.Add("Winform开发");
this.lstlessonmsg.Items.Add(".NET编程");
this.lstlessonmsg.Items.Add("ASP.NET编程");
}
private void lstclassmsg_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.lstclassmsg.SelectedIndex == 0)//如果第一列被选中的话执行如下语句
{
string word = "select * from DZ";
DB db = new DB(); //实例化调用
Model md = new Model();
db.GetConnection();
md.GetInstance();
db.GetDataAdapter(word);
if (!ds.Tables.Contains("DZ"))//如果包含"DZ"表
{
adapter.Fill(ds,"DZ");
}
this.dataGrid1.DataSource = ds;
db.Close();
md.Dispose();
}
}
}
}
以下是DB层代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace student
{
class DB
{
private SqlConnection con;
private SqlDataAdapter adapter;
public SqlConnection GetConnection()
{
if (con == null)
{
con = new SqlConnection("server=113512\\MINE;uid=sa;pwd=sa;database=test");
con.Open();
}
return con;
}
public SqlDataAdapter GetDataAdapter(string selectText)
{
if (adapter == null)
{
adapter = new SqlDataAdapter(selectText, con);
}
return adapter;
}
public SqlDataAdapter GetDataAdapter(string selectText, SqlConnection conn)
{
if (adapter == null)
{
adapter = new SqlDataAdapter(selectText, conn);
}
return adapter;
}
public void Close()
{
if (con != null)
{
con.Close();
}
}
}
}
以下是model层代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace student
{
class Model
{
DataSet ds;
public Model()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public DataSet GetInstance()
{
if (ds == null)
{
ds = new DataSet();
}
return ds;
}
public void Dispose()
{
if (ds != null)
{
ds = null;
ds.Dispose();
}
}
}
}