本文介绍了更新gridview中的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在gridview_rowupdating事件上编写此代码,错误是将对象引用未设置为object.why的实例? 我的代码如下: 受保护 void GridView1_RowUpdating( object sender,GridViewUpdateEventArgs e) { GridViewRow row = GridView1.Rows [e.RowIndex]; string constr = ConfigurationManager.ConnectionStrings [ 。构造]的ToString(); SqlConnection conn = new SqlConnection(constr); string id =(((Label)row.FindControl( lblid))。Text).ToString(); string comments =(((TextBox)row.FindControl( txtcomments))。Text).ToString(); conn.Open(); SqlCommand CmdSql = new SqlCommand( 更新请设置admin_comments =' + comments + ',grant =' + true + 'where id = + id + ,conn); CmdSql.ExecuteNonQuery(); GridView1.EditIndex = -1; conn.Close(); 它还没有更新任何行为什么?解决方案 请调试你的代码并找出你获得该异常的那一行。 一些积分 如果web.config中没有连接字符串名称为constr,则可能会抛出异常。 string constr = ConfigurationManager.ConnectionStrings [ constr ]。ToString(); 所以,你应该像下面这样阅读... if (ConfigurationManager.ConnectionStrings [ constr]!= null && !String.IsNullOrEmpty(Convert.ToString(ConfigurationManager.ConnectionStrings [ constr]))) { string constr = ConfigurationManager.ConnectionStrings [ constr]。ToString(); } 额外的.ToString()调用,不是必需的。 string id =(((Label)row.FindControl( lblid))。文本).ToString(); 这里 .Text 给你一个字符串,然后你再次调用 .ToString(),这是不需要的。 在这里,您将id作为字符串传递。请检查表中的字段类型是什么。 标识字段大多是整数字段,在这种情况下它会抛出异常。 然后你需要将 id 转换为整数然后传入查询。 SqlCommand CmdSql = new SqlCommand( 更新假set admin_comments =' + comments + ',grant =' + true + 'where id = + id + ,conn); writing this code on gridview_rowupdating event a error is comming object reference not set to instance of object.why?my code is as follows:protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = GridView1.Rows[e.RowIndex]; string constr = ConfigurationManager.ConnectionStrings["constr"].ToString(); SqlConnection conn = new SqlConnection(constr); string id = (((Label)row.FindControl("lblid")).Text).ToString(); string comments = (((TextBox)row.FindControl("txtcomments")).Text).ToString(); conn.Open(); SqlCommand CmdSql = new SqlCommand("Update leave set admin_comments='" + comments + "', granted='" + true + "' where id=" + id + " ", conn); CmdSql.ExecuteNonQuery(); GridView1.EditIndex = -1; conn.Close();Also it is not updating any row why? 解决方案 Please debug your code and find out on which line you are getting that exception.Some PointsThis may throw exception, if there is no connection string names as "constr" in the web.config.string constr = ConfigurationManager.ConnectionStrings["constr"].ToString();So, you should read it like below...if(ConfigurationManager.ConnectionStrings["constr"] != null && !String.IsNullOrEmpty(Convert.ToString(ConfigurationManager.ConnectionStrings["constr"])) ){ string constr = ConfigurationManager.ConnectionStrings["constr"].ToString();} Extra .ToString() called, not necessary.string id = (((Label)row.FindControl("lblid")).Text).ToString();Here .Text gives you a string, then why you again call .ToString(), which is not needed. Here you are passing the id as a string. Please check what is the field type in Table.Identity fields are mostly integer field, in that case it will throw exception. Then you need to convert that id to integer and then pass in the query.SqlCommand CmdSql = new SqlCommand("Update leave set admin_comments='" + comments + "', granted='" + true + "' where id=" + id + " ", conn); 这篇关于更新gridview中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-27 06:54