求助!从未对字段“XXX”赋值,字段将一直保持其默认值 null

eddison2009 2010-11-14 06:22:33
BrowseDepart browsedepart;
private void BrowseDepart_Closed(object sender, System.EventArgs e)
{
if (browsedepart!= null)
browsedepart.Close();
oleDbConnection1.Close();
}
从未对字段“push.BrowseDepart.browsedepart”赋值,字段将一直保持其默认值 null

源码如下:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
namespace push
{
public partial class BrowseDepart : Form
{
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public BrowseDepart()
{
//
// Windows 窗体设计器支持所必需的
//
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.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// button3
//
this.button3.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button3.ForeColor = System.Drawing.SystemColors.ControlText;
this.button3.Location = new System.Drawing.Point(152, 248);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 24);
this.button3.TabIndex = 12;
this.button3.Text = "删除";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button2.ForeColor = System.Drawing.SystemColors.ControlText;
this.button2.Location = new System.Drawing.Point(32, 248);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 24);
this.button2.TabIndex = 11;
this.button2.Text = "修改";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.ForeColor = System.Drawing.SystemColors.ControlText;
this.button1.Location = new System.Drawing.Point(280, 248);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 10;
this.button1.Text = "退出";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGrid1
//
this.dataGrid1.AllowSorting = false;
this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
this.dataGrid1.CaptionVisible = false;
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.PreferredColumnWidth = 110;
this.dataGrid1.PreferredRowHeight = 30;
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(384, 224);
this.dataGrid1.TabIndex = 9;
this.dataGrid1.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = "data source = C:\\Documents and Settings\\Administrator\\桌面\\zob\\zob\\push\\push\\bin\\De" +
"bug\\persons.mdb;Jet OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;";
//
// BrowseDepart
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(392, 278);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "BrowseDepart";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "浏览部门信息";
this.Load += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
this.Closed += new System.EventHandler(this.BrowseDepart_Closed);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

DataSet ds;
private void BrowseDepart_Load(object sender, System.EventArgs e)
{
oleDbConnection1.Open();
string sql = "select DID as 编号,Dname as 部门名称,Dleader as 部门领导,Remark as 描述 from depart";
OleDbDataAdapter adp = new OleDbDataAdapter(sql, oleDbConnection1);
ds = new DataSet();
ds.Clear();
adp.Fill(ds, "depart");
dataGrid1.DataSource = ds.Tables[0].DefaultView;
}
//当DataGrid1中当前单元格发生变化时,将对应的部门名称显示在DataGrid1标题中
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
oleDbConnection1.Open();
string sql = "select DID as 编号,Dname as 部门名称,Dleader as 部门领导,Remark as 描述 from depart";
OleDbDataAdapter adp = new OleDbDataAdapter(sql, oleDbConnection1);
ds = new DataSet();
ds.Clear();
adp.Fill(ds, "depart");
dataGrid1.DataSource = ds.Tables[0].DefaultView;
dataGrid1.CaptionText = dataGrid1[dataGrid1.CurrentRowIndex, 1].ToString();

}

BrowseDepart browsedepart;
private void BrowseDepart_Closed(object sender, System.EventArgs e)
{
if (browsedepart!= null)
browsedepart.Close();
oleDbConnection1.Close();
}

private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}

ModifyDepart modifydepart;
private void button2_Click(object sender, System.EventArgs e)
{
if (dataGrid1[dataGrid1.CurrentCell] != null)
{
modifydepart = new ModifyDepart();
modifydepart.Tag = ds.Tables[0].Rows[dataGrid1.CurrentRowIndex][0].ToString().Trim();
modifydepart.textBox1.Text = ds.Tables[0].Rows[dataGrid1.CurrentRowIndex][1].ToString().Trim();
modifydepart.textBox2.Text = ds.Tables[0].Rows[dataGrid1.CurrentRowIndex][2].ToString().Trim();
modifydepart.textBox3.Text = ds.Tables[0].Rows[dataGrid1.CurrentRowIndex][3].ToString().Trim();
modifydepart.ShowDialog();
}
}

private void button3_Click(object sender, System.EventArgs e)
{
if (dataGrid1[dataGrid1.CurrentCell] != null)
{
string sql = "select Dname from depart where DID=" + ds.Tables["depart"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim() + " and DID not in (select distinct depart.DID from person inner join depart on person.DID=depart.DID)";
OleDbCommand cmd = new OleDbCommand(sql, oleDbConnection1);
OleDbDataReader dr;
dr = cmd.ExecuteReader();
if (!dr.Read())
{
MessageBox.Show("删除部门'" + ds.Tables["depart"].Rows[dataGrid1.CurrentCell.RowNumber][1].ToString().Trim() + "'失败,请先删除与此工种相关的员工", "提示");
dr.Close();
}
else
{
dr.Close();
sql = "delete * from depart where DID not in (select distinct DID from person) and DID=" + ds.Tables["depart"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim();

cmd.CommandText = sql;
cmd.ExecuteNonQuery();
MessageBox.Show("删除部门'" + ds.Tables["depart"].Rows[dataGrid1.CurrentCell.RowNumber][1].ToString().Trim() + "'成功", "提示");
}
}
}

private void dataGrid1_Navigate(object sender, NavigateEventArgs ne)
{

}
}
}
...全文
2161 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ds252743641 2010-11-14
  • 打赏
  • 举报
回复
BrowseDepart browsedepart = new BrowseDepart()
ds252743641 2010-11-14
  • 打赏
  • 举报
回复
BrowseDepart browsedepart;
private void BrowseDepart_Closed(object sender, System.EventArgs e)
{
if (browsedepart!= null)
browsedepart.Close();
oleDbConnection1.Close();
}



browsedepart何时赋的值啊?
lvfeng19806001 2010-11-14
  • 打赏
  • 举报
回复
你这个browsedepart既没有实例化也没用赋值,当然永远都是null了,你这句if (browsedepart!= null)browsedepart.Close();有意义吗?
yangyi_33855 2010-11-14
  • 打赏
  • 举报
回复
LZ,你单步运行一下先,太多了,看不过来啊~

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧