Why is employee_id not being inserted into database
<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" />
开发者_C百科 </InsertParameters>
For some reason when I insert John Smith or Indiana Jones, the employee_id is not inserted into the database, I get a NULL value. What am I missing?
Make the employee_id parameter a ControlParameter like the pgy parameter. The controlID should be the DropDownlist ID.
<asp:ControlParameter Name="pgy" Type="Int32" ControlID="pgyTextBox" PropertyName="Text" />
<asp:Parameter Name="employee_id" Type="String" />
Or, you could manually set the parameter to a value.
Found it! long time programmer, but novice to asp.net so I'm not sure of the reason, but just poking around other code that did the same thing i wanted, I was able to write this function:
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
I couldn't find any call to this function, but apparnetly it gets called automatically with every insert of my "AbsentlistView" item.
This function somehow tells the SQL command what the value should be for the drop down, and therefore when the SQL command goes, it has the correct value in the employee_id field.
Thanks all for your help.
I guess you need to bind the ExcusedAbsences_employee_idDropDownList2
SelectedValue
property to the employee_ID
field.
精彩评论