C#,用户代码未处理NullReferenceException?高手帮忙!

jjyy2272635627 2012-01-26 08:03:23
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace 车库管理系统
{
public partial class Form1 : Form
{
private DataSet dsnew;
private OleDbCommand command = new OleDbCommand();
private OleDbConnection connection;
private OleDbDataAdapter adapter;
private DataTable thetable = new DataTable();
public Form1()
{
InitializeComponent();
}
public int a = 0;

private void 信息BindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.信息BindingSource.EndEdit();
this.信息TableAdapter.Update(this.userDataSet.信息);

}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“userDataSet.信息”中。您可以根据需要移动或移除它。
this.信息TableAdapter.Fill(this.userDataSet.信息);
}

private void pictureBox1_Click(object sender, EventArgs e)
{
this.textBox8.Text = "请输入停车卡号关键字";
this.textBox8.Focus();
textBox8.SelectAll();
a = 1;

}
private void pictureBox2_Click(object sender, EventArgs e)
{
this.textBox8.Text = "请输入车主姓名关键字";
a = 2;
this.textBox8.Focus();
textBox8.SelectAll();

}


private void pictureBox5_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(); //返回
f2.Show();
this.Hide();
}

private void button2_Click(object sender, EventArgs e)
{
if (a == 0)
{
MessageBox.Show("请选择查询条件!");
textBox8.Focus();
textBox8.SelectAll();
}


if (a == 1)
{
string con = "Select * from 信息 where 停车卡号 like '%" + textBox8.Text + "%'";
connection = new OleDbConnection();
connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\车库管理系统\\user.mdb";
connection.Open();
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = con;
adapter = new OleDbDataAdapter("Select * From 信息", connection);
adapter.SelectCommand = command;
dsnew = new DataSet();
adapter.Fill(dsnew, "user");
thetable = dsnew.Tables["user"];
信息DataGridView.DataSource = thetable;
}
if (a == 2)
{
string con = "Select * from 信息 where 车主姓名 like '%" + textBox8.Text + "%'";
connection = new OleDbConnection();
connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\车库管理系统\\user.mdb";
connection.Open();
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = con;
adapter = new OleDbDataAdapter("Select * From 信息", connection);
adapter.SelectCommand = command;
dsnew = new DataSet();
adapter.Fill(dsnew, "user");
thetable = dsnew.Tables["user"];
信息DataGridView.DataSource = thetable;
}

}

private void button3_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.Show();
this.Hide();
}

private void button4_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你确定删除吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
信息DataGridView.Rows.RemoveAt(信息DataGridView.SelectedRows[0].Index);
else
{
Form1 f1 = new Form1();
f1.Show();
this.Hide();
}
}

private void 信息DataGridView_CurrentCellChanged(object sender, EventArgs e)
{
//信息DataGridView内容显示到textbox里,可是加入这个事件之后,进行查询、删除的操作就无法调试通过了,提示(this.textBox2.Text = this.信息DataGridView.CurrentRow.Cells[0].Value.ToString(); “未将对 象引用设置到对象的实例”。)

this.textBox2.Text = this.信息DataGridView.CurrentRow.Cells[0].Value.ToString();
this.textBox3.Text = this.信息DataGridView.CurrentRow.Cells[1].Value.ToString();
this.textBox4.Text = this.信息DataGridView.CurrentRow.Cells[2].Value.ToString();
this.textBox5.Text = this.信息DataGridView.CurrentRow.Cells[3].Value.ToString();
this.textBox6.Text = this.信息DataGridView.CurrentRow.Cells[4].Value.ToString();
this.textBox7.Text = this.信息DataGridView.CurrentRow.Cells[5].Value.ToString();
}





}
}
...全文
304 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
mizuho_2006 2012-01-26
  • 打赏
  • 举报
回复
应该是信息DataGridView 中没有数据,取不出来。
加一个判断,如果DataGridView的行数大于0再进行操作。

110,539

社区成员

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

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

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