gridview编辑更新后,数据库已经成功改写。可却出现下面错误提示。
请问是什么原因?
'GridView1_RowUpdating' 附近有语法错误。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 'GridView1_RowUpdating' 附近有语法错误。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。  堆栈跟踪: 
[SqlException (0x80131904): 'GridView1_RowUpdating' 附近有语法错误。]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +98
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +82
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +346
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +3430
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +186
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1272
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +334
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +407
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +149
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) +496
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +916
   System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +179
   System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1140
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +809
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +162
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +118
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +107
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +175
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +244
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3824 
代码部分:
  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        sqlcon = new SqlConnection(strCon);
        string sqlstr = "update sysjob set beizhu='" + ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text.ToString()  + "' where id='" +  GridView1.DataKeys[e.RowIndex].Value.ToString()  +  "'";
        SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        GridView1.EditIndex = -1;
        bind();    }

解决方案 »

  1.   

    最简单的:套个try...catch
      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
        {
    try
    {
    sqlcon = new SqlConnection(strCon); 
            string sqlstr = "update sysjob set beizhu='" + ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text.ToString()  + "' where id='" +  GridView1.DataKeys[e.RowIndex].Value.ToString()  +  "'"; 
            SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon); 
            sqlcon.Open(); 
            sqlcom.ExecuteNonQuery(); 
            sqlcon.Close(); 
            GridView1.EditIndex = -1; 
            bind(); }
    catch
    {
    sqlcon.Close(); 
    }
    }
      

  2.   

    是不是update成功后没有重新绑定数据?
      

  3.   

     string sqlstr = "update sysjob set beizhu='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString()  + "' where id='" +  GridView1.DataKeys[e.RowIndex].Value.ToString()  +  "'";