代码:
 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT [departmentid], [departmentname], [departmentcount], [updatetime] FROM [department_tbl]" UpdateCommand="update [department_tbl] set [departmentname] = @departmentname,[departmentcount] = @departmentcount where [departmentid] = @old_departmentid " OldValuesParameterFormatString="old_{0}">
        </asp:SqlDataSource>        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2"
            Width="100%" AllowPaging="True" PageSize="20" DataKeyNames="departmentid">
            <Columns>
                <asp:BoundField DataField="departmentid" HeaderText="单位id" InsertVisible="False"
                    ReadOnly="True" >
                    <ItemStyle HorizontalAlign="Center" Width="10%" />
                </asp:BoundField>
                <asp:BoundField DataField="departmentname" HeaderText="单位名"  >
                    <ItemStyle HorizontalAlign="Center" Width="25%" />
                </asp:BoundField>
                <asp:BoundField DataField="departmentcount" HeaderText="单位说明" >
                    <ItemStyle HorizontalAlign="Center" Width="35%" />
                </asp:BoundField>
               <asp:BoundField DataField="updatetime" HeaderText="填加时间" SortExpression="updatetime" InsertVisible="false">
                    <ItemStyle HorizontalAlign="Center" Width="15%" />
                </asp:BoundField>
                <asp:CommandField HeaderText="管理" ShowDeleteButton="True" ShowEditButton="True">
                    <ItemStyle HorizontalAlign="Center" Width="15%" />
                </asp:CommandField>
                
            </Columns>
        </asp:GridView>
错误提示:
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------必须声明变量 '@departmentname'。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.OleDb.OleDbException: 必须声明变量 '@departmentname'。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。  堆栈跟踪: 
[OleDbException (0x80040e14): 必须声明变量 '@departmentname'。]
   System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +764
   System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +108
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) +401
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +721
   System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +78
   System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1215
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +858
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +117
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +115
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +163
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +174
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102 
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.210

解决方案 »

  1.   

    LoveCherry(论成败,人生豪迈;大不了,重头再来!^_^):   老兄在叹气,何不具体说说这种东西的使用优劣?偶感觉就像鸡肋……
      

  2.   

    楼主,错误不是显得很明显了?不认识@departmentname
    你的gridview的邦定中应该有一个输入输出的departmentname才行
      

  3.   

    SqlDataSource,不提倡用,还不如ObjectDataSourc
      

  4.   

    在Sqldatasource中加一个`
    <updateParameters>
     //这写出departmentname的调用
    </updateParameters>
      

  5.   

    <updateParameters>
     //这写出departmentname的调用
    </updateParameters>
    这个我也加了,但是好像也不行
    <UpdateParameters>
              <asp:Parameter  Name="departmentname" Type="string"/>
            </UpdateParameters>
    还提示这个错误
      

  6.   

    你的gridview的邦定中应该有一个输入输出的departmentname才行---------->你是直接邦定的么?是的话有一个邦定departmentname,改为输入输出就可以了
      

  7.   

    举个例子吧,cs文件里没有程序,都是在aspx文件里做的,程序如上,我应该怎么做直接告诉我吧,调 通了就结贴给分,谢谢lovcal(枫兮) 
      

  8.   

    GridView的datasource---〉SQLDataSource
    SQLDataSource的select,update,delete都有相应的语句(这个你自己有了)也就是GridView显示通过你select方法返回的记录集,显示的时候有个DataField邦定,其中有departmentname字段,你用Bind,也就是双向,不要用Eval(单向)这样说不知明白否?
      

  9.   

    以前不知道,现在知道了,但是这个bind或者是eval是在什么里设的呀?我只用过<%# DataBinder.Eval(Container.DataItem, "id")%>这样的,但是BoundField里好像不支持这个,呵呵,我刚学.net你的耐心教教我吧。谢谢了!
      

  10.   

    gridview中右上角,编辑列,将departmentname的邦定设置为模板列,然后在Gridview中右键--编辑刚刚这个模板列,你就会看到编辑状态的TextBox了,可以进行更改