| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 582 人关注过本帖
标题:诚心来请教问题.谢谢.
只看楼主 加入收藏
uukkcc
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2007-7-24
收藏
 问题点数:0 回复次数:9 
诚心来请教问题.谢谢.

运行以下代码后提示:未将对象的引用应用到对象的实例. 实在不知道哪里错了.请大家指教!
谢谢.
代码如下:

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();
}
}

}
}

搜索更多相关主题的帖子: 诚心 
2007-11-19 00:05
guoxhvip
Rank: 8Rank: 8
来 自:聖西羅南看臺
等 级:贵宾
威 望:44
帖 子:4052
专家分:135
注 册:2006-10-8
收藏
得分:0 
LZ可不可以把你报异常的地方标名一下 那么大段代码确实``````````

愛生活 && 愛編程
2007-11-19 01:42
pacocai
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1583
专家分:0
注 册:2007-3-12
收藏
得分:0 
guoxhvip 加下天使的群。

浮生若梦天边月,醉死如酒水中星。红楼一梦千人叹,岂让万夫空做贱。博客:http://hi.baidu.com/rxvip
2007-11-19 07:03
uukkcc
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2007-7-24
收藏
得分:0 
编译没有错误.只是执行的时候才有错误提示.
哎,我也郁闷的不行了.想知道哪里错了都不行.....
2007-11-19 09:29
farderce
Rank: 1
来 自:上海
等 级:新手上路
帖 子:146
专家分:3
注 册:2006-1-11
收藏
得分:0 
System.Diagnostics.Process.start("D:a.wav");
在一个button上调用鼠标移上事件然后触发上面那句
我不想让它弹出播放器,怎,么做哦 (C#)
2007-11-19 10:56
andey
Rank: 2
等 级:新手上路
威 望:4
帖 子:938
专家分:0
注 册:2007-7-18
收藏
得分:0 
以下是引用uukkcc在2007-11-19 9:29:09的发言:
编译没有错误.只是执行的时候才有错误提示.
哎,我也郁闷的不行了.想知道哪里错了都不行.....

晕...只是执行的时候有错误提示???
那么..这个提示是出现在哪里呢?
总不能没有地方吧?


msdn == 葵花宝典!!!
QQ:122768959
2007-11-19 11:07
uukkcc
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2007-7-24
收藏
得分:0 
真的没地方.我说的是实话.就抱出个异常来.
怎么办啊?
2007-11-19 11:17
andey
Rank: 2
等 级:新手上路
威 望:4
帖 子:938
专家分:0
注 册:2007-7-18
收藏
得分:0 
那你总知道是执行到哪一步报的异常吧?
不是一运行就报异常吧??

msdn == 葵花宝典!!!
QQ:122768959
2007-11-19 11:19
pacocai
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1583
专家分:0
注 册:2007-3-12
收藏
得分:0 
以下是引用uukkcc在2007-11-19 9:29:09的发言:
编译没有错误.只是执行的时候才有错误提示.
哎,我也郁闷的不行了.想知道哪里错了都不行.....

你执行的时候有错误提求那会抛出异常的,你再认真的看看异常的提示些什么。这样才是解决问题的根本。编译没有错误不能说明什么的。


浮生若梦天边月,醉死如酒水中星。红楼一梦千人叹,岂让万夫空做贱。博客:http://hi.baidu.com/rxvip
2007-11-19 13:13
pacocai
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1583
专家分:0
注 册:2007-3-12
收藏
得分:0 
以下是引用farderce在2007-11-19 10:56:51的发言:
System.Diagnostics.Process.start("D:a.wav");
在一个button上调用鼠标移上事件然后触发上面那句
我不想让它弹出播放器,怎,么做哦 (C#)

直接使用MediaPlayer控件或是API又或是用Directx进行播放就可以了。


浮生若梦天边月,醉死如酒水中星。红楼一梦千人叹,岂让万夫空做贱。博客:http://hi.baidu.com/rxvip
2007-11-19 13:14
快速回复:诚心来请教问题.谢谢.
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025798 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved