从类型转换为DBNull'输入'整数'无效。使用asp.net和对象数据源

时间:2017-04-23 11:31:47

标签: asp.net objectdatasource

我尝试在gridview中编辑名为(gvTeam)的行 gridview使用objectdatasource(odsGvTeam)

Admin_team.aspx 中的

      <asp:GridView ID="gvTeam" runat="server" AutoGenerateColumns="False" DataKeyNames="team_id" DataSourceID="odsGvTeam" AllowPaging="True">
                <Columns>
                 <asp:BoundField DataField="team_id" HeaderText="team_id" InsertVisible="False" ReadOnly="True" SortExpression="team_id" Visible="False" />
                    <asp:BoundField DataField="team_name" HeaderText="key_team_name" SortExpression="team_name" />
                    <asp:BoundField DataField="team_lang" HeaderText="key_team_lang" SortExpression="team_lang" />
                    <asp:BoundField DataField="team_status" HeaderText="key_status" SortExpression="team_status" />
                    <asp:BoundField DataField="team_key" HeaderText="key_team_key" SortExpression="team_key" />
                    <asp:BoundField DataField="team_rating" HeaderText="key_team_rating" SortExpression="team_rating" />

                     <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="lnkEditTeam" runat="server" CausesValidation="false" CommandName="edit_team" Text="Edit" CommandArgument='<%# Bind("team_id") %>'></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                 <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="lnkDeleteTeam" runat="server" CausesValidation="false" OnClientClick="return confirm('Are you sure?');" CommandName="delete_team" Text="Delete" CommandArgument='<%# Bind("team_id") %>'></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:ObjectDataSource ID="odsGvTeam" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="CMS_NG_Sql_V1.dsTeamTableAdapters.taTeam" UpdateMethod="Update">
                <DeleteParameters>
                    <asp:Parameter Name="original_team_id" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="team_name" Type="String" />
                    <asp:Parameter Name="team_lang" Type="String" />
                    <asp:Parameter Name="team_status" Type="Int32" />
                    <asp:Parameter Name="team_key" Type="String" />
                    <asp:Parameter Name="team_rating" Type="Int32" />
                </InsertParameters>
                <SelectParameters>
                    <asp:Parameter DefaultValue="en" Name="team_lang" Type="String" />
                </SelectParameters>
                <UpdateParameters>
                    <asp:Parameter Name="team_name" Type="String" />
                    <asp:Parameter Name="team_lang" Type="String" />
                    <asp:Parameter Name="team_status" Type="Int32" />
                    <asp:Parameter Name="team_key" Type="String" />
                    <asp:Parameter Name="team_rating" Type="Int32" />
                    <asp:Parameter Name="team_id" Type="Int32" />
                </UpdateParameters>
            </asp:ObjectDataSource>

CodeBehing

Protected Sub gvTeam_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gvTeam.RowCommand

    Select Case e.CommandName.ToLower

        Case "edit_team"
            txtID.Value = e.CommandArgument
            Using objTeam As New cls_Team(CInt(txtID.Value))


                txtID.Value = objTeam.TeamDataRow.team_id
                txtLang.Text = objTeam.TeamDataRow.team_lang
                txtRating.Text = objTeam.TeamDataRow.team_rating
                txtTeamKey.Text = objTeam.TeamDataRow.team_key
                optStatus.SelectedValue = objTeam.TeamDataRow.team_status
                txtName.Text = objTeam.TeamDataRow.team_name
                MultiView1.SetActiveView(viwEditTeam)

            End Using
            AdminLogFile(CMS_NG_Sql_V1.enum_AdminLogFile.Edit)

        Case "delete_team"
            Dim intID As Integer = e.CommandArgument
            'Response.Redirect("admin_team.aspx?IDTeam=" & intID)
            Using objTeam As New cls_Team(intID)

                objTeam.Delete(intID)


                AdminLogFile(CMS_NG_Sql_V1.enum_AdminLogFile.Delete)
                gvTeam.DataBind()
            End Using

    End Select
End Sub

案例&#34; delete_team&#34;工作正常

但案例&#34; edit_team&#34;没工作 我收到了错误:

enter image description here

我也使用了名为(dsTeam.xsd)的数据集

如何解决IT?

1 个答案:

答案 0 :(得分:0)

team_rating 列中的值为NULL。数据库NULL与VB中的字符串变量不同,可能具有空值。在为txtRating.Text指定值之前,您需要检查这个:

If Not objTeam.TeamDataRow.IsNull("team_rating") Then
    txtRating.Text = objTeam.TeamDataRow.team_rating
End If