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
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