2016-09-18 75 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. :由于上下文不可用,我无法测试代码。

相关问题