Solved

Modify record in xml from code behind on page load and unload events

Posted on 2009-04-06
2
297 Views
Last Modified: 2013-12-17
When my web page opens, I want it to modify a value in an xml file based on what's in its Page.Title, and when the page unloads, modify that value to "Not Broadcasting". I'm having a real hard time figuring this one out--I am going about this all wrong!  Is there an Expert here that can help?
--------###CODE BEHIND###--------
Dim hashTableData As New Hashtable
Dim strWebcastName As String
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'Reading xml file
            Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
            doc.Load(Server.MapPath("~/App_Data/WebcastDemos.xml"))
            Dim list As System.Xml.XmlNodeList = doc.SelectNodes("Records/Record")
            For Each node As System.Xml.XmlNode In list
                hashTableData(node.Item("ID").FirstChild.Value) = node.Item("Data").FirstChild.Value
            Next
            ViewState("Records") = hashTableData
        End If
 
        ' we do this to avoid multiple cast
        Dim myTable As Hashtable = CType(ViewState("Records"), Hashtable)
    End Sub
 
    Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
        strWebcastName = Page.Title
        Me.ModifyRecord()
    End Sub
 
    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        strWebcastName = "Not Broadcasting"
        Me.ModifyRecord()
    End Sub
 
    Private Sub ModifyRecord()
        Dim key As String = ViewState("HashKey")
 
        CType(ViewState("Records"), Hashtable)(key) = strWebcastName
 
        Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
        doc.Load(Server.MapPath("~/App_Data/WebcastDemos.xml"))
 
        Dim myNode As System.Xml.XmlNode = doc.SelectSingleNode("Records/Record[ID='" & key & "']")
        myNode("Data").InnerText = strWebcastName
 
        doc.Save(Server.MapPath("~/App_Data/WebcastDemos.xml"))
    End Sub

Open in new window

0
Comment
Question by:John Account
2 Comments
 
LVL 11

Accepted Solution

by:
anuragal earned 500 total points
ID: 24076543
Where you have set the value of ViewState("HashKey") ??
Anurag

0
 

Author Comment

by:John Account
ID: 24076855
I haven't. And I think the ViewState("HashKey") is completely unnecessary in this scenario, No? I mean, all I want to do is write to and modify a certain field in a certain record of the xml file, when opening the page, and then again when closing the page. So I shouldn't even need a ViewState, right? Just need a way to modify the value directly. Can you help?
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net Report Printing Issue 3 31
mail.To.Add 5 25
JQuery JavaScripot loop though RadioButtonList 4 23
Sql server insert 13 26
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now