Editable SQL05 XML in ASP.NET

  • Thread starter Greg Collins [Microsoft MVP]
  • Start date
G

Greg Collins [Microsoft MVP]

What do people use when they want to pull XML from a SQL05 XML datatype column, edit it in an ASP.NET web app, and the post the updated XML back to SQL05?

I've looked into pulling the XML down into an XmlDataSource control, but this doesn't seem to be a very likely option as the bindings are readonly.

Seems like a common enough scenario--what have you done (those who have done this)?
 
G

Greg Collins [Microsoft MVP]

Yes, I pull the SQL XML column down and place it in an XmlDocument, and then I put that into an XmlDataSource, but then I can't read it back out from there...

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=SqlXmlTest;Integrated Security=True");
conn.Open();

string sql = "SELECT [Data] FROM [Record] WHERE ([UserId] = " + tbUserId.Text + ")";
SqlCommand comm = new SqlCommand(sql, conn);
XmlReader reader = comm.ExecuteXmlReader();
XmlDocument doc = new XmlDocument();
doc.Load(reader);

reader.Close();
conn.Close();

XmlDataSource1.Data = doc.OuterXml;
Repeater1.DataSourceID = "XmlDataSource1";
}

protected void Button2_Click(object sender, EventArgs e)
{
string sql = "UPDATE [Record] SET [Data] = '" + XmlDataSource1.Data +"' WHERE ([UserId] = " + tbUserId.Text + ")";
Response.Write(sql);
}

The Button1 click works fine... but clicking Button2, XmlDataSource1.Data (which is supposed to be read/write) gives me an empty string.

In the web page, I bound the data to a text box control, so I could try editing it to see what would happen... but I just get a blank trying to read the Data property no matter what.

Ideas?
 
B

Bruno Piovan

Hi Greg, I never did that before, but have you tried to put the xml in a
XmlDocument class?

Bruno

"Greg Collins [Microsoft MVP]" <gcollins_AT_msn_DOT_com> wrote in message
What do people use when they want to pull XML from a SQL05 XML datatype
column, edit it in an ASP.NET web app, and the post the updated XML back to
SQL05?

I've looked into pulling the XML down into an XmlDataSource control, but
this doesn't seem to be a very likely option as the bindings are readonly.

Seems like a common enough scenario--what have you done (those who have done
this)?
 
G

Greg Collins [Microsoft MVP]

I'm still fairly new to a lot of this. I have XML/XSLT/HTML background, and I've got my feet wet in ASP.NET and SQL -- but a lot of these asp:controls are still new to my. Still trying to wrap my head around them as quickly as possible.

How complex of an XML structure can a dataset handle? If not very, maybe I can do multiple datasets and pull various parts of the document out... I'd have to see if that would work.

I'm assuming it's fairly easy to drop an XML doc into a dataset? Do you have a small code sample or a pointer to an article?

Thanks!
 
B

Bruno Piovan

Hi greg,
according to the documentation, the XmlDataSource control is tipically used
on read-only scenarios. My suggestion would be to load the xml into a
dataset (assuming you can change the xml accordingly if needed), this will
create a datatable filled in the dataset, then you use this datatable as the
datasource of a gridview for example...

what you think?

Bruno

"Greg Collins [Microsoft MVP]" <gcollins_AT_msn_DOT_com> wrote in message
Yes, I pull the SQL XML column down and place it in an XmlDocument, and then
I put that into an XmlDataSource, but then I can't read it back out from
there...

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data
Source=.\SQLEXPRESS;Initial Catalog=SqlXmlTest;Integrated Security=True");
conn.Open();

string sql = "SELECT [Data] FROM [Record] WHERE ([UserId] = " +
tbUserId.Text + ")";
SqlCommand comm = new SqlCommand(sql, conn);
XmlReader reader = comm.ExecuteXmlReader();
XmlDocument doc = new XmlDocument();
doc.Load(reader);

reader.Close();
conn.Close();

XmlDataSource1.Data = doc.OuterXml;
Repeater1.DataSourceID = "XmlDataSource1";
}

protected void Button2_Click(object sender, EventArgs e)
{
string sql = "UPDATE [Record] SET [Data] = '" + XmlDataSource1.Data +"'
WHERE ([UserId] = " + tbUserId.Text + ")";
Response.Write(sql);
}

The Button1 click works fine... but clicking Button2, XmlDataSource1.Data
(which is supposed to be read/write) gives me an empty string.

In the web page, I bound the data to a text box control, so I could try
editing it to see what would happen... but I just get a blank trying to read
the Data property no matter what.

Ideas?
 
B

Bruno Piovan

Hi Greg,
The underlying data of a DataSet is stored as XML.

Below is a code that creates a DataSet, a DataTable, DataColumns and then
add some sample data.

protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("MyDataTable");

dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));

ds.Tables.Add(dt);

dt.Rows.Add(new object[] { 1, "Bruno 1" }); /* the values passed
here are in the order of creation of the datacolumns, "ID" first, "Name"
second */
dt.Rows.Add(new object[] { 2, "Bruno 2" });
dt.Rows.Add(new object[] { 3, "Bruno 3" });

Response.Write("<pre>" + Server.HtmlEncode(ds.GetXml()) + "</pre>");
}

The GetXml method returns the underlying xml... this is to show you how the
xml structure is, and below is a code that you would use to "bind" this xml
to a GridView control.

protected void Button2_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();

/* fills the dataset with the xml from db */
ds.ReadXml(command.ExecuteXmlReader());

GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}

I don't know how the scructure of your xml is, but if you can fit it in a
DataSet it would be a good way to do what you want, try to add more
DataTables to the DataSet and see the output xml. Check also the
GetXmlSchema and ReadXmlSchema methods of the DataSet.

More info on DataSet here
http://msdn2.microsoft.com/en-us/library/system.data.dataset.aspx

Bruno
 

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,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top