2012-09-19 35 views
0

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

当我尝试通过gridview将新记录添加到数据库时出现这个错误,所以我很好地将数据从数据库中获取到了这个gridview中,因此我不明白当数据库出现异常时catch catch不可用。 @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 
+0

您不能在您的示例中将'cmd'分配给'GridView1.DataSource'。 – Sam

+0

为什么不!它从这个教程... http://www.aspsnippets.com/Articles/GridView---Add-Edit-Update-Delete-and-Paging-the-AJAX-way.aspx –

+0

该示例不直接分配一个'OleDbCommadn'到数据源,因为它不能。如果你看这个例子,作者将'cmd'变量传递给'GetData'函数'GetData(cmd)',它很可能执行存储过程并返回一个支持DataSource的类型(例如'IListSource','IEnumerable'或'IDataSource'。 – Sam

回答

0

如果回答你原来的问题上面我的评论(引号下面贴在这里)得到的回答是,那么你应该纪念这个问题的回答,然后发布一个全新的问题,让你获得更准确的答复。这个问题不再适用于您的实际问题。

我的答案,在你的评论,解决了你原来的问题:

这个例子(指上文您的评论的链接)不直接分配 的OleDbCommand到数据源,因为它 不能。如果您查看示例,作者将cmd变量 传递给GetData函数GetData(cmd),该函数很可能会执行 存储过程并返回支持DataSource的类型(例如, IListSource,IEnumerable或IDataSource)。

相关问题