2017-10-18 67 views
-2

所以我寫了我的代碼,但每次我嘗試執行它說「異常未處理System.InvalidOperationException:'填充:SelectCommand.Connection屬性尚未初始化。」,它始終顯示它在說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) 
     { 


     } 
    } 
} 
+0

你看了錯誤消息,想想它告訴你是錯的?你是否使用調試器來發現'con'在拋出異常的情況下明顯爲空? –

+0

也許你應該嘗試初始化SelectCommand的Connection屬性。你已經爲SqlCommand正確地做了它。 –

回答

-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) 
     { 
     } 
    } 
    } 
+0

由於它實現了'IDisposable',所以處理連接是一種很好的做法。 –

+0

同意,但在什麼時候? winforms是否有卸載事件?如果是這樣,它可以去那裏 –

0

您的連接屬性不intialised.Click button_click有你的連接intialised.or內Textbox5_textchanged再次別的intialize連接對象檢查連接con.Isopen。

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; 
       } 
      } 
     } 
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; 
    } 
相關問題