我得到一個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
您不能在您的示例中將'cmd'分配給'GridView1.DataSource'。 – Sam
爲什麼不!它從這個教程... http://www.aspsnippets.com/Articles/GridView---Add-Edit-Update-Delete-and-Paging-the-AJAX-way.aspx –
該示例不直接分配一個'OleDbCommadn'到數據源,因爲它不能。如果你看這個例子,作者將'cmd'變量傳遞給'GetData'函數'GetData(cmd)',它很可能執行存儲過程並返回一個支持DataSource的類型(例如'IListSource','IEnumerable'或'IDataSource'。 – Sam