要插入数据库的CommandName问题

时间:2010-04-05 16:00:05

标签: c# asp.net

在下面的代码中,当我们定义参数CommandName =“Insert”时,它实际上与执行Insert方法相同吗?因为我找不到随处插入......

<div class="actionbuttons">
                        <Club:RolloverButton ID="apply1" CommandName="Insert" Text="Add Event" runat="server" />
                        <Club:RolloverLink ID="Cancel" Text="Cancel" runat="server" NavigateURL='<%# "Events_view.aspx?EventID=" + Convert.ToString(Eval("ID")) %>' />
                    </div>

我也有以下SqlDataSource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
    SelectCommand="SELECT dbo.Events.id, dbo.Events.starttime, dbo.events.endtime, dbo.Events.title, dbo.Events.description, dbo.Events.staticURL, dbo.Events.photo,  dbo.Events.location, dbo.Locations.title AS locationname FROM  dbo.Events LEFT OUTER JOIN dbo.Locations ON dbo.Events.location = dbo.Locations.id where Events.id=@id"
    InsertCommand="INSERT INTO Events(starttime, endtime, title, description, staticURL, location, photo) VALUES (@starttime, @endtime,  @title, @description, @staticURL, @location, @photo)"
    UpdateCommand="UPDATE Events SET starttime = @starttime, endtime=@endtime, title = @title, description = @description, staticURL = @staticURL, location = @location, photo = @photo WHERE (id = @id)"
    DeleteCommand="DELETE Events WHERE id=@id" OldValuesParameterFormatString="{0}">
    <SelectParameters>
        <asp:QueryStringParameter Name="id" QueryStringField="ID" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="starttime" Type="DateTime" />
        <asp:Parameter Name="endtime" Type="DateTime" />
        <asp:Parameter Name="title" />
        <asp:Parameter Name="description" />
        <asp:Parameter Name="staticURL" />
        <asp:Parameter Name="location" />
        <asp:Parameter Name="photo" />
        <asp:Parameter Name="id" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="starttime" Type="DateTime" />
        <asp:Parameter Name="endtime" Type="DateTime" />
        <asp:Parameter Name="title" />
        <asp:Parameter Name="description" />
        <asp:Parameter Name="staticURL" />
        <asp:Parameter Name="location" />
        <asp:Parameter Name="photo" />
        <asp:Parameter Name="id" />
    </InsertParameters>
    <DeleteParameters>
        <asp:QueryStringParameter Name="id" QueryStringField="ID" />
    </DeleteParameters>
</asp:SqlDataSource>

我希望它使用InsertCommand插入,但是当我执行SqlDataSource1.Insert()时,它抱怨starttime为NULL

1 个答案:

答案 0 :(得分:0)

由于错误是针对未设置的启动时间值,因此错误可能与您用于获取启动时间值的字段有关。你使用Bind(“starttime”)而不是Eval(“starttime”)?

Bind("starttime")

更新: 你在设置参数吗?如果不是,你应该告诉SqlDataSource从哪里获取它。

<asp:ControlParameter Name="starttime" ControlID="dtpicker"
                   PropertyName="startDateTime" Type="DateTime" />