Saturday, March 24, 2012

Waht is wrong with this code

Hi

I can duplicate an employee info in a XMl, but i do have a problem with delete option. I recreated the same error message which i am getting in my project. So Could anyone test this code and tell me what is wrong with code,

<%@dotnet.itags.org. page language="VB" debug="true" trace='true' %>
<%@dotnet.itags.org. Import Namespace="System.Data" %>
<%@dotnet.itags.org. Import Namespace="System.Data.SqlClient" %>
<%@dotnet.itags.org. Import Namespace="System.IO" %>
<%@dotnet.itags.org. Import Namespace="System.XML" %>
<%@dotnet.itags.org. Import Namespace="System.Xml.Serialization" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataList ID="listEmployees" DataKeyField="EmployeeId" CellPadding='2' BorderWidth=0
cssClass=small OnItemDataBound="ListEmployees_ItemDataBound" OnItemCommand="ListEmployees_ItemCommand" Runat='server'>
<ItemTemplate>
<table cellspacing='0' cellpadding='5' borer='0' width='95%'>
<tr>
<td width='460' class='small'>
<asp:Label ID="lblEmployeeName" Runat=server CssClass=Normal /><br />
<asp:linkbutton id="lnkPreview" Text='Preview' CommandName="Preview" cssClass='small' runat="server" /> |
<asp:linkbutton id="lnkEdit" Text='Edit' CommandName="Edit" cssClass='small' runat="server" /> |
<asp:linkbutton id="lnkDuplicate" Text='Duplicate' CommandName="Duplicate" cssClass='small' runat="server" /><asp:Label ID='lblDuplicateSep' CssClass='small' text=" |" Runat=server />
<asp:linkbutton id="lnkDelete" Text='Delete' CommandName="Delete" cssClass='small' runat="server" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>
<script runat="server">
<XmlInclude(GetType(MyEmployee))> _
Public Class MyCompany
Dim _CompanyName As String
Dim _UserName As String
Dim _CompanyAddress As String
Dim _MyEmployee As New ArrayList

Public Property UserName() As String
Get
Return _UserName
End Get
Set(ByVal Value As String)
_UserName = Value
End Set
End Property
Public Property CompanyName() As String
Get
Return _CompanyName
End Get
Set(ByVal Value As String)
_CompanyName = Value
End Set
End Property
Public Property CompanyAddress() As String
Get
Return _CompanyAddress
End Get
Set(ByVal Value As String)
_CompanyAddress = Value
End Set
End Property
Public Property MyEmployee() As ArrayList
Get
Return _MyEmployee
End Get
Set(ByVal Value As ArrayList)
_MyEmployee = Value
End Set
End Property
End Class

Public Class MyEmployee
Dim _Row_Id as string
Dim _EmployeeId As String
Dim _EmployeeName as string
Dim _EmployeeDept As String
Dim _EmployeeAddress As string
Dim _EmployeeDesig As String

Public Property Row_Id() As String
Get
Return _Row_Id
End Get
Set(ByVal Value As String)
_Row_Id = Value
End Set
End Property
Public Property EmployeeName() As String
Get
Return _EmployeeName
End Get
Set(ByVal Value As String)
_EmployeeName = Value
End Set
End Property
Public Property EmployeeId() As String
Get
Return _EmployeeId
End Get
Set(ByVal Value As String)
_EmployeeId = Value
End Set
End Property
Public Property EmployeeDept() As String
Get
Return _EmployeeDept
End Get
Set(ByVal Value As String)
_EmployeeDept = Value
End Set
End Property

Public Property EmployeeAddress() As String
Get
Return _EmployeeAddress
End Get
Set(ByVal Value As String)
_EmployeeAddress = Value
End Set
End Property

Public Property EmployeeDesig() As String
Get
Return _EmployeeDesig
End Get
Set(ByVal Value As String)
_EmployeeDesig = Value
End Set
End Property
End Class

Public Class CompanyWriter
Public Sub SerializeObject(ByVal filename As String, ByVal CompanyObject As MyCompany)

Dim serializer As New XmlSerializer(GetType(MyCompany))

' Create a StreamWriter to write with. First create a FileStream
' object, and create the StreamWriter specifying an Encoding to use.

Dim fs As New FileStream(filename, FileMode.OpenOrCreate)
Dim writer As New StreamWriter(fs, New UTF8Encoding)
' Serialize using the XmlTextWriter.
serializer.Serialize(writer, CompanyObject)
writer.Close()
fs.Close()
writer = Nothing
fs = Nothing
End Sub

Public Function DeSerializeObject(ByVal filename As String, ByVal CompanyObject As MyCompany)
' Create an instance of the XmlSerializer specifying type and namespace.
Dim serializer As New XmlSerializer(GetType(MyCompany))

' A FileStream is needed to read the XML document.
Dim fs As New StreamReader(File.OpenRead(filename))
Dim reader As New XmlTextReader(fs)

' Use the Deserialize method to restore the object's state.
CompanyObject = CType(serializer.Deserialize(reader), MyCompany)
reader.Close()
fs.Null.Close()
fs = Nothing
Return CompanyObject
End Function
End Class

Dim EmployeeCount As Integer
Dim CompanyPath As String
Dim ThisCompany
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Session.Add("MyCompany", New MyCompany)
ThisCompany = new MyCompany
ThisCompany.UserName = "test"
Dim MyCompanyWriter As New CompanyWriter
CompanyPath = Request.PhysicalApplicationPath & "Samples\" & ThisCompany.UserName & ".xml"

If Not System.IO.File.Exists(CompanyPath) Then
'Redirect to other page
End If
ThisCompany = MyCompanyWriter.DeSerializeObject(CompanyPath, ThisCompany)

EmployeeCount = ThisCompany.MyEmployee.Count
If Not (IsPostBack) Then
If EmployeeCount > 0 Then
listEmployees.DataSource = CreateDataSource()
listEmployees.DataBind()
End If
End If

end sub

Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
MyBase.OnPreRender(e)
EmployeeCount = ThisCompany.MyEmployee.Count
End Sub

Function CreateDataSource() As DataView
Dim ThisEmployee As MyEmployee
Dim EmployeeDT As DataTable
Dim EmployeeDR As DataRow
'ThisEmployee.
EmployeeDT = New DataTable
EmployeeDT.Columns.Add(New DataColumn("Row_Id", GetType(String)))
EmployeeDT.Columns.Add(New DataColumn("EmployeeId", GetType(String)))
EmployeeDT.Columns.Add(New DataColumn("EmployeeName", GetType(String)))
EmployeeDT.Columns.Add(New DataColumn("EmployeeAddress", GetType(String)))
EmployeeDT.Columns.Add(New DataColumn("EmployeeDept", GetType(String)))
EmployeeDT.Columns.Add(New DataColumn("EmployeeDesig", GetType(String)))

For Each ThisEmployee In ThisCompany.MyEmployee
EmployeeDR = EmployeeDT.NewRow()
EmployeeDR(0) = ThisEmployee.Row_id
EmployeeDR(1) = ThisEmployee.EmployeeId
EmployeeDR(2) = ThisEmployee.EmployeeName
EmployeeDR(3) = ThisEmployee.EmployeeAddress
EmployeeDR(4) = ThisEmployee.EmployeeDept
EmployeeDR(5) = ThisEmployee.EmployeeDesig
EmployeeDT.Rows.Add(EmployeeDR)
Next

Dim DV As DataView = New DataView(EmployeeDT)
Return DV
End Function

Public Sub ListEmployees_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
'Dim lnkbutton As System.Web.UI.WebControls.LinkButton = CType(e.Item.Controls(2), System.Web.UI.WebControls.LinkButton)
CType(e.Item.FindControl("lnkDelete"), LinkButton).Attributes.Add("onclick", "return confirm (""Are you sure that youn want to delete this Company?"");")
CType(e.Item.FindControl("lblEmployeeName"), Label).Text = "Employee " & CType(e.Item.DataItem, DataRowView).Row.ItemArray(0).ToString()

If CType(e.Item.DataItem, DataRowView).DataView.Count = 5 Then
CType(e.Item.FindControl("lnkDuplicate"), LinkButton).Visible = False
CType(e.Item.FindControl("lblDuplicateSep"), Label).Visible = False
Else
CType(e.Item.FindControl("lblDuplicateSep"), Label).Visible = True
End If

End If
End Sub

Public Sub ListEmployees_ItemCommand(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
'Dim ThisCompany As MyCompany
Dim Command As String = e.CommandName
Dim RowView As DataRowView = CreateDataSource()(e.Item.ItemIndex)
Select Case (Command)
Case "Preview"

Case "Edit"

Case "Duplicate"
Dim DuplicateEmployee As MyEmployee
Dim exists As Boolean = False
Trace.Warn("duplicate RowView", RowView("Row_Id"))
For Each DuplicateEmployee In ThisCompany.MyEmployee
If DuplicateEmployee.Row_Id = RowView("Row_Id") Then
ThisCompany.MyEmployee.add(DuplicateEmployee)
UpDateDB()
Trace.Warn(" duplicate", DuplicateEmployee.EmployeeId)
DuplicateEmployee = Nothing
exists = True
Exit For
End If
Next
If exists Then
Dim tempEmployee As MyEmployee
tempEmployee = ThisCompany.MyEmployee(ThisCompany.MyEmployee.Count - 1)
tempEmployee.Row_Id = ThisCompany.MyEmployee.Count
UpDateDB()
listEmployees.DataSource = CreateDataSource()
listEmployees.DataBind()
End If
exists = False

Case "Delete"
ThisCompany.MyEmployee.RemoveRange(CType(RowView("Row_Id"), Integer) - 1, 1)
Dim ThisEmp As MyEmployee
Dim J as integer = 1
For Each ThisEmp In ThisCompany.MyEmployee
ThisEmp.Row_Id = j
j += 1
Next
UpDateDB()
listEmployees.DataSource = CreateDataSource()
listEmployees.DataBind()
End Select

Response.Redirect(Page.Request.Url.PathAndQuery)
End Sub
Sub UpDateDB()
Dim CompanyPath As String
CompanyPath = Request.PhysicalApplicationPath & "Samples\" & ThisCompany.UserName & ".xml"
Dim MyCompanyWriter As New CompanyWriter
MyCompanyWriter.SerializeObject(CompanyPath, ThisCompany)

ThisCompany = MyCompanyWriter.DeSerializeObject(CompanyPath, ThisCompany)
End Sub
</script
Thank YOuThat is an awful lot of unformatted code, and I doubt anyone has access to your database to attempt to run the code themselves. You could help us help you if you would:

* tell us what error message you are getting
* limit the code posted to that which seems to be causing the error
* try using the debugger to find out more about what is going on and pass that along, too.
Hi
I am getting the following error

The data at the root level is invalid. Line 32, position 13.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Xml.XmlException: The data at the root level is invalid. Line 32, position 13.
*************

I debug the code, and i could not find the solution. The final XML after Delete option is not well formed XML. the deleted node is appending at the end of the root node which causing the problem

before delete command
<?xml version="1.0" encoding="utf-8"?>
<MyCompany xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<UserName>Test</UserName>
<CompanyName>ABC Inc</CompanyName>
<CompanyAddress>123 avenuw</CompanyAddress>
<MyEmployee>
<anyType xsi:type="MyEmployee">
<Row_Id>1</Row_Id>
<EmployeeName>Nathan</EmployeeName>
<EmployeeId>1</EmployeeId>
<EmployeeDept>IT</EmployeeDept>
<EmployeeAddress>12 avenue</EmployeeAddress>
<EmployeeDesig>Manager</EmployeeDesig>
</anyType>
<anyType xsi:type="MyEmployee">
<Row_Id>2</Row_Id>
<EmployeeName>Nathan</EmployeeName>
<EmployeeId>123</EmployeeId>
<EmployeeDept>IT</EmployeeDept>
<EmployeeAddress>123 main street</EmployeeAddress>
<EmployeeDesig>Manager</EmployeeDesig>
</anyType>
</MyEmployee>
</MyCompany
after delete command i am getting
<?xml version="1.0" encoding="utf-8"?>
<MyCompany xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<UserName>Test</UserName>
<CompanyName>ABC Inc</CompanyName>
<CompanyAddress>123 avenuw</CompanyAddress>
<MyEmployee>
<anyType xsi:type="MyEmployee">
<Row_Id>1</Row_Id>
<EmployeeName>Nathan</EmployeeName>
<EmployeeId>1</EmployeeId>
<EmployeeDept>IT</EmployeeDept>
<EmployeeAddress>12 avenue</EmployeeAddress>
<EmployeeDesig>Manager</EmployeeDesig>
</anyType>
</MyEmployee>
</MyCompany>oyee">
<Row_Id>4</Row_Id>
<EmployeeName>Nathan</EmployeeName>
<EmployeeId>1</EmployeeId>
<EmployeeDept>IT</EmployeeDept>
<EmployeeAddress>0</EmployeeAddress>
<EmployeeDesig>Manager</EmployeeDesig>
</anyType>
</MyEmployee>
</MyCompany>
Yep i agree, here is the eror anyway

0 comments:

Post a Comment