473,729 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "<NewDataSe t />". 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.P age
{
DataSet ds;
protected void Page_Load(objec t sender, EventArgs e)
{
ds = new DataSet();
}
protected void Button1_Click(o bject sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@ "Data Source=.\SQLEXP RESS;Initial Catalog=SqlXmlT est;Integrated Security=True") ;
conn.Open();

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

ds.ReadXml(comm .ExecuteXmlRead er());
lit.Text = Server.HtmlEnco de(ds.GetXml()) ;

conn.Close();

DataGrid1.DataS ource = ds;
DataGrid1.DataM ember = "Goal";
DataGrid1.DataB ind();
}
protected void Button2_Click(o bject sender, EventArgs e)
{
lit.Text = Server.HtmlEnco de(ds.GetXml()) ;
}
}

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

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_ms n_DOT_comwrote in message
news:eu******** ******@TK2MSFTN GP02.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 "<NewDataSe t />". 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.P age
{
DataSet ds;
protected void Page_Load(objec t sender, EventArgs e)
{
ds = new DataSet();
}
protected void Button1_Click(o bject sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@ "Data
Source=.\SQLEXP RESS;Initial Catalog=SqlXmlT est;Integrated Security=True") ;
conn.Open();

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

ds.ReadXml(comm .ExecuteXmlRead er());
lit.Text = Server.HtmlEnco de(ds.GetXml()) ;

conn.Close();

DataGrid1.DataS ource = ds;
DataGrid1.DataM ember = "Goal";
DataGrid1.DataB ind();
}
protected void Button2_Click(o bject sender, EventArgs e)
{
lit.Text = Server.HtmlEnco de(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.P age
{
DataSet m_modelDS;
protected void Page_Load(objec t 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=.\SQLEXP RESS;Initial Catalog=SqlXmlT est;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.ReadX ml(comm.Execute XmlReader());
lit.Text = Server.HtmlEnco de(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.DataS ource = m_modelDS;
DataGrid1.DataM ember = "Goal";
DataGrid1.DataB ind();
}
}
protected void Button1_Click(o bject sender, EventArgs e)
{

LoadData()
PersistData();
BindData()

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

lit.Text = Server.HtmlEnco de(m_modelDS.Ge tXml());
}
}


"Greg Collins [Microsoft MVP]" <gcollins_AT_ms n_DOT_comwrote in message
news:eu******** ******@TK2MSFTN GP02.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 "<NewDataSe t />". 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.P age
{
DataSet ds;
protected void Page_Load(objec t sender, EventArgs e)
{
ds = new DataSet();
}
protected void Button1_Click(o bject sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@ "Data
Source=.\SQLEXP RESS;Initial Catalog=SqlXmlT est;Integrated Security=True") ;
conn.Open();

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

ds.ReadXml(comm .ExecuteXmlRead er());
lit.Text = Server.HtmlEnco de(ds.GetXml()) ;

conn.Close();

DataGrid1.DataS ource = ds;
DataGrid1.DataM ember = "Goal";
DataGrid1.DataB ind();
}
protected void Button2_Click(o bject sender, EventArgs e)
{
lit.Text = Server.HtmlEnco de(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
4445
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 = ? AND MAFLD = ? It populates a data adapter in which at that point I open a new dataset and I have the parameters and such setup including the proper update, insert and delete commands created as well. It works great I can update and insert and...
0
1977
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 dataset then should reflect all the details put into the xsd? Somewhere down the line there is not a 1:1 relationship. Be it that .NET is so tightly coupled to xsd, xml and dataset I would have expected any discrepancies to have been well documented and...
3
2547
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 "Resource" with the following structure : Resource{,en,fr,es} Yes.. these are the only languages supported actually. A couple of rows in that table would look like this :
1
2208
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 taken from database are very large, and filling DataSet bounded to this DataGrid takes some time, a decided to put fill logic into new thread. And so method running in this thread clears only this DataSet bounded to DataGrid and fills it once more...
2
4827
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 row selected in the datagrid. I want to save the changes that user make in the textboxes when they select a different row in the datagrid. I tried capturing the textbox.text at datagrid's CurrentCellChanged event but by then the textbox.text...
3
2052
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 table should be created or not. In order to do this, I need to have full control over the conditional logic for how items get displayed within a repeater element which I'm not seeing as possible. How can I cursor through a data set, apply...
2
8876
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 will load the data from that table in the DataSet. Assuming I've got two tables in the DataSet, how can I set the DataSource property of the DataGrid to be an inner join of the two tables in the DataSet? If I want the DataGrid to get data...
0
2594
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 tablestyles to format any of the columns within the datagrid, the exception "Can-not parent objects created on one thread to objects created on another" or words to that effect. I'm not too sure if what I said make sense, but i will add details to...
3
2520
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 others. One of the custom format is as follows: dsmessages_dt.Columns.Add("Image", GetType(Image)) I had a problem of when I used a checkbox in the grid that was bound to the datatable that it would not update my dataset. So I created another...
17
2766
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 allow user to filter the first time, when they tried the second time, the speficied cast error message will prompt one.... I create a mydataset1 first, and the mydataset1 data source was getting from DataGrid.DataSource.
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.