2016-09-18 77 views
-1

如何解決此錯誤如何解決此錯誤「連接未關閉,連接的當前狀態已打開。」

連接未關閉。連接的當前狀態已打開。

雖然我收我的下面這段代碼連接,想盡一切辦法,甚至關閉

private void cmbdealercode_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (txtinvoiceno.Text == "") 
    { 
     if (txtinvoicedate.Text == "") 
     { 
      con.Open(); 

      SqlCommand cmd1 = new SqlCommand("select * from tbl_invoice where invoice_no = @no,[email protected],[email protected]", con); 

      cmd1.Parameters.AddWithValue("@no", txtinvoiceno.Text); 
      cmd1.Parameters.AddWithValue("@date", txtinvoicedate.Text); 
      cmd1.Parameters.AddWithValue("@dealercode", cmbdealercode.SelectedValue); 

      SqlDataAdapter adpt = new SqlDataAdapter(cmd1); 
      DataSet ds = new DataSet(); 
      DataTable dt = new DataTable(); 

      adpt.Fill(dt); 

      if (dt.Rows.Count > 0) 
      { 
       MessageBox.Show("Already invoice has done for the concern details"); 
       lblpaymentstatus.Text = ""; 
       lbluntilpaid.Text = ""; 
       lblpaymentstatus.Text = dt.Rows[0]["invoice_payment_status"].ToString(); 
       lbluntilpaid.Text = dt.Rows[0]["so_far_paid"].ToString(); 
      } 
      else 
      { 
       DialogResult dialogueresult = MessageBox.Show("The entered details are seems to be new; your new invoice will be raised", "The Question", MessageBoxButtons.OK); 
       lblpaymentstatus.Text = "Not Paid"; 
       lbluntilpaid.Text = "0"; 
      } 

      con.Close(); 
     } 
    }   
} 

回答

0

使用try catch塊第一:

try 
{ 
    con.open(); 
} 
catch(Exception ex) 
{ 
    throw ex; 
} 
finally 
{ 
    con.close(); 
} 
1

請嘗試以下粘貼代碼。

private void cmbdealercode_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     //Get your connection string as per your project settings 
     var connectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet-Northwind.mdf;Initial Catalog=aspnet-Northwind;Integrated Security=True;User Instance=True" 
     if (txtinvoiceno.Text == "") 
     { 
      if (txtinvoicedate.Text == "") 
      { 
       using (SqlConnection con = new SqlConnection(connectionString)) 
       { 
        con.Open(); 
        using (SqlCommand cmd1 = new SqlCommand("select * from tbl_invoice where invoice_no = @no,[email protected],[email protected]", con)) 
        { 
         cmd1.Parameters.AddWithValue("@no", txtinvoiceno.Text); 
         cmd1.Parameters.AddWithValue("@date", txtinvoicedate.Text); 
         cmd1.Parameters.AddWithValue("@dealercode", cmbdealercode.SelectedValue); 
         using (SqlDataAdapter adpt = new SqlDataAdapter(cmd1)) 
         { 
          using (DataSet ds = new DataSet()) 
          { 
           using (DataTable dt = new DataTable()) 
           { 
            adpt.Fill(dt); 
            if (dt.Rows.Count > 0) 
            { 
             MessageBox.Show("Already invoice has done for the concern details"); 
             lblpaymentstatus.Text = ""; 
             lbluntilpaid.Text = ""; 
             lblpaymentstatus.Text = dt.Rows[0]["invoice_payment_status"].ToString(); 
             lbluntilpaid.Text = dt.Rows[0]["so_far_paid"].ToString(); 
            } 
            else 
            { 
             DialogResult dialogueresult = MessageBox.Show("The entered details are seems to be new; your new invoice will be raised", "The Question", MessageBoxButtons.OK); 
             lblpaymentstatus.Text = "Not Paid"; 
             lbluntilpaid.Text = "0"; 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 

所有這些實現IDisposable這些類可以封裝內使用塊,一旦封閉的,你不必擔心關閉連接或處置的對象。希望這可以幫助。 P.S. :由於上下文不可用,我無法測試代碼。

相關問題