简体   繁体   English

要插入数据库的CommandName问题

[英]CommandName issues to insert to a database

In the below code, when we define the parameters CommandName="Insert" is it actually the same as executing the method Insert? 在下面的代码中,当我们定义参数CommandName =“ Insert”时,它实际上与执行方法Insert相同吗? As I can't find Insert anywhere... 由于我在任何地方都找不到插入...

<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>

I have the following SqlDataSource as well: 我也有以下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>

I want it to insert using the InsertCommand, however when I do SqlDataSource1.Insert() it's complaining that starttime is NULL 我希望它使用InsertCommand进行插入,但是当我执行SqlDataSource1.Insert()时,它抱怨开始时间为NULL

Since the error is for the starttime value not being set, the error might be related to the field you are using to get the starttime value. 由于错误是由于未设置开始时间值,因此该错误可能与您用于获取开始时间值的字段有关。 Are you using a Bind("starttime") instead of Eval("starttime")? 您使用的是Bind(“ starttime”)而不是Eval(“ starttime”)吗?

Bind("starttime")

Updated: Are you setting the paramter? 更新:您正在设置参数吗? if not you should tell the SqlDataSource where to get it. 如果不是,您应该告诉SqlDataSource在哪里获取它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM