updating database from datagrid

  • Thread starter Thread starter T.K Kullervo
  • Start date Start date
T

T.K Kullervo

Hi,

Im sorry, but i have never really used a database from VB. Im showing data
from a
database via a datagrid and i want the user to be able to update the records
by writing on the datagrid. Could someone help me with this code

Dim cn2 As OleDbConnection
Dim strSelect2 As String
Dim dscmd2 As OleDbDataAdapter
Dim ds2 As New DataSet
Dim dt2 As DataTable
Dim strConnection As String

Public Sub openDb()
Dim key As Microsoft.Win32.RegistryKey
key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("EN_DIN_ASME")
Dim path As String = CType(key.GetValue("pathTo"), String)

Dim strConnection As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& path
cn2 = New OleDbConnection(strConnection)
cn2.Open()
End Sub

Public Sub GetData()
strSelect2 = "SELECT..."
dscmd2 = New OleDbDataAdapter(strSelect2, cn2)
dscmd2.Fill(ds2, "ENDIMASME")
dt2 = ds2.Tables.Item("ENDIMASME")
syottogrid.DataSource = ds2
syottogrid.DataMember = "ENDIMASME"
End Sub

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
dscmd2.Update(dt2)
End Sub

When i click the update button it says:
Update requires a valid UpdateCommand when passed DataRow collection with
modified rows.

Help me.....
 
Hi,

Im sorry, but i have never really used a database from VB. Im showing
data from a
database via a datagrid and i want the user to be able to update the
records by writing on the datagrid. Could someone help me with this
code

Dim cn2 As OleDbConnection
Dim strSelect2 As String
Dim dscmd2 As OleDbDataAdapter
Dim ds2 As New DataSet
Dim dt2 As DataTable
Dim strConnection As String

Public Sub openDb()
Dim key As Microsoft.Win32.RegistryKey
key =
Microsoft.Win32.Registry.CurrentUser.OpenSubKey("EN_DIN_ASME")
Dim path As String = CType(key.GetValue("pathTo"), String)

Dim strConnection As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& path
cn2 = New OleDbConnection(strConnection)
cn2.Open()
End Sub

Public Sub GetData()
strSelect2 = "SELECT..."
dscmd2 = New OleDbDataAdapter(strSelect2, cn2)
dscmd2.Fill(ds2, "ENDIMASME")
dt2 = ds2.Tables.Item("ENDIMASME")
syottogrid.DataSource = ds2
syottogrid.DataMember = "ENDIMASME"
End Sub

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles Button2.Click
dscmd2.Update(dt2)
End Sub

When i click the update button it says:
Update requires a valid UpdateCommand when passed DataRow collection
with modified rows.

You've told the DataAdapter how to get records from the database with
your select command. However, how it is supposed to know how to Insert,
Delete, or Update a record?

The DataAdapter has 4 commands on it with corresponding properties.
"SelectCommand" is being set by the constructor of DataAdapter that you
are using. The other 3 (UpdateCommand, InsertCommand, DeleteCommand) are
all null until you set them to a valid command.

dscmd2.UpdateCommand = new OleDbCommand("Update ...");
dscmd2.DeleteCommand = new OleDbCommand("Delete ...");
dscmd2.InsertCommand = new OleDbCommand("Insert ...");

I have some samples of how to do all of this and more in a data
application in freely downloadable templates on the database app code
generator project in my signature. Take a hard look at the class it
generates for a single table in the data layer.

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/colcodegen (simple code generator)
http://sourceforge.net/projects/dbobjecter (database app code generator)
http://sourceforge.net/projects/genadonet ("generic" ADO.NET)
 
You could use the OleDbCommandBuilder object to build your Update, Delete and Insert commands. Check the .NET Framework Documentation and search for this object,
it should have plenty of sample codes. If you know how to build theses command manually, then you could assign these commands to the DataAdapter but its a lot of work
that can be avoided simply by using the above object.


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
 
Thanks for the help guys.. I actually got it to work..

Hussein Abuthuraya said:
You could use the OleDbCommandBuilder object to build your Update, Delete
and Insert commands. Check the .NET Framework Documentation and search for
this object,
it should have plenty of sample codes. If you know how to build theses
command manually, then you could assign these commands to the DataAdapter
but its a lot of work
that can be avoided simply by using the above object.


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
 
Back
Top