I am developing an application where it displays a table that I have on the server and I would like the user to update it (insert/delete/update) directly within the application.
I created a telerik RadGridView
named definitionView
to display the data and I am following the instructions given directly from the Telerik Documentation: Updating the Database with ADO.NET
However I am stuck with the first part: Updating the database when the current row is changed
I tried to adapt the code shown in the example to my code but I keep getting an error on the adapter.Update()
statement.
Public Class definitionSolution
Private lastEditRow As DataRow = Nothing
Private bindingSource
Public Sub New()
InitializeComponent()
Dim bindingSource As New BindingSource
Dim dsSolution As New DataSet()
adapter.Fill(dsSolution, "MyTable")
bindingSource.DataSource = dsSolution.Tables("MyTable")
definitionView.DataSource = bindingSource
AddHandler bindingSource.CurrentChanged, AddressOf DefinitionView_CurrentChanged
End Sub
Private Sub AutoSavingDataBSEventsForm_Load(ByVal sender As Object, ByVal e As EventArgs)
' TODO: This line of code loads data into the 'nwindDataSet.Employees' table. You can move, or remove it, as needed.
Dim dsSolution As New DataSet()
adapter.Fill(dsSolution, "MyTable")
bindingSource.DataSource = dsSolution.Tables("MyTable")
Dim current As Object = bindingSource.Current
If current IsNot Nothing Then
Me.lastEditRow = (CType(current, DataRowView)).Row
End If
End Sub
Private Sub DefinitionView_CurrentChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim dataRow As DataRow = CType(CType(sender, BindingSource).Current, DataRowView).Row
If lastEditRow IsNot Nothing AndAlso lastEditRow.RowState = DataRowState.Modified Then
adapter.Update(lastEditRow)
End If
lastEditRow = dataRow
End Sub
End Class
SQLDataAdapter is initialized in another module as follow:
Public queryString = "SELECT * FROM [dbo].Mytable"
Public adapter As New SqlDataAdapter(queryString, DB_CONNECTION)
The error is the following:
Error BC30518 Overload resolution failed because no accessible 'Update' can be called with these arguments:
- Public Overrides Function Update(dataSet As DataSet) As Integer': Value of type 'DataRow' cannot be converted to 'DataSet
- Public Overloads Function Update(dataRows As DataRow()) As Integer': Value of type 'DataRow' cannot be converted to 'DataRow()
- Public Overloads Function Update(dataTable As DataTable) As Integer': Value of type 'DataRow' cannot be converted to 'DataTable
Does anybody has an idea on how to solve this issue?
Also from their doc:
Let's assume that we have an ADO.NET DataTable that loads its data from a SqlDataAdapter and it is bound to a BindingSource component. Further, the BindingSource component is bound to RadGridView control.
I understand that I have a DataSet from SqlDataAdapter that is bound to a BindingSource component but I miss the last bit. What does it mean to bound a BindingSource to the RadGridView control?
Edit after @jmcilhinney comments
Switching from adapter.Update(lastEditRow)
to adapter.Update(dsSolution)
gives me the following error:
System.Reflection.AmbiguousMatchException: 'Overload resolution failed because no Public 'Update' is most specific for these arguments:
'Public Overrides Function Update(dataSet As System.Data.DataSet) As Integer':
Not most specific.
'Public Function Update(dataRows As System.Data.DataRow()) As Integer':
Not most specific.
'Public Function Update(dataTable As System.Data.DataTable) As Integer':
Not most specific.'
Switching from adapter.Update(lastEditRow)
to adapter.Update(lastEditRow.ItemArray)
gives me the following error:
System.InvalidCastException: 'Unable to cast object of type 'System.Object[]' to type 'System.Data.DataRow[]'
CodePudding user response:
Your data adapter's Update
method accepts multiple rows and performs inserts, updates and deletes as required. That Update
method is overloaded and it will accept a DataRow
array, so you can put your single row into an array and pass that:
adapter.Update({lastEditRow})
Otherwise, you need to pass the whole DataTable
and let it pick out the row(s) that needs saving.