I have a datagrid that is successfully loading an XML file. My goal is
to also edit the XML data in this same datagrid. I used an Edit,
Update, Cancel "hyperlink style button" in a column. This successfully
displays the data to be edited in nice textboxes. I can change the data
and hit update but it will not update the file. Here is my
vb.net scripting if anyone can help... Thanks!
<script runat="server">
Private Function MakeDataView() as DataView
Dim myDataSet As New DataSet()
myDataSet.ReadXml(Server.MapPath("Kennedy.xml"))
Dim view As DataView = New DataView(myDataSet.Tables(0))
view.AllowDelete = False
view.AllowEdit = False
view.AllowNew = False
view.Sort = "Year ASC"
Return view
End Function
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim view as DataView = MakeDataView()
dgEvents.DataSource = view
dgEvents.AllowSorting = True
If Not Page.IsPostBack Then
dgEvents.DataBind()
End If
End Sub
Sub dgEvents_UpdateRow(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Using ds As New DataSet()
ds.ReadXml(Server.MapPath("Kennedy.xml"))
dgEvents.EditItemIndex = e.Item.ItemIndex
Dim yearTextBox As TextBox = e.Item.Cells(2).Controls(0)
Dim eventTextBox As TextBox = e.Item.Cells(3).Controls(0)
Dim iEventID As Integer = dgEvents.DataKeys
(e.Item.ItemIndex)
' Dim dr As DataRow = ds.Tables(0).
' dr("event") = eventTextBox.Text
' dr("year") = yearTextBox.Text
' ds.WriteXml(Server.MapPath("Kennedy.xml"))
End Using
'dgEvents.EditItemIndex = -1
'DataBind()
End Sub
This isn't something I've tried to do before, but I'll take a stab. You code looks ok with the exception of row selection. I see you saying ds = DataRow = ds.Tables(0)... But where are you saying find row where EventId = IEventID? If you're doing that
and simply ommitted that code, is your attempt at an update throwing an exception? If so, what? If not, can you confirm that the UpdateRow delegate is being called?
Ben
MCAD .Net (70-315, 70-320 & 70-229)
:One that throws dirt loses ground:
Sub dgEvents_UpdateRow(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Using ds As New DataSet()
ds.ReadXml(Server.MapPath("Kennedy.xml"))
dgEvents.EditItemIndex = e.Item.ItemIndex
Dim yearTextBox As TextBox = e.Item.Cells(2).Controls(0)
Dim eventTextBox As TextBox = e.Item.Cells(3).Controls(0)
Dim iEventID As Integer = dgEvents.DataKeys
(e.Item.ItemIndex) ds.Tables(0).Rows(e.Item.ItemIndex)("event") = eventTextBox.Text
Sorry had to create new user name (forgot password).
Anyway I used this:
Sub dgEvents_UpdateRow(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Using ds As New DataSet()
ds.ReadXml(Server.MapPath("Kennedy.xml"))
dgEvents.EditItemIndex = e.Item.ItemIndex
Dim yearTextBox As TextBox = e.Item.Cells(2).Controls(0)
Dim eventTextBox As TextBox = e.Item.Cells(3).Controls(0)
Dim iEventID As Integer = dgEvents.DataKeys(e.Item.ItemIndex)
ds.Tables(0).Rows(e.Item.ItemIndex)("event") = eventTextBox.Text
ds.Tables(0).Rows(e.Item.ItemIndex)("year") = yearTextBox.Text
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("Kennedy.xml"))
End Using
dgEvents.EditItemIndex = -1 ' note I removed the comment out
DataBind() ' note I removed the comment out
End Sub
But got a server Error:
Unable to cast object of type 'System.Web.UI.WebControls.DataGridLinkButton' to type 'System.Web.UI.WebControls.TextBox'.
on this line: Dim eventTextBox As TextBox = e.Item.Cells(3).Controls(0)
Sub dgEvents_UpdateRow(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Using ds As New DataSet()
ds.ReadXml(Server.MapPath("Kennedy.xml"))
dgEvents.EditItemIndex = e.Item.ItemIndex Dim eventTextBox As TextBox = e.Item.Cells(2).Controls(0)
Dim yearTextBox As TextBox = e.Item.Cells(3).Controls(0)
Dim iEventID As Integer = dgEvents.DataKeys(e.Item.ItemIndex)
ds.Tables(0).Rows(e.Item.ItemIndex)("event") = eventTextBox.Text
ds.Tables(0).Rows(e.Item.ItemIndex)("year") = yearTextBox.Text
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("Kennedy.xml"))
End Using
dgEvents.EditItemIndex = -1
DataBind()
End Sub
I changed the two lines in bold above but running still gives me the same error.
For Bound Fields you can use Text property see below
Sub dgEvents_UpdateRow(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Using ds As New DataSet()
ds.ReadXml(Server.MapPath("Kennedy.xml"))
dgEvents.EditItemIndex = e.Item.ItemIndex Dim iEventID As Integer = dgEvents.DataKeys(e.Item.ItemIndex)
ds.Tables(0).Rows(e.Item.ItemIndex)("event") = e.Item.Cells(1)..Text
ds.Tables(0).Rows(e.Item.ItemIndex)("year") = e.Item.Cells(2)..Text
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("Kennedy.xml"))
End Using
dgEvents.EditItemIndex = -1
DataBind()
End Sub
This makes sense and I believe it is about to work. It ran as far as the update then gave an error message "Access to the path 'E:\kunden\homepages\26\d190091667\Kennedy.xml' is denied."
Which is strange since I can read the XML obviously since it populates my datagrid (?)
i got the permissions issue resolved but when I go to edit a row I change a value and hit update... but instead of saving the edited value a NEW row is added.
Eugene1969
Member
2 Points
5 Posts
Updating XML file - urgent!
Jan 07, 2009 08:07 PM|LINK
I have a datagrid that is successfully loading an XML file. My goal is
to also edit the XML data in this same datagrid. I used an Edit,
Update, Cancel "hyperlink style button" in a column. This successfully
displays the data to be edited in nice textboxes. I can change the data
and hit update but it will not update the file. Here is my
vb.net scripting if anyone can help... Thanks!
<script runat="server">
Private Function MakeDataView() as DataView
Dim myDataSet As New DataSet()
myDataSet.ReadXml(Server.MapPath("Kennedy.xml"))
Dim view As DataView = New DataView(myDataSet.Tables(0))
view.AllowDelete = False
view.AllowEdit = False
view.AllowNew = False
view.Sort = "Year ASC"
Return view
End Function
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim view as DataView = MakeDataView()
dgEvents.DataSource = view
dgEvents.AllowSorting = True
If Not Page.IsPostBack Then
dgEvents.DataBind()
End If
End Sub
Sub dgEvents_UpdateRow(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Using ds As New DataSet()
ds.ReadXml(Server.MapPath("Kennedy.xml"))
dgEvents.EditItemIndex = e.Item.ItemIndex
Dim yearTextBox As TextBox = e.Item.Cells(2).Controls(0)
Dim eventTextBox As TextBox = e.Item.Cells(3).Controls(0)
Dim iEventID As Integer = dgEvents.DataKeys
(e.Item.ItemIndex)
' Dim dr As DataRow = ds.Tables(0).
' dr("event") = eventTextBox.Text
' dr("year") = yearTextBox.Text
' ds.WriteXml(Server.MapPath("Kennedy.xml"))
End Using
'dgEvents.EditItemIndex = -1
'DataBind()
End Sub
XML and XmlDataSource Control Datagrid
MuteThis
Participant
1126 Points
209 Posts
Re: Updating XML file - urgent!
Jan 08, 2009 04:10 AM|LINK
This isn't something I've tried to do before, but I'll take a stab. You code looks ok with the exception of row selection. I see you saying ds = DataRow = ds.Tables(0)... But where are you saying find row where EventId = IEventID? If you're doing that and simply ommitted that code, is your attempt at an update throwing an exception? If so, what? If not, can you confirm that the UpdateRow delegate is being called?
MCAD .Net (70-315, 70-320 & 70-229)
:One that throws dirt loses ground:
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: Updating XML file - urgent!
Jan 08, 2009 04:50 AM|LINK
Sub dgEvents_UpdateRow(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Using ds As New DataSet()
ds.ReadXml(Server.MapPath("Kennedy.xml"))
dgEvents.EditItemIndex = e.Item.ItemIndex
Dim yearTextBox As TextBox = e.Item.Cells(2).Controls(0)
Dim eventTextBox As TextBox = e.Item.Cells(3).Controls(0)
Dim iEventID As Integer = dgEvents.DataKeys
(e.Item.ItemIndex)
ds.Tables(0).Rows(e.Item.ItemIndex)("event") = eventTextBox.Text
ds.Tables(0).Rows(e.Item.ItemIndex)("year") = yearTextBox.Text
ds.acceptChanges()
ds.WriteXml(Server.MapPath("Kennedy.xml"))
End Using
'dgEvents.EditItemIndex = -1
'DataBind()
End Sub
Contact me
Eugene1968
Member
90 Points
360 Posts
Re: Updating XML file - urgent!
Jan 08, 2009 02:14 PM|LINK
Sorry had to create new user name (forgot password).
Anyway I used this:
Sub dgEvents_UpdateRow(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)Using ds As New DataSet()
ds.ReadXml(Server.MapPath("Kennedy.xml"))
dgEvents.EditItemIndex = e.Item.ItemIndex
Dim yearTextBox As TextBox = e.Item.Cells(2).Controls(0)
Dim eventTextBox As TextBox = e.Item.Cells(3).Controls(0)
Dim iEventID As Integer = dgEvents.DataKeys(e.Item.ItemIndex)
ds.Tables(0).Rows(e.Item.ItemIndex)("event") = eventTextBox.Text
ds.Tables(0).Rows(e.Item.ItemIndex)("year") = yearTextBox.Text
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("Kennedy.xml"))
End Using
dgEvents.EditItemIndex = -1 ' note I removed the comment out
DataBind() ' note I removed the comment out
End Sub
But got a server Error:
Unable to cast object of type 'System.Web.UI.WebControls.DataGridLinkButton' to type 'System.Web.UI.WebControls.TextBox'.
on this line: Dim eventTextBox As TextBox = e.Item.Cells(3).Controls(0)
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: Updating XML file - urgent!
Jan 08, 2009 02:21 PM|LINK
Do you have a TextBox in this column 4?
try replacing this
Dim eventTextBox As TextBox = e.Item.Cells(3).Controls(0)
Remeber its Zero based index that is 1st column has index 0
so fourth column will have index 3
Check that
Contact me
Eugene1968
Member
90 Points
360 Posts
Re: Updating XML file - urgent!
Jan 08, 2009 03:49 PM|LINK
1: <asp:BoundColumn HeaderText="Event ID" DataField="EventID" ReadOnly="true"/>
2: <asp:BoundColumn HeaderText="Event" DataField="event" /> 3. <asp:BoundColumn HeaderText="Year" DataField="year" />
4. <asp:EditCommandColumn CancelText="Cancel" EditText="Edit" UpdateText="Update">Sub dgEvents_UpdateRow(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Using ds As New DataSet()
ds.ReadXml(Server.MapPath("Kennedy.xml"))
dgEvents.EditItemIndex = e.Item.ItemIndex
Dim eventTextBox As TextBox = e.Item.Cells(2).Controls(0)
Dim yearTextBox As TextBox = e.Item.Cells(3).Controls(0)
Dim iEventID As Integer = dgEvents.DataKeys(e.Item.ItemIndex)
ds.Tables(0).Rows(e.Item.ItemIndex)("event") = eventTextBox.Text
ds.Tables(0).Rows(e.Item.ItemIndex)("year") = yearTextBox.Text
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("Kennedy.xml"))
End Using
dgEvents.EditItemIndex = -1
DataBind()
End Sub
I changed the two lines in bold above but running still gives me the same error.
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: Updating XML file - urgent!
Jan 08, 2009 04:23 PM|LINK
For Bound Fields you can use Text property see below
Sub dgEvents_UpdateRow(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Using ds As New DataSet()
ds.ReadXml(Server.MapPath("Kennedy.xml"))
dgEvents.EditItemIndex = e.Item.ItemIndex
Dim iEventID As Integer = dgEvents.DataKeys(e.Item.ItemIndex)
ds.Tables(0).Rows(e.Item.ItemIndex)("event") = e.Item.Cells(1)..Text
ds.Tables(0).Rows(e.Item.ItemIndex)("year") = e.Item.Cells(2)..Text
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("Kennedy.xml"))
End Using
dgEvents.EditItemIndex = -1
DataBind()
End Sub
It will work now
Contact me
Eugene1968
Member
90 Points
360 Posts
Re: Updating XML file - urgent!
Jan 08, 2009 06:48 PM|LINK
This makes sense and I believe it is about to work. It ran as far as the update then gave an error message "Access to the path 'E:\kunden\homepages\26\d190091667\Kennedy.xml' is denied."
Which is strange since I can read the XML obviously since it populates my datagrid (?)
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: Updating XML file - urgent!
Jan 09, 2009 03:59 AM|LINK
Its a permission issue Give full rights to the folder which has this file
Contact me
Eugene1969
Member
2 Points
5 Posts
Re: Updating XML file - urgent!
Jan 09, 2009 01:00 PM|LINK
i got the permissions issue resolved but when I go to edit a row I change a value and hit update... but instead of saving the edited value a NEW row is added.