日期:2014-05-17  浏览次数:20999 次

未将对象引用设置到对象的实例。是怎么回事?
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("已成功更新记录&quo

相关资料更多>