Почему employee_id не вставляется в базу данных - PullRequest
1 голос
/ 21 апреля 2011
    <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>

Почему-то, когда я вставляю Джона Смита или Индиану Джонса, employee_id не вставляется в базу данных, я получаю значение NULL.Чего мне не хватает?

Ответы [ 3 ]

1 голос
/ 21 апреля 2011

Сделайте параметр employee_id параметром ControlParameter, подобным параметру pgy. ControlID должен быть идентификатором DropDownlist.

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

Или вы можете вручную установить для параметра значение.

1 голос
/ 21 апреля 2011

Нашли это! давно программист, но новичок в 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

Мне не удалось найти ни одного вызова этой функции, но, по-видимому, она вызывается автоматически при каждой вставке моего элемента «AbsentlistView».

Эта функция каким-то образом сообщает команде SQL, каким должно быть значение для раскрывающегося списка, и поэтому, когда команда SQL идет, она имеет правильное значение в поле employee_id.

Спасибо всем за помощь.

0 голосов
/ 21 апреля 2011

Полагаю, вам нужно связать свойство ExcusedAbsences_employee_idDropDownList2 SelectedValue с полем employee_ID.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...