为什么employee_id没有插入数据库

时间:2011-04-20 22:46:07

标签: asp.net sql-server-2005 listview

    <asp:TabPanel ID="AbsentTabPanel" runat="server" HeaderText="Excused Absences">
        <ContentTemplate>
        <asp:ListView ID="AbsentListView" runat="server" InsertItemPosition="LastItem"
                DataSourceID="AbsentSqlDataSource" DataKeyNames="">

            <InsertItemTemplate>
                <tr>
                    <td>
                        <asp:DropDownList ID="ExcusedAbsences_employee_idDropDownList2" runat="server">
                            <asp:ListItem Text="John Smith" Value="1" />
                            <asp:ListItem Text="Indiana Jones" Value="2" />
                        </asp:DropDownList> 

                    </td>
                    <td>
                        <!-- start date -->
                        <asp:TextBox ID="start_dt_codeTextBox" runat="server" Columns="6" Text='<%#Bind("start_dt")%>' />
                    </td>
                    <td>
                        <!-- end date -->
                        <asp:TextBox ID="end_dt_codeTextBox" runat="server" Columns="6" Text='<%#Bind("end_dt")%>' />
                    </td>
                    <td>
                        <asp:Button runat="server" CommandName="Insert" Text="insert" />
                        <asp:Button runat="server" CommandName="Clear" Text="clear" />
                    </td>
                </tr>                    
            </InsertItemTemplate>

...

<asp:SqlDataSource ID="AbsentSqlDataSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:Rotations3ConnectionString %>"
    SelectCommand="SELECT employee_id, start_dt, end_dt
                     FROM Excused_Absences
                    WHERE fy = @fy AND pgy = @pgy"

    UpdateCommand="UPDATE Excused_Absences
                      SET start_dt = @start_dt, end_dt = @end_dt, employee_id = @employee_id
                    WHERE absence_nbr = @absence_nbr"

    InsertCommand="INSERT INTO Excused_Absences (fy, pgy, employee_id, start_dt, end_dt)
                        VALUES (@fy, @pgy, @employee_id, @start_dt, @end_dt)"

    OldValuesParameterFormatString="old_{0}">

    <SelectParameters>
        <asp:ControlParameter Name="fy" Type="Int32" ControlID="fyTextBox" PropertyName="Text" />
        <asp:ControlParameter Name="pgy" Type="Int32" ControlID="pgyTextBox" PropertyName="Text" />
    </SelectParameters>

    <InsertParameters>
        <asp:ControlParameter Name="fy" Type="Int32" ControlID="fyTextBox" PropertyName="Text" />
        <asp:ControlParameter Name="pgy" Type="Int32" ControlID="pgyTextBox" PropertyName="Text" />
        <asp:Parameter Name="employee_id" Type="String" />
        <asp:Parameter Name="start_dt" Type="DateTime" />
        <asp:Parameter Name="end_dt" Type="DateTime" />
    </InsertParameters>

由于某些原因,当我插入John Smith或Indiana Jones时,employee_id未插入数据库,我得到一个NULL值。我错过了什么?

3 个答案:

答案 0 :(得分:1)

使employee_id参数成为像pgy参数一样的ControlParameter。 controlID应该是DropDownlist ID。

<asp:ControlParameter Name="pgy" Type="Int32" ControlID="pgyTextBox" PropertyName="Text" />
<asp:Parameter Name="employee_id" Type="String" />

或者,您可以手动将参数设置为值。

答案 1 :(得分:1)

发现它!很长一段时间的程序员,但是asp.net的新手,所以我不确定原因,但只是在寻找其他代码,我做了同样的事情,我能够编写这个函数:

Protected Sub AbsentListView_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles AbsentListView.ItemInserting
    e.Values("employee_id") = DirectCast(AbsentListView.InsertItem.FindControl("ExcusedAbsences_employee_idDropDownList2"), DropDownList).SelectedValue
End Sub

我无法找到对此功能的任何调用,但是随着我的&#34; AbsentlistView&#34;的每次插入,它都会被自动调用。项目

此函数以某种方式告诉SQL命令下拉值应该是什么,因此当SQL命令运行时,它在employee_id字段中具有正确的值。

感谢大家的帮助。

答案 2 :(得分:0)

我想你需要将ExcusedAbsences_employee_idDropDownList2 SelectedValue属性绑定到employee_ID字段。