0

データソースが無効なタイプであり、IListSource、IEnumerable、または IDataSource のいずれかのタイプである必要があるという例外エラーとして catch exp を取得します。

このエラーは、グリッドビューを介してデータベースに新しいレコードを追加しようとすると発生するため、データベースからこのグリッドビューにデータを適切に取得するため、データベースが利用できない場合に catch exp を例外として取得することを理解していません。@thesli_number OleDbType.VarChar 値 = 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

エディット.......エディット......エディット........ .....編集

これで、グリッドビューにデータを追加したり、レコードを削除したり、新しいレコードを追加したりできます。しかし、更新イベントを機能させることができません。この新しいコードの何が問題なのかわかりますか!?

    '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
4

1 に答える 1

0

元の質問に対する回答が上記の私のコメントで回答されている場合 (以下に引用符で投稿)、この質問を回答済みとしてマークしてから、まったく新しい質問を投稿して、より正確な回答を得てください。この質問は、実際の問題には当てはまりません。

あなたのコメントで、元の質問を解決した私の答え:

その例 (上記のコメントのリンクを参照) では、 OleDbCommandを DataSource に直接割り当てる ことはできません。例を見ると、作成者は cmd 変数を GetData 関数 GetData(cmd) に渡します。これはストアド プロシージャを実行し、DataSource がサポートする型 (IListSource、IEnumerable、IDataSource など) を返す可能性が高いです。

于 2012-09-20T14:35:49.070 に答える