网格视图不会更新数据库(没有错误)

时间:2012-12-01 00:48:29

标签: c# asp.net gridview

我有一个包含详细信息视图和网格视图的页面(每个都有自己的数据源。我无法弄清楚为什么更新说它成功但不更新表。详细信息视图工作正常。网格视图只是假设显示与详细信息视图中的内容相关的数据。如果我对源代码中的值进行硬编码,它会起作用,但是当我将其更改回来时它就会爆炸。这是我的代码

        <asp:SqlDataSource OnUpdated="dsCar_Updated" ID="dsCar" runat="server" 
            ConnectionString="<%$ ConnectionStrings:VehicalList %>" 
            ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>" 
            SelectCommand="SELECT Car.* FROM Car WHERE (VIN = ?)" 
            UpdateCommand="UPDATE Car SET [Branch ID] = ?, [State Registration] = ?, [License Plate] = ?, Color = ?, Model = ?, [Car Year] = ?, [Plate Expiration] = ? WHERE (VIN = ?)">
            <SelectParameters>
                <asp:QueryStringParameter Name="?" QueryStringField="VIN" />
            </SelectParameters>
        </asp:SqlDataSource>

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
            DataSourceID="dsCar" Height="50px" Width="265px" CellPadding="4" 
            ForeColor="#333333" GridLines="None" DataKeyNames="VIN" DefaultMode="Edit">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
            <EditRowStyle BackColor="#999999" />
            <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
            <Fields>
                <asp:BoundField DataField="VIN" HeaderText="VIN" ReadOnly="True" 
                    SortExpression="VIN" />
                <asp:TemplateField HeaderText="Branch Name" SortExpression="Branch ID">
                    <EditItemTemplate>
                        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:VehicalList %>" 
                            ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>" 
                            SelectCommand="SELECT * FROM [Branch]"></asp:SqlDataSource>
                        <asp:DropDownList ID="DropDownList1" runat="server" 
                            DataSourceID="SqlDataSource2" DataTextField="Branch Name" 
                            DataValueField="Branch ID" SelectedValue='<%# Bind("[Branch ID]") %>'>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:VehicalList %>" 
                            ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>" 
                            SelectCommand="SELECT * FROM [Branch]"></asp:SqlDataSource>
                        <asp:DropDownList ID="DropDownList1" runat="server" 
                            DataSourceID="SqlDataSource3" DataTextField="Branch Name" 
                            DataValueField="Branch ID" SelectedValue='<%# Bind("[Branch ID]") %>'>
                        </asp:DropDownList>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:VehicalList %>" 
                            ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>" 
                            SelectCommand="SELECT * FROM [Branch]"></asp:SqlDataSource>
                        <asp:DropDownList ID="DropDownList1" runat="server" 
                            DataSourceID="SqlDataSource1" DataTextField="Branch Name" 
                            DataValueField="Branch ID" Enabled="False" SelectedValue='<%# Bind("[Branch ID]") %>'>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="State Registration" HeaderText="State Registration" 
                    SortExpression="State Registration" />
                <asp:BoundField DataField="License Plate" HeaderText="License Plate" 
                    SortExpression="License Plate" />
                <asp:BoundField DataField="Color" HeaderText="Color" SortExpression="Color" />
                <asp:TemplateField HeaderText="Model" SortExpression="Model">
                    <EditItemTemplate>
                        <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:VehicalList %>" 
                            ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>" 
                            SelectCommand="SELECT * FROM [Model]"></asp:SqlDataSource>
                        <asp:DropDownList ID="DropDownList2" runat="server" 
                            DataSourceID="SqlDataSource4" DataTextField="Model Name" 
                            DataValueField="ModelID" SelectedValue='<%# Bind("Model") %>'>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Model") %>'></asp:TextBox>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:VehicalList %>" 
                            ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>" 
                            SelectCommand="SELECT * FROM [Model]"></asp:SqlDataSource>
                        <asp:DropDownList ID="DropDownList2" runat="server" 
                            DataSourceID="SqlDataSource4" DataTextField="Model Name" 
                            DataValueField="ModelID" SelectedValue='<%# Bind("Model") %>' 
                            Enabled="False">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Car Year" HeaderText="Car Year" 
                    SortExpression="Car Year" />
                <asp:TemplateField HeaderText="Plate Expiration" 
                    SortExpression="Plate Expiration">
                    <EditItemTemplate>
                        <asp:SqlDataSource ID="SqlDataSource5" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:VehicalList %>" 
                            ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>" 
                            SelectCommand="SELECT * FROM [Car]"></asp:SqlDataSource>
                        <asp:Calendar ID="Calendar1" runat="server" 
                            onselectionchanged="Calendar1_SelectionChanged" 
                            VisibleDate='<%# Eval("[Plate Expiration]") %>' 
                            SelectedDate='<%# Bind("[Plate Expiration]") %>'></asp:Calendar>
                        <asp:TextBox ID="txtDateEdit" runat="server" 
                            Text='<%# Bind("[Plate Expiration]") %>'></asp:TextBox>
                        <br />
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:Calendar ID="Calendar1" runat="server" 
                            onselectionchanged="Calendar1_SelectionChanged" 
                            VisibleDate='<%# Eval("[Plate Expiration]") %>'></asp:Calendar>
                        <asp:TextBox ID="TextBox1" runat="server" 
                            Text='<%# Bind("[Plate Expiration]") %>' Visible="false"></asp:TextBox>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <br />
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("[Plate Expiration]") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" />
            </Fields>
</asp:DetailsView>

<asp:SqlDataSource OnUpdated="dsMaintance_Updated" ID="dsMaintance" runat="server" 
            ConnectionString="<%$ ConnectionStrings:VehicalList %>" 
            DeleteCommand="DELETE FROM [Maintenance] WHERE [MainteanceID] = ?" 
            InsertCommand="INSERT INTO [Maintenance] ([MainteanceID], [VIN], [Procedure ID], [Date]) VALUES (?, ?, ?, ?)" 
            ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>" 
            SelectCommand="SELECT MainteanceID, VIN, [Procedure ID], [Date] FROM Maintenance WHERE (VIN = ?)" 


            UpdateCommand="UPDATE Maintenance SET [Procedure ID] = ?, [Date] = ? WHERE (MainteanceID = ?)">
            <DeleteParameters>
                <asp:Parameter Name="MainteanceID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="MainteanceID" Type="Int32" />
                <asp:Parameter Name="VIN" Type="String" />
                <asp:Parameter Name="Procedure_ID" Type="Int32" />
                <asp:Parameter Name="Date" Type="DateTime" />
            </InsertParameters>
            <SelectParameters>
                <asp:QueryStringParameter Name="VIN" QueryStringField="VIN" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="MainteanceID" Type="Int32" />
                <asp:Parameter Name="VIN" Type="String" />
                <asp:Parameter Name="Procedure_ID" Type="Int32" />
                <asp:Parameter Name="Date" Type="DateTime" />

            </UpdateParameters>
        </asp:SqlDataSource>


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="MainteanceID" DataSourceID="dsMaintance" BackColor="White" 
            BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" 
            CellSpacing="1" GridLines="None">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="MainteanceID" HeaderText="MainteanceID" 
                    InsertVisible="False" ReadOnly="True" SortExpression="MainteanceID" />
                <asp:BoundField DataField="VIN" HeaderText="VIN" SortExpression="VIN" />
                <asp:BoundField DataField="Procedure ID" HeaderText="Procedure ID" 
                    SortExpression="Procedure ID" />
                <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
            </Columns>
</asp:GridView>

2 个答案:

答案 0 :(得分:0)

您应该将适当的参数传递给UpdateParameters部分。看起来您有复制粘贴问题(可能是您复制了InsertParameters部分中的部分)。您的UPDATE语句只需要3个参数。

答案 1 :(得分:0)

<asp:SqlDataSource OnUpdated="dsMaintance_Updated" ID="dsMaintance" runat="server" 
        ConnectionString="<%$ ConnectionStrings:VehicalList %>" 
        DeleteCommand="DELETE FROM [Maintenance] WHERE [MainteanceID] = ?" 
        InsertCommand="INSERT INTO [Maintenance] ([MainteanceID], [VIN], [Procedure ID], [Date]) VALUES (?, ?, ?, ?)" 
        ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>" 
        SelectCommand="SELECT MainteanceID, VIN, [Procedure ID], [Date] FROM Maintenance WHERE (VIN = ?)"  
        UpdateCommand="UPDATE Maintenance SET [Procedure ID] = ?, [Date] = ? WHERE (MainteanceID = ?)">
        <DeleteParameters>
            <asp:Parameter Name="MainteanceID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="MainteanceID" Type="Int32" />
            <asp:Parameter Name="VIN" Type="String" />
            <asp:Parameter Name="Procedure_ID" Type="Int32" />
            <asp:Parameter Name="Date" Type="DateTime" />
        </InsertParameters>
        <SelectParameters>
            <asp:QueryStringParameter Name="VIN" QueryStringField="VIN" Type="String" />
        </SelectParameters>
        <UpdateParameters>
            <%--<asp:Parameter Name="MainteanceID" Type="Int32" />--%>
            <%--<asp:Parameter Name="VIN" Type="String" />--%>
            <asp:Parameter Name="Procedure_ID" Type="Int32" />
            <asp:Parameter Name="Date" Type="DateTime" />   
        </UpdateParameters>
    </asp:SqlDataSource>