当它说Exception Unhandled时如何修复我的代码?

时间:2017-10-18 18:27:14

标签: c#

所以我编写了我的代码,但每次我尝试执行它时都会说"异常未处理的System.InvalidOperationException:' Fill:SelectCommand.Connection属性尚未初始化。'&# 34;它始终显示在da.Fill(dt);

的行上

请告诉我如何解决它

namespace FairyTailHRSolution
{
    public partial class Form1 : Form
    {
        SqlCommand cmd;
        SqlConnection con;
        SqlDataAdapter da;


        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            con=new SqlConnection(@"Data Source = LAPTOP-VHSGV41H\SQLEXPRESS; Initial Catalog = EmpDB; Integrated Security = True");
            con.Open();
            cmd = new SqlCommand("INSERT INTO FRYEMP (EmployeeID, EmployeeName, EmployeePosition, EmployeeSalary) VALUES (@EmployeeID, @EmployeeName, @EmployeePosition, @EmployeeSalary)", con);
            cmd.Parameters.Add("@EmployeeID", textBox1.Text);
            cmd.Parameters.Add("@EmployeeName", textBox2.Text);
            cmd.Parameters.Add("@EmployeePosition", textBox3.Text);
            cmd.Parameters.Add("@EmployeeSalary", textBox4.Text); 
            cmd.ExecuteNonQuery();


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void find_Click(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {


            if(comboBox1.Text == "EmployeeID")
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT EmployeeID, EmployeeName,EmployeePosition, EmployeeSalary FROM FRYEMP where EmployeeID like @employeeID", con);
                da.SelectCommand.Parameters.AddWithValue("@employeeID", "%" + textBox5.Text + "%");
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }

            else if (comboBox1.Text == "EmployeeName")
            {


                SqlDataAdapter da = new SqlDataAdapter("SELECT EmployeeID, EmployeeName,EmployeePosition, EmployeeSalary FROM FRYEMP where EmployeeName like @employeeName", con);
                da.SelectCommand.Parameters.AddWithValue("@employeeName", "%" + textBox5.Text + "%");
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }


        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {


        }
    }
}

4 个答案:

答案 0 :(得分:1)

处理连接对象的最佳做法是将它们存储在局部变量中并尽快处理它们。您无需担心开销和关闭连接;它们实际上是在池中管理的,而且非常有效。

您正在类级别存储连接,而不是正确处理连接。如果您将它存储在类级别,它可能会在按钮单击之间超时,并且它会占用整个资源。立即关闭或接通连接,这会将其返回到连接池。

要修复,请遵循以下这种模式:

namespace FairyTailHRSolution
{
    public partial class Form1 : Form
    {
        //Get rid of member variable for the connection. Add constant for connection string.
        private const string ConnectionString = @"Data Source = LAPTOP-VHSGV41H\SQLEXPRESS; Initial Catalog = EmpDB; Integrated Security = True";  

        private void button1_Click(object sender, EventArgs e)
        {
            //Use using and use a local variable for the connection
            using (var con=new SqlConnection(this.ConnectionString))
            {
                con.Open();
                var cmd = new SqlCommand("INSERT INTO FRYEMP (EmployeeID, EmployeeName, EmployeePosition, EmployeeSalary) VALUES (@EmployeeID, @EmployeeName, @EmployeePosition, @EmployeeSalary)", con);
                cmd.Parameters.Add("@EmployeeID", textBox1.Text);
                cmd.Parameters.Add("@EmployeeName", textBox2.Text);
                cmd.Parameters.Add("@EmployeePosition", textBox3.Text);
                cmd.Parameters.Add("@EmployeeSalary", textBox4.Text); 
                cmd.ExecuteNonQuery();
            }
        }


        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            if(comboBox1.Text == "EmployeeID")
            {
                //Create a new connection each time you need one
                using (var con = new SqlConnection(this.ConnectionString))
                {
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter("SELECT EmployeeID, EmployeeName,EmployeePosition, EmployeeSalary FROM FRYEMP where EmployeeID like @employeeID", con);
                    da.SelectCommand.Parameters.AddWithValue("@employeeID", "%" + textBox5.Text + "%");
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
            else if (comboBox1.Text == "EmployeeName")
            {
                using (var con = new SqlConnection(this.ConnectionString))
                {
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter("SELECT EmployeeID, EmployeeName,EmployeePosition, EmployeeSalary FROM FRYEMP where EmployeeName like @employeeName", con);
                    da.SelectCommand.Parameters.AddWithValue("@employeeName", "%" + textBox5.Text + "%");
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
        }

答案 1 :(得分:0)

您的连接属性未初始化。单击button_click以使您的连接intialised.or在Textbox5_textchanged中检查连接con.Isopen否则初始化连接对象。

答案 2 :(得分:0)

我认为你首先要初始化连接对象。 您可以使用以下代码更改代码,然后返回结果:

private void textBox5_TextChanged(object sender, EventArgs e)
  {
    if(con == null)
    {
        con=new SqlConnection(@"Data Source = LAPTOP-VHSGV41H\SQLEXPRESS; Initial Catalog = EmpDB; Integrated Security = True");
    }
    if(con.State == ConnectionState.Closed)
    {
        con.Open();
    }

    SqlDataAdapter da = null;       
    DataTable dt = new DataTable();
    if(comboBox1.Text == "EmployeeID")
    {
        da = new SqlDataAdapter("SELECT EmployeeID, EmployeeName,EmployeePosition, EmployeeSalary FROM FRYEMP where EmployeeID like @employeeID", con);
        da.SelectCommand.Parameters.AddWithValue("@employeeID", "%" + textBox5.Text + "%");
        da.Fill(dt);
    }
    else if (comboBox1.Text == "EmployeeName")
    {
        da = new SqlDataAdapter("SELECT EmployeeID, EmployeeName,EmployeePosition, EmployeeSalary FROM FRYEMP where EmployeeName like @employeeName", con);
        da.SelectCommand.Parameters.AddWithValue("@employeeName", "%" + textBox5.Text + "%");
        da.Fill(dt);
    }
    else
    {

    }

    dataGridView1.DataSource = dt;
  }

答案 3 :(得分:-1)

在您更改textBox5文本时,您的连接尚未初始化。将它移动到构造函数。

namespace FairyTailHRSolution
{
    public partial class Form1 : Form
    {
        SqlCommand cmd;
        SqlConnection con;
        SqlDataAdapter da;


        public Form1()
        {
            InitializeComponent();
            con=new SqlConnection(@"Data Source = LAPTOP-VHSGV41H\SQLEXPRESS; Initial Catalog = EmpDB; Integrated Security = True");
            con.Open();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cmd = new SqlCommand("INSERT INTO FRYEMP (EmployeeID, EmployeeName, EmployeePosition, EmployeeSalary) VALUES (@EmployeeID, @EmployeeName, @EmployeePosition, @EmployeeSalary)", con);
            cmd.Parameters.Add("@EmployeeID", textBox1.Text);
            cmd.Parameters.Add("@EmployeeName", textBox2.Text);
            cmd.Parameters.Add("@EmployeePosition", textBox3.Text);
            cmd.Parameters.Add("@EmployeeSalary", textBox4.Text); 
            cmd.ExecuteNonQuery();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }

        private void label2_Click(object sender, EventArgs e)
        {
        }

        private void find_Click(object sender, EventArgs e)
        {
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            if(comboBox1.Text == "EmployeeID")
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT EmployeeID, EmployeeName,EmployeePosition, EmployeeSalary FROM FRYEMP where EmployeeID like @employeeID", con);
                da.SelectCommand.Parameters.AddWithValue("@employeeID", "%" + textBox5.Text + "%");
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }

            else if (comboBox1.Text == "EmployeeName")
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT EmployeeID, EmployeeName,EmployeePosition, EmployeeSalary FROM FRYEMP where EmployeeName like @employeeName", con);
                da.SelectCommand.Parameters.AddWithValue("@employeeName", "%" + textBox5.Text + "%");
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
    }
   }