G
Greg Collins [Microsoft MVP]
I have a SQL2005 XML column I load into a DataSet.
I then bind to a DataGrid.
If I set an asp:Literal text value to the DataSet.GetXml() when I first load it into the DataSet, it displays correctly.
If I later try to display the XML content in the asp:Literal control, then I get "<NewDataSet />". Why is that? Why can't I get the actual XML back from the DataSet? I'm new to DataSets, so I'm probably just missing something basic.
Here's the code... I'm leaving out the ASP.NET markup... if you feel you need to see that too, please let me know.
Basically, I have a text box where the user enters a SQL record #, then they click a button to load it. Then a second button is clicked to display the XML content in the literal control. Currently I also fill the literal with the XML in the first button click for debug purposes.
public partial class SqlXml : System.Web.UI.Page
{
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
ds = new DataSet();
}
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);
ds.ReadXml(comm.ExecuteXmlReader());
lit.Text = Server.HtmlEncode(ds.GetXml());
conn.Close();
DataGrid1.DataSource = ds;
DataGrid1.DataMember = "Goal";
DataGrid1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
lit.Text = Server.HtmlEncode(ds.GetXml());
}
}
I then bind to a DataGrid.
If I set an asp:Literal text value to the DataSet.GetXml() when I first load it into the DataSet, it displays correctly.
If I later try to display the XML content in the asp:Literal control, then I get "<NewDataSet />". Why is that? Why can't I get the actual XML back from the DataSet? I'm new to DataSets, so I'm probably just missing something basic.
Here's the code... I'm leaving out the ASP.NET markup... if you feel you need to see that too, please let me know.
Basically, I have a text box where the user enters a SQL record #, then they click a button to load it. Then a second button is clicked to display the XML content in the literal control. Currently I also fill the literal with the XML in the first button click for debug purposes.
public partial class SqlXml : System.Web.UI.Page
{
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
ds = new DataSet();
}
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);
ds.ReadXml(comm.ExecuteXmlReader());
lit.Text = Server.HtmlEncode(ds.GetXml());
conn.Close();
DataGrid1.DataSource = ds;
DataGrid1.DataMember = "Goal";
DataGrid1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
lit.Text = Server.HtmlEncode(ds.GetXml());
}
}