用于多个表的可编辑GridView控件

时间:2018-10-10 17:25:01

标签: c# asp.net stored-procedures gridview sql-update

我要从SQL Server 检索多个表数据到GridView控件。我能够显示从不同表到GridView的列,并且我想使其对用户可编辑。但是我对此有一个小问题

Gridview中的

Edit链接工作正常,我可以对每个列进行更改。但是,当我单击“更新”链接时,我所做的更改未更新。因此,这是我的 GridView控件HTML代码,如下所示:

<asp:GridView ID="GridView1" runat="server" CssClass="auto-style36" AllowPaging="True" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="Atd_ID" HeaderText="Atd_ID" SortExpression="Atd_ID" InsertVisible="False" ReadOnly="True" />
        <asp:BoundField DataField="Atd_Date" HeaderText="Atd_Date" SortExpression="Atd_Date" />
        <asp:BoundField DataField="Atd_InTime" HeaderText="Atd_InTime" SortExpression="Atd_InTime" />
        <asp:BoundField DataField="Atd_OutTime" HeaderText="Atd_OutTime" SortExpression="Atd_OutTime" />
        <asp:BoundField DataField="Atd_Comment" HeaderText="Atd_Comment" SortExpression="Atd_Comment" />
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
        <asp:BoundField DataField="SID" HeaderText="SID" SortExpression="SID" />
        <asp:BoundField DataField="S_FName" HeaderText="S_FName" SortExpression="S_FName" />
        <asp:BoundField DataField="S_LName" HeaderText="S_LName" SortExpression="S_LName" />
    </Columns>

</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SAOS1ConnectionString %>" SelectCommand="SELECT attendance.Atd_ID,attendance.Atd_Date, attendance.Atd_InTime, attendance.Atd_OutTime, attendance.Atd_Comment, attendance.Status, student.SID, student.S_FName, student.S_LName FROM student LEFT OUTER JOIN attendance ON student.SID = attendance.SID AND attendance.Atd_Date = @Atd_Date LEFT OUTER JOIN class ON student.CID = class.CID WHERE (class.CID = @CID)" UpdateCommand="updatetable5" UpdateCommandType="StoredProcedure">
    <SelectParameters>
        <asp:ControlParameter ControlID="datepicker" Name="Atd_Date" PropertyName="Text" />
        <asp:ControlParameter ControlID="LabelClassID" Name="CID" PropertyName="Text" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="Atd_ID" Type="Int32" />
        <asp:Parameter DbType="Date" Name="Atd_Date" />
        <asp:Parameter DbType="Time" Name="Atd_InTime" />
        <asp:Parameter DbType="Time" Name="Atd_OutTime" />
        <asp:Parameter Name="Atd_Comment" Type="String" />
        <asp:Parameter Name="Status" Type="String" />
        <asp:Parameter Name="SID" Type="Int32" />
        <asp:Parameter Name="S_FName" Type="String" />
        <asp:Parameter Name="S_LName" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>

这是我的存储过程,用于更新表attendancestudent,我相信问题是从这里开始的。

USE [SAOS1]
GO
CREATE PROCEDURE [dbo].[updatetable2]
(
    @Atd_ID int,
    @Atd_Date date,
    @Atd_InTime time(7),
    @Atd_OutTime time(7),
    @Atd_Comment varchar(50),
    @Status varchar(50),
    @SID int,
    @S_FName varchar(50),
    @S_LName varchar(50)
)
AS
BEGIN
UPDATE attendance SET Atd_Date=@Atd_Date, Atd_InTime=@Atd_InTime, Atd_OutTime=@Atd_OutTime, Atd_Comment=@Atd_Comment, Status=@Status WHERE Atd_ID=@Atd_ID
UPDATE student SET S_FName=@S_FName, S_LName=@S_LName WHERE SID=@SID
END

下面Atd_ID=@Atd_ID行中的 WHERE CLAUSE 自动生成的密钥。这是我无法更新GridView控件中的更改的原因吗?我尝试过几次更改WHERE子句,尽管结果不是我想要的,但是gridview允许我进行更改。

UPDATE attendance SET Atd_Date=@Atd_Date, Atd_InTime=@Atd_InTime, Atd_OutTime=@Atd_OutTime, Atd_Comment=@Atd_Comment, Status=@Status where Atd_ID=@Atd_ID

0 个答案:

没有答案