asp.net无法转换类型为' System.String'的对象输入' System.Data.DataTable'用VB

时间:2015-09-29 08:09:13

标签: asp.net vb.net

我有一个问题,为什么我会收到错误:

  

无法转换类型为' System.String'的对象键入System.Data.DataTable

Protected Sub gvInqProqurement_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    Try
        If e.CommandName.Equals("AddNew") Then
            Dim txtItem As TextBox = gvInqProqurement.FooterRow.FindControl("txtItem")
            Dim txtItemMaterial As TextBox = gvInqProqurement.FooterRow.FindControl("txtItemMaterial")
            Dim txtItemDesc As TextBox = gvInqProqurement.FooterRow.FindControl("txtItemDesc")
            Dim txtItemSize As TextBox = gvInqProqurement.FooterRow.FindControl("txtItemSize")
            Dim txtItemBrand As TextBox = gvInqProqurement.FooterRow.FindControl("txtItemBrand")
            Dim txtItemQty As TextBox = gvInqProqurement.FooterRow.FindControl("txtItemQty")
            Dim ddlItemUnit As DropDownList = gvInqProqurement.FooterRow.FindControl("ItemUnit")
            Dim txtCerNeeds As TextBox = gvInqProqurement.FooterRow.FindControl("txtCerNeeds")
            Dim txtRemarks As TextBox = gvInqProqurement.FooterRow.FindControl("txtRemarks")
            Dim hfItem As HiddenField = gvInqProqurement.FooterRow.FindControl("hfItem")

            If String.IsNullOrEmpty(txtItem.Text) Then
                Throw New Exception("Invalid Data. Item Must Be Filled In.")
            End If

            Dim dtItem As New DataTable
            dtItem = ViewState("InqProcurement_New.dtItem")

            For i As Integer = 0 To dtItem.Rows.Count - 1
                If hfItem.Value = dtItem.Rows(i).Item("FKItem").ToString Then
                    Throw New Exception("Item Already Selected.")
                End If
            Next


            If Not String.IsNullOrEmpty(dtItem.Rows(0).Item(0).ToString()) Then
                dtItem.Rows.Add()
            Else
                gvInqProqurement.Rows(0).Cells(9).Visible = True
                gvInqProqurement.Rows(0).Cells(10).Visible = True
            End If
            dtItem.Rows(dtItem.Rows.Count - 1).Item("ItemCode") = txtItem.Text
            dtItem.Rows(dtItem.Rows.Count - 1).Item("ItemMaterial") = txtItemMaterial.Text
            dtItem.Rows(dtItem.Rows.Count - 1).Item("ItemDescription") = txtItemDesc.Text
            dtItem.Rows(dtItem.Rows.Count - 1).Item("ItemSize") = txtItemSize.Text
            dtItem.Rows(dtItem.Rows.Count - 1).Item("BrandName") = txtItemBrand.Text
            dtItem.Rows(dtItem.Rows.Count - 1).Item("Quantity_InqProcurement") = txtItemQty.Text
            dtItem.Rows(dtItem.Rows.Count - 1).Item("FKUnit") = ddlItemUnit.SelectedValue
            dtItem.Rows(dtItem.Rows.Count - 1).Item("FKCertificate") = txtItemQty.Text
            dtItem.Rows(dtItem.Rows.Count - 1).Item("Remarks") = txtItemQty.Text

            ViewState("InqProcurement_New.dtItem") = dtItem

            gvInqProqurement.DataSource = dtItem
            gvInqProqurement.DataBind()
        End If
    Catch ex As Exception
        Me.cvErrorMessage.IsValid = False
        Me.cvErrorMessage.ErrorMessage = ex.Message
    End Try
End Sub

如何解决此错误?

4 个答案:

答案 0 :(得分:0)

只需更改以下代码

即可
  If String.IsNullOrEmpty(ViewState("InqProcurement_New.dtItem").Tostring())=false then

        If Not String.IsNullOrEmpty(Convert.Tostring(dtItem.Rows(0).Item(0))) Then
            dtItem.Rows.Add()
        Else
            gvInqProqurement.Rows(0).Cells(9).Visible = True
            gvInqProqurement.Rows(0).Cells(10).Visible = True
        End If
  

在这种情况下,如果你使用.ToString()方法,那么我们的程序应该抛出null引用异常。在Convert.ToString()中,我们的程序不会抛出任何异常。 bcoz默认采用空值而不是null。

答案 1 :(得分:0)

这可能不是您遇到错误的原因,但您可能在第一行中出现语法错误。

这一行:

If Not String.IsNullOrEmpty(dtItem.Rows(0).Item(0).ToString) Then

应如下:

If Not String.IsNullOrEmpty(dtItem.Rows(0).Item(0).ToString()) Then

答案 2 :(得分:0)

检查行ViewState("InqProcurement_New.dtItem")

dtItem = ViewState("InqProcurement_New.dtItem")的类型

它可能返回一个字符串值。

答案 3 :(得分:0)

使用Ctype将从viewstate返回的值转换为DataTable。您甚至可以使用DirectCast

Dim dtItem As New DataTable
if ViewState("InqProcurement_New.dtItem") isnot nothing then
    dtItem = CType(ViewState("InqProcurement_New.dtItem"),DataTable)
end if

当它为空时你可能需要考虑这个案例......