【问题标题】:Exception Error must be type IListSource, IEnumerable or IDataSource. Gridview异常错误必须是 IListSource、IEnumerable 或 IDataSource 类型。网格视图
【发布时间】:2012-09-19 18:15:28
【问题描述】:

我得到一个作为异常错误的 catch exp,其中说,数据源是无效类型,它必须是 IListSource、IEnumerable 或 IDataSource 类型。

当我尝试通过网格视图向数据库添加新记录时出现此错误,因此我将数据库中的数据很好地获取到此网格视图中,因此我不明白当数据库运行时我得到一个 catch exp 作为异常不可用。 @thesli_number OleDbType.VarChar Value = thenumber 是数据库中的数字类型。

 'Add new record to DB
Protected Sub AddNewTask(ByVal sender As Object, ByVal e As EventArgs)
    Dim thecat As String = DirectCast(GridView1.FooterRow.FindControl("txttestcat"), TextBox).Text
    Dim theinfo As String = DirectCast(GridView1.FooterRow.FindControl("txttestinfo"), TextBox).Text
    Dim thenumber As String = DirectCast(GridView1.FooterRow.FindControl("txttestnumber"), TextBox).Text

    Dim strSQL As String = ""
    strSQL = "" & _
    "INSERT INTO [TableTest] " & _
    "([test_cat], [test_info], [test_number])" & _
    "VALUES (@thesli_cat, @thesli_info, @thesli_number)"

    Using conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("MyConnStr").ConnectionString)
        Try
            conn.Open()
            Dim cmd As New OleDbCommand(strSQL, conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.Add("@thesli_cat", OleDbType.VarChar).Value = thecat
            cmd.Parameters.Add("@thesli_info", OleDbType.VarChar).Value = theinfo
            cmd.Parameters.Add("@thesli_number", OleDbType.VarChar).Value = thenumber
            GridView1.DataSource = cmd
            GridView1.DataBind()
            'MsgBox("Row(s) Added !! ")
        Catch exp As OleDbException
            If True Then
                MsgBox("Error trying to add current record. " & vbCrLf & "Error: " & exp.Message & "Database Error", MsgBoxStyle.OkOnly, MsgBoxStyle.Critical)
            End If
        Catch exp As Exception
            If True Then
                MsgBox("Error the Database can be unavailable atm. " & vbCrLf & "Error: " & exp.Message & "Database Error", MsgBoxStyle.OkOnly, MsgBoxStyle.Information)
            End If
        End Try
    End Using
End Sub

编辑……编辑…………编辑………… ........编辑

好的,我现在可以向 gridview 添加数据,我可以删除一条记录,也可以添加一条新记录。 但是我不能让更新事件工作,你能看到这个新代码有什么问题吗!?

    'Update record
Protected Sub UpdateTask(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    Dim theid = Convert.ToInt32(DirectCast(GridView1.FooterRow.FindControl("lbltestid"), Label).Text)
    Dim thecat As String = DirectCast(GridView1.FooterRow.FindControl("lbltestcat"), Label).Text
    Dim theinfo As String = DirectCast(GridView1.FooterRow.FindControl("lbltestinfo"), Label).Text
    Dim thenumber As String = DirectCast(GridView1.FooterRow.FindControl("lbltestnumber"), Label).Text

    Dim strSQL As String = ""
    strSQL = "" & _
    "UPDATE [TableTest] " & _
    "SET [test_cat] = @thesli_cat, [test_info] = @thesli_info, [test_number] = @thesli_number " & _
    "WHERE [test_id] = @thesli_id"

    Using conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("MyConnStr").ConnectionString)
        Try
            conn.Open()
            Dim cmd As New OleDbCommand(strSQL, conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.Add("@thesli_id", OleDbType.Integer).Value = theid
            cmd.Parameters.Add("@thesli_cat", OleDbType.VarChar).Value = thecat
            cmd.Parameters.Add("@thesli_info", OleDbType.VarChar).Value = theinfo
            cmd.Parameters.Add("@thesli_number", OleDbType.Integer).Value = thenumber
            cmd.ExecuteNonQuery()
            'MsgBox("Row(s) Updated !! ")
            GridView1.EditIndex = -1
            GetRecords()
        Catch exp As OleDbException
            If True Then
                MsgBox("Error trying to add current record. " & vbCrLf & "Error: " & exp.Message & "Database Error", MsgBoxStyle.OkOnly, MsgBoxStyle.Critical)
            End If
        Catch exp As Exception
            If True Then
                MsgBox("Error the Database can be unavailable atm. " & vbCrLf & "Error: " & exp.Message & "Database Error", MsgBoxStyle.OkOnly, MsgBoxStyle.Information)
            End If
        End Try
    End Using
End Sub

【问题讨论】:

  • 您不能将示例中的cmd 分配给GridView1.DataSource
  • 为什么不!?它来自本教程...aspsnippets.com/Articles/…
  • 该示例没有直接将OleDbCommadn 分配给DataSource,因为它不能。如果您查看示例,作者将cmd 变量传递给GetData 函数GetData(cmd),这很可能会执行存储过程并返回DataSource 支持的类型(例如IListSourceIEnumerable 或@987654332 @.
  • 嗨,山姆。好的,听起来不错。你可以吗?查看/阅读主线程中的编辑!?
  • 请为您的编辑提出一个新问题,因为它实际上是一个全新的问题。如果您包含遇到的错误,这也很有帮助。

标签: asp.net ado.net


【解决方案1】:

如果您的原始问题的答案在我上面的评论中得到了回答(在下面的引号中发布),那么您应该将此问题标记为已回答,然后发布一个全新的问题,以便获得更准确的回答。这个问题不再适用于您的实际问题。

我的回答在您的评论中解决了您最初的问题:

该示例(参考您上面评论中的链接)没有直接分配 OleDbCommand 到 DataSource,因为它 不能。如果您查看示例作者传递 cmd 变量 到 GetData 函数 GetData(cmd),它很可能会执行 存储过程并返回 DataSource 支持的类型(例如 IListSource、IEnumerable 或 IDataSource)。

【讨论】:

    猜你喜欢
    • 2017-02-04
    • 2017-04-25
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多