2012-09-07 246 views
1

我创建了一个Web应用程序,并包含一个GridView,它将从表testtable中检索记录。GridView更新功能不起作用 - 不更新记录

的代码如下:

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
       DataSourceID="SqlDataSource1"> 
       <Columns> 
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> 
        <asp:BoundField DataField="product_no" HeaderText="product_no" 
         InsertVisible="False" ReadOnly="True" SortExpression="product_no" /> 
        <asp:BoundField DataField="product_name" HeaderText="product_name" 
         SortExpression="product_name" /> 
        <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" /> 
        <asp:BoundField DataField="expire_date" HeaderText="expire_date" 
         SortExpression="expire_date" /> 
        <asp:BoundField DataField="expire_time" HeaderText="expire_time" 
         SortExpression="expire_time" /> 
       </Columns> 
      </asp:GridView> 

      <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
       ConnectionString="<%$ ConnectionStrings:testbaseConnectionString %>" 
       DeleteCommand="DELETE FROM testtable WHERE (product_no = @product_no)" 
       InsertCommand="INSERT INTO testtable(product_name, price, expire_date, expire_time) VALUES (@product_name, @price, @expire_date, @expire_time)" 
       SelectCommand="SELECT testtable.* FROM testtable" 
       UpdateCommand="UPDATE testtable SET product_name = @product_name, price = @price, expire_date = @expire_date, expire_time = @expire_time WHERE (product_no = @product_no)"> 
       <DeleteParameters> 
        <asp:Parameter Name="product_no" /> 
       </DeleteParameters> 
       <InsertParameters> 
        <asp:Parameter Name="product_name" /> 
       </InsertParameters> 
       <UpdateParameters> 
        <asp:Parameter Name="product_name" /> 
        <asp:Parameter Name="price" /> 
        <asp:Parameter Name="expire_date" /> 
        <asp:Parameter Name="expire_time" /> 
        <asp:Parameter Name="product_no" /> 
       </UpdateParameters> 
      </asp:SqlDataSource> 

但是当我点击的GridView中的特定记录Edit链接,并做出一些改变,然后我点击Update链接,但变化并不反映它似乎它没有更新到表格中。 (该链接Edit看起来就像一个展示如下图)

enter image description here

什么可能我会丢失?

+1

我可能失去了一些东西(因为我有限的经验与GridView的)但是你的插入语句对我来说看起来不正确。是不是所有的值都是参数,而不仅仅是第一个?我知道这是插入而不是更新,但也许它是一种连锁效应 – freefaller

+1

此外,它可能是你的SO问题上的拼写错误,但你在GridView上的数据源是'SqlDataSource1',但是你的源的名字'我们提供的是'SqlDataSource2' – freefaller

+0

它最好让visual studio为你制作网格。你在参数上有很多错误,并且在插入时,你忘记了许多'@' – Aristos

回答

0

没有 “的DataKeyNames” GridView控件。

修改你的GridView标签如下:

<asp:GridView DataKeyNames="product_no" ... /> 
0

尝试改变这两个

<asp:BoundField DataField="product_no" HeaderText="product_no" 
        InsertVisible="False" ReadOnly="True" SortExpression="product_no" /> 
<asp:BoundField DataField="product_name" HeaderText="product_name" 
        SortExpression="product_name" /> 

<asp:TemplateField HeaderText="product_name" SortExpression="product_name"> 
    <ItemTemplate> 
     <asp:HiddenField id="hfProduct_No" runat="server" value='<%# Bind("product_no") %>' /> 
     <asp:Label id="lblProduct_name" runat="server" Text ='<%#Bind("product_name")%>' ></asp:Label> 
    </ItemTemplate> 
    <EditItemTemplate> 
     <asp:HiddenField id="hfProduct_No" runat="server" value='<%# Bind("product_no") %>' /> 
     <asp:TextBox id="txtProduct_name" runat="server" Text ='<%#Bind("product_name")%>' ></asp:TextBox> 
    </EditItemTemplate>  
</asp:TemplateField>