网格视图更新错误发生连接未关闭。连接的当前状态是打开的。 [英] Grid view Updation error Occurs The connection was not closed. The connection's current state is open.

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

问题描述

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);
        con.Close();
        GridView1.DataSource = ds;
        GridView1.DataBind();
}



错误发生如下;连接没有关闭。连接的当前状态是开放的。



请帮助我。从我上面的代码是什么问题。


The Error occurs as follows; The connection was not closed. The connection''s current state is open.

please help me. from my above code what is the problem.

推荐答案

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(); // it comes here
}


使用此代码
if(con.State = ConnectionState.Closed)
con.open



而不是你的代码


instead of your code

con.Open













实际上 你正在调用con.open()方法两次。因此,删除con声明旁边的con.open()。







Actually you are calling con.open() method twice. So delete con.open() next to the declaration of "con".


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

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