Gridview更新错误连接未关闭。连接的当前状态是打开的。 [英] Gridview Updation Error The connection was not closed. The connection's current state is open.

查看:89
本文介绍了Gridview更新错误连接未关闭。连接的当前状态是打开的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
 string Sno = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtSno")).Text;
        string Name = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtName")).Text;
        string Age = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtage")).Text;
        string Address = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtaddress")).Text;
        SqlConnection con = new SqlConnection("Server=(local);initial catalog=master;Trusted_Connection=True");
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update Details set Sno=@Sno, Name=@Name," +
     "Age=@Age where Sno=@Sno;" +
     "select Sno,Name,Age,Address from Details";
        cmd.Parameters.Add("@Sno", SqlDbType.Int).Value = Sno;
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
        cmd.Parameters.Add("@Age", SqlDbType.VarChar).Value = Age;
        cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = Address;
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        SqlCommand cmd1 = new SqlCommand("Select * from Details", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd1);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
con.Close();
}



错误如下:


The error as follows:

The connection was not closed. The connection's current state is open.



我的代码有什么问题?


What is the problem with my code?

推荐答案

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
 string Sno = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtSno")).Text;
        string Name = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtName")).Text;
        string Age = ((TextBox)GridView1.Rows[e.RowIndex]
                            .FindControl("txtage")).Text;
        string Address = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtaddress")).Text;
        SqlConnection con = new SqlConnection("Server=(local);initial catalog=master;Trusted_Connection=True");
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update Details set Sno=@Sno, Name=@Name," +
     "Age=@Age where Sno=@Sno;" +
     "select Sno,Name,Age,Address from Details";
        cmd.Parameters.Add("@Sno", SqlDbType.Int).Value = Sno;
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
        cmd.Parameters.Add("@Age", SqlDbType.VarChar).Value = Age;
        cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = Address;
        cmd.Connection = con;
        con.Open(); // here is the error.. remove it
        cmd.ExecuteNonQuery();
        SqlCommand cmd1 = new SqlCommand("Select * from Details", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd1);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
con.Close();
}


这篇关于Gridview更新错误连接未关闭。连接的当前状态是打开的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆