首页 > 解决方案 > 连接未关闭。连接的当前状态是 open.C#

问题描述

我正在练习我的 C# 编程技能,我相对较新,我正在尝试创建一个软件来帮助我父亲跟踪他的文档,我做得很好但遇到了这个问题。

当我运行代码时,它工作正常,当我在文本框中输入一些信息时,假设我输入 123 作为 ID,它显示“由于这已经是主键,并且 ID 已经存在,请选择其他内容”,所以我将我的 ID 重新输入为 1234 以进行测试,我收到此错误“连接未关闭。连接的当前状态为打开”我确实有 con.Open() 和 con.Close(),但我仍然无法解决问题,这是我的代码,如果有人可以提供帮助,非常感谢:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Documents
{
    public partial class hoadonKT : Form
    {
        SqlConnection con = new SqlConnection(Properties.Settings.Default.HoaDonKetThucConnection);
        SqlCommand cmd;
        SqlDataAdapter da;
        DataTable dt;
        public hoadonKT()
        {
            InitializeComponent();
        }

        private void Label1_Click(object sender, EventArgs e)
        {

        }

        private void Label1_Click_1(object sender, EventArgs e)
        {

        }

        private void Button1_Click(object sender, EventArgs e)
        {
            // save button
            if (textBox1.Text == "" && textBox2.Text == "")
            {
                MessageBox.Show("Hay dien du thong tin can thiet");
            }
            else
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand(@"INSERT INTO [dbo].[KTHoaDon]
                                        ([MaDon],[TenDon])
                    VALUES

                    ('"+textBox1.Text+"','"+textBox2.Text+"')",con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("Du Lieu Da Duoc Luu Tru");
                    fillGrid();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        public void fillGrid()
        {
            // fill datagridview from datatable
            con.Open();
            da = new SqlDataAdapter("select * from KTHoaDon order by MaDon asc", con);
            con.Close();
            SqlCommandBuilder cd = new SqlCommandBuilder(da);
            dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }

        private void Button1_Click_1(object sender, EventArgs e)
        {

        }

        private void HoadonKT_Load(object sender, EventArgs e)
        {
            fillGrid();

        }

        int i;
        private void DataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
            DataGridViewRow row = dataGridView1.Rows[i];
            textBox1.Text = row.Cells[0].Value.ToString();
            textBox2.Text = row.Cells[1].Value.ToString();
        }
    }
}

标签: c#

解决方案


试试这个代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Documents
{
    public partial class hoadonKT : Form
    {
        SqlConnection con = new SqlConnection(Properties.Settings.Default.HoaDonKetThucConnection);
        SqlCommand cmd;
        SqlDataAdapter da;
        DataTable dt;
        public hoadonKT()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            // save button
            if (textBox1.Text == "" && textBox2.Text == "")
            {
                MessageBox.Show("Hay dien du thong tin can thiet");
            }
            else
            {
                try
                {
                    string queryText = "INSERT INTO [dbo].[KTHoaDon] ([MaDon],[TenDon]) VALUES(@MaDon,@TenDon)";
                    using (cmd = new SqlCommand(queryText, con))
                    {
                        cmd.Parameters.AddWithValue("MaDon", textBox1.Text);
                        cmd.Parameters.AddWithValue("TenDon", textBox2.Text);

                        cmd.ExecuteNonQuery();
                    }
                    MessageBox.Show("Du Lieu Da Duoc Luu Tru");

                    fillGrid();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        public void fillGrid()
        {
            dt = new DataTable();
            // fill datagridview from datatable
            using (da = new SqlDataAdapter("select * from KTHoaDon order by MaDon asc", con))
            {                                
                da.Fill(dt);
            }
            dataGridView1.DataSource = dt;
        }

        private void HoadonKT_Load(object sender, EventArgs e)
        {
            con.Open();
            fillGrid();
        }

        private void HoadonKT_FormClosing(object sender, FormClosingEventArgs e)
        {
            con.Close();
        }

        int i;
        private void DataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
            DataGridViewRow row = dataGridView1.Rows[i];
            textBox1.Text = row.Cells[0].Value.ToString();
            textBox2.Text = row.Cells[1].Value.ToString();
        }
    }
}

推荐阅读