发生网格视图更新操作错误 [英] Grid view update operation error occurs

查看:116
本文介绍了发生网格视图更新操作错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用网格视图进行更新。当我更新错误发生时如下;



我的代码如下;



  protected   void  GridView1_RowUpdating( object  sender,GridViewUpdateEventArgs e)
{
string Sno =((TextBox)GridView1.Rows [ e.RowIndex]
.FindControl( txtSno))。文字;
string 名称=((TextBox)GridView1.Rows [e.RowIndex]
.FindControl( txtName))。文字;
string 年龄=((TextBox)GridView1.Rows [e.RowIndex]
.FindControl( txtage))。文字;
string 地址=((TextBox)GridView1.Rows [e.RowIndex] .FindControl( txtaddress))。文字;

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详细信息设置Sno = @ Sno,Name = @ Name, +
年龄= @年龄,其中Sno = @Sno; +
从详细信息中选择Sno,名称,年龄,地址;
cmd.Parameters.Add( @ Sno,SqlDbType.Int).Value =斯诺;
cmd.Parameters.Add( @ Name,SqlDbType.VarChar).Value =名称;
cmd.Parameters.Add( @ Age,SqlDbType.VarChar).Value =年龄;
cmd.Parameters.Add( @ Address,SqlDbType.VarChar).Value =地址;
cmd.ExecuteNonQuery();
SqlCommand cmd1 = new SqlCommand( 选择*来自详细信息,con);
SqlDataAdapter adp = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.EditIndex = -1;
cmd.Connection = con;
con.Close();
}



错误发生如下,ExecuteNonQuery:Connection属性尚未初始化。 (cmd.ExecuteNonQuery();)出现此行错误请帮帮我。上面代码中我的问题是什么。

解决方案

写这行

 cmd.Connection = con; 

 con.Open(); 


i am doing update using grid view.when i update error occurs as follows;

my code as follows;

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.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();
        GridView1.EditIndex = -1;
        cmd.Connection = con;
        con.Close();
    }


the error occurs as follows, ExecuteNonQuery: Connection property has not been initialized. ( cmd.ExecuteNonQuery();) in this line error occurs please help me.what is my problem in the above code.

解决方案

Write this line

cmd.Connection=con;

after

con.Open();


这篇关于发生网格视图更新操作错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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