简体   繁体   中英

ASP.NET VB.NET detailsview not updating one column in SQL Server database

I am at a standstill. I have two detailsview forms to insert new records. In form 1 everything works and all columns are updated in the SQL Server table.

In form 2 (which is almost the same as the first form, form 1 has a filter) doesn't update one column in the same SQL Server table using the same code as in form 1. I don't know why. When I break on the code behind in the form 2 the correct value appears in the dropdownlist.Selectedvalue but when I click the insert button that value is not inserted.

Here is the markup for form 1:

<asp:TemplateField HeaderText="Water Body Name*">
    <ItemTemplate>
        <asp:Label ID="lblStreamName" runat="server" Text='<%# Eval("LLIDNUM")%>'   Visible = "true"></asp:Label>
    </ItemTemplate>
    <InsertItemTemplate>
        <asp:DropDownList ID="ddCatalogName" runat="server" AutoPostBack="True" 
             DataSourceID="GetCatalogNames" DataTextField="StreamName" 
             DataValueField="LLIDNUM" AppendDataBoundItems="true"> 
            <asp:ListItem Text="Select" Value="" />
        </asp:DropDownList>
        <asp:ObjectDataSource ID="dsWaterBody" runat="server" TypeName="StreamName"
             SelectMethod="GetCatalogNames">
            <SelectParameters>
                <%--<asp:Parameter Name="HatcheryCodetName"  Type="string" />--%>
                <asp:ControlParameter Name="HatcheryCodePlant" Type="string" ControlID="ddPlantingHatchery" PropertyName="Selectedvalue"/>
            </SelectParameters>
        </asp:ObjectDataSource>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
             ControlToValidate="ddCatalogName" ErrorMessage="Please Enter Water Body Name" 
             Display="Dynamic" ForeColor="Red">*
        </asp:RequiredFieldValidator>
    </InsertItemTemplate>
</asp:TemplateField>

Code behind:

Private Sub DetailsView1_ItemInserting(sender As Object, e As DetailsViewInsertEventArgs) Handles dvSMasterCurrentYear.ItemInserting
Try
    Dim view As DetailsView = DirectCast(sender, DetailsView)
    Dim username As String = System.Web.HttpContext.Current.User.Identity.Name
    Dim ddCatNum As DropDownList = TryCast(view.FindControl("ddCatalogName"), DropDownList) 
    e.Values.Add("LLIDNUM", ddCatNum.SelectedValue)

    Page.Validate()
    'show a message or throw an exception
    If Not Page.IsValid Then
    End If
Catch ex As Exception
    Throw New Exception(ex.Message)
End Try
End Sub

Here is the markup code for form 2:

<asp:TemplateField HeaderText="Water Body Name*">
    <ItemTemplate>
        <asp:Label ID="lblStreamName" runat="server" Text='<%# Eval("LLIDNUM")%>' Visible = "true"></asp:Label>
    </ItemTemplate>
    <InsertItemTemplate>
        <asp:DropDownList ID="ddCatalogName" runat="server" AutoPostBack="True" 
             DataSource='<%# GetCatalogNames()%>' DataTextField="StreamName" DataValueField="LLIDNUM" 
             AppendDataBoundItems="true"> 
            <asp:ListItem Text="Select" Value="" />
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
             ControlToValidate="ddCatalogName" ErrorMessage="Please Enter Water Body" 
             Display="Dynamic" ForeColor="Red">*
        </asp:RequiredFieldValidator>
    </InsertItemTemplate>
</asp:TemplateField>

Code behind for form 2:

Private Sub dvNewStockingWater_ItemInserting(sender As Object, e As DetailsViewInsertEventArgs) Handles dvNewStockingWater.ItemInserting
    Try
       Dim view As DetailsView = DirectCast(sender, DetailsView)
       Dim ddCatNum As DropDownList = TryCast(view.FindControl("ddCatalogName"), DropDownList)  

       e.Values.Add("LLIDNUM", ddCatNum.SelectedValue)
    Catch ex As Exception
       Throw New Exception(ex.Message)
    End Try
End Sub

All the remaining fields in both forms update correctly. The data source is configured the same for both forms each is set up for inserting and updating. What am I missing?

thank you.

Compare your try block in the first sample to the one in the second.

You're missing the username. Is that needed for your insert to work? (We're in the template, so it may be a parameter in your datasource which isn't listed)

You also don't check page.IsValid in the second sample, but if you can hit a breakpoint your page is probably valid.

I decided to check the insert statements of the two forms. It turned out that the second form had an older version of the insert statement. I updated this statement and all field are uploaded to the database. Thank you all for your help.

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