Thursday, April 12, 2012

NotImplementedException XML Reader/Writer

I'm trying to write/read xml from gridview and database. i'm getting the error: NotImplementedException
 
Here is the code:
Default.aspx
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Insert Edit Update and Delete selected row in GridView </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" 
              DataKeyNames="ID"
              DataSourceID="SqlDataSource1" 
              OnRowDeleted="GridView1_RowDeleted" 
              OnRowUpdated="GridView1_RowUpdated" 
              ShowFooter="true" 
              OnRowCommand="GridView1_RowCommand">
<Columns>
    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
    <asp:TemplateField HeaderText="ID" SortExpression="ID">
    <ItemTemplate>
    <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>'>
    </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:Button ID="btnInsert" runat="server" Text="Insert" CommandName="Add" />
    </FooterTemplate>
    </asp:TemplateField>
    
    <asp:TemplateField HeaderText="FirstName" 
                       SortExpression="FirstName">
    <ItemTemplate>
    <asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtFirstName" runat="server" Text='<%#Bind("FirstName") %>'>
    </asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtFname" runat="server">
    </asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    
    <asp:TemplateField HeaderText="LastName" SortExpression="LastName">
    <ItemTemplate>
    <asp:Label ID="lblLastName" runat="server" Text='<%#Eval("LastName") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtLastName" runat="server" Text='<%#Bind("LastName") %>'>
    </asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtLname" runat="server">
    </asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    
    <asp:TemplateField HeaderText="Department" SortExpression="Department">
    <ItemTemplate>
    <asp:Label ID="lblDepartment" runat="server" Text='<%#Eval("Department") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtDepartmentName" runat="server" Text='<%#Bind("Department") %>'>
    </asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtDept" runat="server">
    </asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    
    <asp:TemplateField HeaderText="Location" SortExpression="Location">
    <ItemTemplate>
    <asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtLocation" runat="server" Text='<%#Bind("Location") %>'>
    </asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtLoc" runat="server">
    </asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString2%>"
DeleteCommand="DELETE FROM [Employees] WHERE 4929527 = @ID" 
InsertCommand="INSERT INTO [Employees] ([FirstName], 
[LastName],[Department], [Location]) 
VALUES (@FirstName, @LastName, @Department, @Location)"
SelectCommand="SELECT 4929527, [FirstName], [LastName], 
[Department], [Location] FROM [Employees]"
UpdateCommand="UPDATE [Employees] SET 
[FirstName] = @FirstName, [LastName] = @LastName, 
[Department] = @Department, [Location] = @Location 
WHERE 4929527 = @ID" OnInserted="SqlDataSource1_Inserted">

<DeleteParameters>
    <asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
    <asp:Parameter Name="FirstName" Type="String" />
    <asp:Parameter Name="LastName" Type="String" />
    <asp:Parameter Name="Department" Type="String" />
    <asp:Parameter Name="Location" Type="String" />
    <asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
    <asp:Parameter Name="FirstName" Type="String" />
    <asp:Parameter Name="LastName" Type="String" />
    <asp:Parameter Name="Department" Type="String" />
    <asp:Parameter Name="Location" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<p><asp:Label ID="lblMessage" runat="server" Font-Bold="True"></asp:Label><tr><td colspan="2" align="right">
<asp:Button ID="btnWrtXml" runat="server" Text="Write XML File" 
        onclick="btnWrtXml_Click" style="height: 26px"/>
</p>
<p>Read XML File :
<asp:Button id="btnRdXml" text="Read XML File" runat="server" onclick="btnRdXml_Click"/>
<br/>
<asp:label id="lblXml" runat="server"/></p></div>
</form>
</body>
</html>

Default.aspx.vb
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml
Imports System.Text

Public Partial Class _Default
 Inherits System.Web.UI.Page

    Dim strLastName As String
    Dim strDepartment As String
    Dim strLocation As String
    Dim txtLastName As String
    Dim txtDept As String
    Dim txtLname As String
    Dim txtLoc As String

    Private Property txtDepartment As String

    Protected Sub Page_Load(sender As Object, e As EventArgs)
        lblMessage.Text = ""
    End Sub
 Protected Sub GridView1_RowUpdated(sender As Object, e As GridViewUpdatedEventArgs)
  lblMessage.Text = "Record Updated"
 End Sub
 Protected Sub GridView1_RowDeleted(sender As Object, e As GridViewDeletedEventArgs)
  lblMessage.Text = "Record Deleted"
 End Sub
 Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
  If e.CommandName = "Add" Then
   Dim strFirstName As String = DirectCast(GridView1.FooterRow.FindControl("txtFname"), TextBox).Text

   Dim strLastName As String = DirectCast(GridView1.FooterRow.FindControl("txtLname"), TextBox).Text

   Dim strDepartment As String = DirectCast(GridView1.FooterRow.FindControl("txtDept"), TextBox).Text
   Dim strLocation As String = DirectCast(GridView1.FooterRow.FindControl("txtLoc"), TextBox).Text
   'SqlDataSource1.InsertParameters.Clear();
   'SqlDataSource1.InsertParameters.Add
   '("FirstName", strFirstName);
   'SqlDataSource1.InsertParameters.Add
   '("LastName", strLastName);
   'SqlDataSource1.InsertParameters.Add
   '("Department", strDepartment);
   'SqlDataSource1.InsertParameters.Add
   '("Location", strLocation);

   SqlDataSource1.InsertParameters("FirstName").DefaultValue = strFirstName
   SqlDataSource1.InsertParameters("LastName").DefaultValue = strLastName
   SqlDataSource1.InsertParameters("Department").DefaultValue = strDepartment
   SqlDataSource1.InsertParameters("Location").DefaultValue = strLocation
   SqlDataSource1.Insert()
  End If
 End Sub
 Protected Sub SqlDataSource1_Inserted(sender As Object, e As SqlDataSourceStatusEventArgs)
  lblMessage.Text = "Record Inserted"
    End Sub
    Protected Sub btnWrtXml_Click(sender As Object, e As EventArgs)
        Dim xmlWriter As New XmlTextWriter(Server.MapPath("Employees.xml"), Encoding.UTF8)
        xmlWriter.WriteStartDocument()
        'Create Parent element
        xmlWriter.WriteStartElement("Employees")
        'Create Child elements
        xmlWriter.WriteStartElement("Details")
        xmlWriter.WriteElementString("txtFname", strFirstName)
        xmlWriter.WriteElementString("txtLname", strLastName)
        xmlWriter.WriteElementString("txtDept", strDepartment)
        xmlWriter.WriteElementString("txtLoc", strLocation)
        xmlWriter.WriteEndElement()

        'End writing top element and XML document
        xmlWriter.WriteEndElement()
        xmlWriter.WriteEndDocument()
        xmlWriter.Close()
    End Sub
    Protected Sub btnRdXml_Click(sender As Object, e As EventArgs)
        RdXmlFile(Server.MapPath("Employees.xml"))
    End Sub
    Protected Sub RdXmlFile(xmlFile As String)
        Dim parentElementName As String = ""
        Dim childElementName As String = ""
        Dim childElementValue As String = ""
        Dim element As Boolean = False
        lblXml.Text = ""

        Dim xmlReader As New XmlTextReader(xmlFile)
        While xmlReader.Read()
            If xmlReader.NodeType = XmlNodeType.Element Then
                If element Then
                    parentElementName = parentElementName & childElementName & "<br/>"
                End If
                element = True
                childElementName = xmlReader.Name
            ElseIf xmlReader.NodeType = XmlNodeType.Text Or xmlReader.NodeType = XmlNodeType.CDATA Then
                element = False
                childElementValue = xmlReader.Value
                lblXml.Text = lblXml.Text + "<b>" & parentElementName & "<br/>" & childElementName & "</b><br/>" & childElementValue
                parentElementName = ""
                childElementName = ""
            End If
        End While
        xmlReader.Close()
    End Sub

    Private Function strFirstName() As String
        Throw New NotImplementedException
    End Function

   

End Class

No comments:

Post a Comment