updating a MS Access 2000 Database through a dataset on a datagrid.

  • Thread starter Tom LaFontaine via .NET 247
  • Start date
T

Tom LaFontaine via .NET 247

I am trying to update an access database using a datagrid with adataset( VS 2003). When I click the Update link on the grid itdoesn't fire and does not come out of the edit mode. My primarykey is TABID (autonumber field) and Fld_name and Fld_domainwhich are both text fields.

Here is my code, if some one could please help that would begreat.

Thanks.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles MyBase.Load
OleDbDataAdapter1.Fill(Site1)
If Not IsPostBack Then
DataGrid1.DataBind()
End If

End Sub

Private Sub MyDataGrid1_CancelCommand(ByVal source As Object,ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)Handles DataGrid1.CancelCommand
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
MsgBox("ok")
End Sub

Private Sub MyDataGrid1_EditCommand(ByVal source As Object,ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub

Private Sub MyDataGrid1_UpdateCommand(ByVal source AsSystem.Object, ByVal e AsSystem.Web.UI.WebControls.DataGridCommandEventArgs)
Dim Name, domain As String

' Gets the value of the key field of the row beingupdated
Dim TABID As String =DataGrid1.DataKeys(e.Item.ItemIndex).ToString()

' Gets get the value of the controls (textboxes) that theuser
' updated. The DataGrid columns are exposed as the Cellscollection.
' Each cell has a collection of controls. In this case,there is only one
' control in each cell -- a TextBox control. To get itsvalue,
' you copy the TextBox to a local instance (whichrequires casting)
' and extract its Text property.
'
' The first column -- Cells(0) -- contains the Update andCancel buttons.
Dim tb As TextBox
tb = CType(e.Item.Cells(1).Controls(0), TextBox)
TABID = tb.Text

' Gets the value the TextBox control in the third column
tb = CType(e.Item.Cells(3).Controls(0), TextBox)
Name = tb.Text

' Gets the value the TextBox control in the fourthcolumn
tb = CType(e.Item.Cells(4).Controls(0), TextBox)
domain = tb.Text

' Finds the row in the dataset table that matches the
' one the user updated in the grid. This example uses a
' special Find method defined for the typed dataset,which
' returns a reference to the row.
Dim r As site.tbl_aspx_hostingRow
r = Site1.tbl_aspx_hosting.FindByTABID(TABID)

' Updates the dataset table.
r.fld_name = Name
r.fld_domain = domain
' Calls a SQL statement to update the database from thedataset
OleDbDataAdapter1.Update(Site1)
' Takes the DataGrid row out of editing mode
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()

End Sub
 
S

Scott M.

Your "Private Sub MyDataGrid1_UpdateCommand" does not have "Handles
DataGrid1.UpdateCommand" at the end of the declaration. That is why it is
not firing.


I am trying to update an access database using a datagrid with a dataset( VS
2003). When I click the Update link on the grid it doesn't fire and does
not come out of the edit mode. My primary key is TABID (autonumber field)
and Fld_name and Fld_domain which are both text fields.

Here is my code, if some one could please help that would be great.

Thanks.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
OleDbDataAdapter1.Fill(Site1)
If Not IsPostBack Then
DataGrid1.DataBind()
End If

End Sub

Private Sub MyDataGrid1_CancelCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.CancelCommand
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
MsgBox("ok")
End Sub

Private Sub MyDataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub

Private Sub MyDataGrid1_UpdateCommand(ByVal source As System.Object,
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
Dim Name, domain As String

' Gets the value of the key field of the row being updated
Dim TABID As String =
DataGrid1.DataKeys(e.Item.ItemIndex).ToString()

' Gets get the value of the controls (textboxes) that the user
' updated. The DataGrid columns are exposed as the Cells collection.
' Each cell has a collection of controls. In this case, there is
only one
' control in each cell -- a TextBox control. To get its value,
' you copy the TextBox to a local instance (which requires casting)
' and extract its Text property.
'
' The first column -- Cells(0) -- contains the Update and Cancel
buttons.
Dim tb As TextBox
tb = CType(e.Item.Cells(1).Controls(0), TextBox)
TABID = tb.Text

' Gets the value the TextBox control in the third column
tb = CType(e.Item.Cells(3).Controls(0), TextBox)
Name = tb.Text

' Gets the value the TextBox control in the fourth column
tb = CType(e.Item.Cells(4).Controls(0), TextBox)
domain = tb.Text

' Finds the row in the dataset table that matches the
' one the user updated in the grid. This example uses a
' special Find method defined for the typed dataset, which
' returns a reference to the row.
Dim r As site.tbl_aspx_hostingRow
r = Site1.tbl_aspx_hosting.FindByTABID(TABID)

' Updates the dataset table.
r.fld_name = Name
r.fld_domain = domain
' Calls a SQL statement to update the database from the dataset
OleDbDataAdapter1.Update(Site1)
' Takes the DataGrid row out of editing mode
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top