473,383 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

XML, DataSet, DataGrid

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());
}
}

--
Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )
May 18 '07 #1
3 2083

The member variable (ds) does not persist .......

You have to persist it somewhere.

So basically, when you click the button, your code does a page_load, and all
you have is (ds = new DataSet())

You have to persist the ds to something.

Session, Application, ViewState .... something if you want to "reclaim it"
on a postback page.

"Greg Collins [Microsoft MVP]" <gcollins_AT_msn_DOT_comwrote in message
news:eu**************@TK2MSFTNGP02.phx.gbl...
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());
}
}

--
Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )

May 18 '07 #2
Here is a refactor:

public partial class SqlXml : System.Web.UI.Page
{
DataSet m_modelDS;
protected void Page_Load(object sender, EventArgs e)
{
// m_modelDS= new DataSet(); // you don't need this
}

private void LoadData()

{

if (null!= Session["mykey"] )
{
m_modelDS = Session["mykey"] as DataSet;
if (null!= this.m_modelDS)
{
return;
}
}
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);

m_modelDS = new DataSet();//you may or may not needt his I cant
remember off hand.

m_modelDS.ReadXml(comm.ExecuteXmlReader());
lit.Text = Server.HtmlEncode(ds.GetXml());

conn.Close();

}

private void PersistData()
{
if (null!= this.m_modelDS)
{
Session["mykey"] = this.m_modelDS;
}
}

private void BindData()
{
if(null!= this.m_modelDS)
{
DataGrid1.DataSource = m_modelDS;
DataGrid1.DataMember = "Goal";
DataGrid1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{

LoadData()
PersistData();
BindData()

}
protected void Button2_Click(object sender, EventArgs e)
{
LoadData(); // follow this, and if its not the first run thru, it
shoudl get teh Session object .

lit.Text = Server.HtmlEncode(m_modelDS.GetXml());
}
}


"Greg Collins [Microsoft MVP]" <gcollins_AT_msn_DOT_comwrote in message
news:eu**************@TK2MSFTNGP02.phx.gbl...
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());
}
}

--
Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )

May 18 '07 #3
Maybe I'm just too novice with this stuff.

What my original intention was (though surely I missed in my execution of it) was to
1. Pull XML from SQL
2. Load it into a DataSet.
3. Bind it to some ASP.NET control for editing.
4. Allow editing of the data.
5. Get the updated data from the control as XML
6. Post it back to SQL.

The sample code was an attempt to do 1-5 above... my novice assumption was that the dataset would automatically be updated during the edtiting of the datagrid due to the binding--therefore it seemed the most likely place to go for the XML, which was surprisingly blank.

As I understand your update, button2 will pull the stored "original" xml from the session variable, and not the updated xml after the user has completed their edits.

--
Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )
May 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steve Donnor | last post by:
Here's my question.. I have a Dataset which has populated a datagrid with information from an AS400 That SQL looks like this "Select MASYS, MAFLD, MADATA, MAEQV from QS36F.MAPDATA WHERE MASYS =...
0
by: Frosty | last post by:
Hi I am using the VS xsd designer to create a strongly typed dataset. The dataset is apparently successfully created, with no warnings or errors given. Is it not then to be expected that this...
3
by: Diego TERCERO | last post by:
Hi... I'm working on a tool for editing text resources for a family of software product my company produces. These text resources are found in a SQL Server database, in a table called...
1
by: Krzysztof Karnicki | last post by:
I have such a problem… I have create my custom DataGridColumn inheriting from System.Windows.Forms.DataGridColumnStyle on using it on DataGrid, to show rows painted by me self. Because dates ...
2
by: Alpha | last post by:
Hi, I have a window based program. One of the form has several textboxes and a datagrid. The textboxes are bind to the same dataset table as the datagrid and the text changes to reflect different...
3
by: Ben Becker | last post by:
I am trying to build a custom crosstab type of grid where I take some items in a data grid and based on the content of the current item compared to the previous item, determine if a new row in a...
2
by: Bennett Haselton | last post by:
I know how to create a DataAdapter that loads data from a data source into a table in a typed DataSet, and how to set the DataSource and DataMember properties of a DataGrid so that at run time it...
0
by: Bob Davies | last post by:
Hi I have a webservice that retrieves data from a database, this is then returned to the calling client application built in windows forms within a dataset, however upon attempting to create...
3
by: Datatable Dataset Datagrid help | last post by:
Hi I am somewhat confused, I am new at VB.net I use XML data, I have a datagrid, I created a datatable so that I can create a custom format like true is this graphic false is this graphic and...
17
by: A_PK | last post by:
I have problem databinding the DataGrid with DataView/DataSet after the filter... I create the following proceudre in order for user to filter as many as they want, but the following code is only...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.