简体   繁体   中英

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

<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

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

Updated: Are you setting the paramter? if not you should tell the SqlDataSource where to get it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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