Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All


my DGV database UPDATE Time This Error Show..

Unable to cast object of type 'System.Data.DataTable' to type 'System.Windows.Forms.BindingSource'.


Here I am USING CODE FOR UPDATE FUNCION
VB
Me.Validate()
 Dim bindingSource1 As New BindingSource

        Dim DTable As New DataTable()
        Me.bindingSource1 = Me.dgvView.DataSource
        DTable = Me.bindingSource1.DataSource
        Me.DaAp.Update(DTable)
        MessageBox.Show("UPDATED SUCESSFULLY")
        DTable.AcceptChanges()



Please Tell Me How Can i Do to database UPDATE using DGV
Posted
Updated 23-Feb-13 9:36am
v2

1 solution

You REALLY need to go through a VB.NET book. This code is meaningless spaghetti.

You don't creat a new bindingsource to do a database update. On top of that, you're creating a new empty DataTable, then ignoring it and letting it go out of scope.

You normally have your data in a DataTable object, bind the Grid to it, and when the user wants to submit changes, you call the Update method of a DataAdapter configured to do the update. This is not a replacement for your code, but it comes down to this:
MyDgv.EndEdit()
MyDataAdapter.Update(myDataTable)
MessageBox.Show("UPDATED SUCESSFULLY")

The rest of that code you posted is meaningless garbage.
 
Share this answer
 
Comments
Navas Khanj 24-Feb-13 5:09am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900