Datagrid Error

F

Flyguy

Please help me figure out what is going wrong. I am getting an error when
trying to update a record using a datagrid control. My datagrid control is
modifying data from an XML file.


The XML file contains the following data:

<?xml version="1.0" encoding="utf-8" ?>
<users>
<user id ="aSmith" password="test1"/>
<user id ="tjones" password="test2"/>
</users>


The aspx file contains the following code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" BackColor="White"
BorderColor="White" BorderStyle="Ridge" BorderWidth="2px"
CellPadding="3" CellSpacing="1"
DataSourceID="UsersXmlDataSource" GridLines="None">
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<Columns>
<asp:CommandField ButtonType="Button" ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id"
SortExpression="id" />
<asp:BoundField DataField="password" HeaderText="password"
SortExpression="password" />
</Columns>
<PagerStyle BackColor="#C6C3C6" ForeColor="Black"
HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True"
ForeColor="White" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True"
ForeColor="#E7E7FF" />
</asp:GridView>
<asp:XmlDataSource ID="UsersXmlDataSource" runat="server"
DataFile="~/App_Data/Users.xml">
</asp:XmlDataSource>

</div>
</form>
</body>
</html>


The error that is thrown is the following:

Specified method is not supported.
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.NotSupportedException: Specified method is not
supported.
 
S

Steven Cheng [MSFT]

Hi Flyguy,

From your description, you're encountering some problem whene try perform
edit/update in GridView against a XML datasource(assocate with a xml file)
, correct?

Based on my understanding, the error is due to the XMLDataSource doesn't
support built-in edit/update like other DataSource(such as SqlDataSource or
ObjectDataSource). If you need to do editing/updating, I think you need to
add the following code logic into your page:

1. add a edit/update command field(as you've already done)

2. register the GridView's "RowUpdating" event and add your code to load
the XML file and update the certain element/record in it.

e.g.

===================
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs
e)
{
// here you can get all the parameters from the
GridViewUpdateEventArgs parameter

//and you can load the xml file and update the corresponding record in
it here.

//need to Cancel the event so as not to let it popagate to build in
updating event pipeline(which will result to error)
e.Cancel = true;
}


===================

here is the complete test page I've used for your reference:

======== aspx =================

<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="XmlDataSource1"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="id" HeaderText="id"
SortExpression="id" />
<asp:BoundField DataField="password" HeaderText="password"
SortExpression="password" />
</Columns>
</asp:GridView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/XMLDir/Data.xml"
XPath="/users/user"></asp:XmlDataSource>

</div>
</form>


=======================code behind====================

protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
Response.Write("<br/>index: " + e.RowIndex);
Response.Write("<br/>old values: ");

foreach (object obj in e.OldValues.Keys)
{
Response.Write("<br/>" + obj + ": " + e.OldValues[obj]);
}

Response.Write("<br/>new values: ");

foreach (object obj in e.NewValues.Keys)
{
Response.Write("<br/>" + obj + ": " + e.NewValues[obj]);
}

e.Cancel = true;
}
=============================

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
 
S

Stan

The XmlDataSource is for reading and filtering only. XML files are not
typically edited like databases.
If you want to build in editing facilites for an Xml file then use an
ObjectDataSource linked to a Class file containing all the necessary
code for selecting, updating, deleting and inserting. Works OK I have
done it many times.
 
F

Flyguy

Steven,
Thanks so much for your help. That worked great. You guys are a massive
help to me. Thanks again.


Steven Cheng said:
Hi Flyguy,

From your description, you're encountering some problem whene try perform
edit/update in GridView against a XML datasource(assocate with a xml file)
, correct?

Based on my understanding, the error is due to the XMLDataSource doesn't
support built-in edit/update like other DataSource(such as SqlDataSource or
ObjectDataSource). If you need to do editing/updating, I think you need to
add the following code logic into your page:

1. add a edit/update command field(as you've already done)

2. register the GridView's "RowUpdating" event and add your code to load
the XML file and update the certain element/record in it.

e.g.

===================
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs
e)
{
// here you can get all the parameters from the
GridViewUpdateEventArgs parameter

//and you can load the xml file and update the corresponding record in
it here.

//need to Cancel the event so as not to let it popagate to build in
updating event pipeline(which will result to error)
e.Cancel = true;
}


===================

here is the complete test page I've used for your reference:

======== aspx =================

<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="XmlDataSource1"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="id" HeaderText="id"
SortExpression="id" />
<asp:BoundField DataField="password" HeaderText="password"
SortExpression="password" />
</Columns>
</asp:GridView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/XMLDir/Data.xml"
XPath="/users/user"></asp:XmlDataSource>

</div>
</form>


=======================code behind====================

protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
Response.Write("<br/>index: " + e.RowIndex);
Response.Write("<br/>old values: ");

foreach (object obj in e.OldValues.Keys)
{
Response.Write("<br/>" + obj + ": " + e.OldValues[obj]);
}

Response.Write("<br/>new values: ");

foreach (object obj in e.NewValues.Keys)
{
Response.Write("<br/>" + obj + ": " + e.NewValues[obj]);
}

e.Cancel = true;
}
=============================

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
From: =?Utf-8?B?Rmx5Z3V5?= <[email protected]>
Subject: Datagrid Error
Date: Wed, 25 Jun 2008 13:13:06 -0700
Please help me figure out what is going wrong. I am getting an error when
trying to update a record using a datagrid control. My datagrid control is
modifying data from an XML file.


The XML file contains the following data:

<?xml version="1.0" encoding="utf-8" ?>
<users>
<user id ="aSmith" password="test1"/>
<user id ="tjones" password="test2"/>
</users>


The aspx file contains the following code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" BackColor="White"
BorderColor="White" BorderStyle="Ridge" BorderWidth="2px"
CellPadding="3" CellSpacing="1"
DataSourceID="UsersXmlDataSource" GridLines="None">
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<Columns>
<asp:CommandField ButtonType="Button" ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id"
SortExpression="id" />
<asp:BoundField DataField="password" HeaderText="password"
SortExpression="password" />
</Columns>
<PagerStyle BackColor="#C6C3C6" ForeColor="Black"
HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True"
ForeColor="White" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True"
ForeColor="#E7E7FF" />
</asp:GridView>
<asp:XmlDataSource ID="UsersXmlDataSource" runat="server"
DataFile="~/App_Data/Users.xml">
</asp:XmlDataSource>

</div>
</form>
</body>
</html>
 
S

Steven Cheng [MSFT]

Thanks for your reply.

I'm very glad that the suggestion helps. If there is anything else we can
help, welcome to post in the newsgroup.

Have a nice day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).


--------------------
From: =?Utf-8?B?Rmx5Z3V5?= <[email protected]>
References: <[email protected]>
Subject: RE: Datagrid Error
Date: Mon, 30 Jun 2008 18:38:00 -0700
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top