读书人

未将对象引用设置到对象的实例。是咋回

发布时间: 2012-09-14 23:00:49 作者: rapoo

未将对象引用设置到对象的实例。是怎么回事?
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.SqlClient;


namespace WindowsFormsApplication7
{
public partial class frmFilght : Form
{
private SqlConnection objSqlConnection;
private SqlCommand objSqlCommand;


// 类变量
private string insCmd;
private string modCmd;
private string delCmd;

public frmFilght()
{
InitializeComponent();
}

private void frmFkight_Load(object sender, EventArgs e)
{
objSqlConnection = new SqlConnection("Data Source=CHINA-582071202;Initial Catalog=houses;user ID=sa;Password=sa123");

this.comSeats.Items.Clear();
this.comSeats.Items.Add("100");
this.comSeats.Items.Add("150");
this.comSeats.Items.Add("200");
//this.btnModify.Enabled = false;
//this.btnDelete.Enabled = false;
}

private void btnAdd_Click(object sender, EventArgs e)
{

insCmd = "insert into users values ('" + this.txtFlightCode.Text + "', '" + this.txtAirline.Text + "','" + this.txtDestinagtion.Text + "', '" + this.txtSource.Text + "','" + this.txtArrival.Text + "', '" + this.txtDeparture.Text + "', '" + this.comSeats.SelectedItem.ToString() + ")";
// 初始化 command 对象
objSqlCommand = new SqlCommand(insCmd, objSqlConnection);
try
{
// 打开连接
objSqlConnection.Open();
// 执行插入语句
objSqlCommand.ExecuteNonQuery();
MessageBox.Show("已成功添加记录");
// 启用和禁用按钮
this.btnModify.Enabled = true;
this.btnDelete.Enabled = true;
this.btnAdd.Enabled = false;
this.txtFlightCode.Enabled = false;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// 关闭连接
objSqlConnection.Close();
}



}


private void btnModify_Click(object sender, System.EventArgs e)
{
modCmd = "update users set Airline ='" + this.txtAirline.Text + "', Destination ='" + this.txtDestinagtion.Text + "',Source = '" + this.txtSource.Text + "', Arrival = '" + this.txtArrival.Text + "', Departure = '" + this.txtDeparture.Text + "', TotalSeats = " + this.comSeats.SelectedItem.ToString() + " where FlightCode like '" + this.txtFlightCode.Text + "'";
objSqlCommand = new SqlCommand(modCmd, objSqlConnection);
try
{
objSqlConnection.Open();
objSqlCommand.ExecuteNonQuery();
MessageBox.Show("已成功更新记录");
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}


finally
{
objSqlConnection.Close();
}
}

private void btnDelete_Click(object sender, System.EventArgs e)
{
delCmd = "delete from [users] where FlightCode like '" + this.txtFlightCode.Text + "'";
MessageBox.Show(delCmd);
// 初始化 command 对象
objSqlCommand = new SqlCommand(delCmd, objSqlConnection);


// 初始化 DialogResult
DialogResult objDialogResult =
MessageBox.Show("您确定要删除当前记录吗?", "确认", MessageBoxButtons.YesNo);
// 确定用户的响应
if (objDialogResult.Equals(DialogResult.Yes))
{
objSqlConnection.Open();
objSqlCommand.ExecuteNonQuery();
MessageBox.Show("已删除记录");
btnModify.Enabled = false;
}



}

private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}


[解决办法]
语句部分有问题,字段在数据库中的表中不存在或者本身有问题,复制粘贴到plsql中,赋予相应的值后执行,就会报错,找到即可;
eg1:update test1 set c1='a';如果test1中不存在c1,就会报这个错误;
eg2:update test1 set c1='a' wherec2='b'; 也会报这个错误;
本人菜鸟too,根据以往经验得出的结论,仅供参考;

读书人网 >C#

热点推荐