Databinding not working

A

Aaron

I have some code that is exactly the same as another area of my
program which is binding an xml doc to a listbox, yet in this case, it
doesn't work...no errors, just doesn't load any data!!!

I'm using C# and here's my code...

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;
using System.Globalization;
..
..
..
DataSet toolDS;
string xmlFileName;
String strAppPath =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

public frmDelete()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

// Create a new dataset.
toolDS = new DataSet();

// Set the Locale for the DataSet,
// using the current culture as the default
toolDS.Locale = CultureInfo.CurrentCulture;

// Set file names and create file streams.
// The XSD, XML, and EXE must be in the same directory.
xmlFileName = strAppPath + "\\Tools.xml";
FileStream FsXML = new FileStream(xmlFileName,FileMode.Open);

// Load the data into the DataSet.
XmlTextReader xtrXML = new XmlTextReader(FsXML);
toolDS.ReadXml(xtrXML);
xtrXML.Close();
FsXML.Close();

// Get a DataTable to conveniently use for binding.
DataTable dt = toolDS.Tables["Tools"];

//Bind the ListBox to the tools
lstTools.DataSource = dt;
lstTools.DisplayMember = "Tool";
}

My xml file...

<?xml version="1.0" encoding="utf-8" ?>
<Tools>
<Tool>Tool #1</Tool>
<Tool>Tool #2</Tool>
 
A

Aaron

Yes, the code is being executed and the xml file hasn't
changed...there is 12 records in it under the Tools node.

Your code is correct. I recommend you to ensure that this code is
executed in your application and also check that Tools.xml contains at
least one <Tool> tag.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
I have some code that is exactly the same as another area of my
program which is binding an xml doc to a listbox, yet in this case, it
doesn't work...no errors, just doesn't load any data!!!

I'm using C# and here's my code...

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;
using System.Globalization;
.
.
.
DataSet toolDS;
string xmlFileName;
String strAppPath =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

public frmDelete()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

// Create a new dataset.
toolDS = new DataSet();

// Set the Locale for the DataSet,
// using the current culture as the default
toolDS.Locale = CultureInfo.CurrentCulture;

// Set file names and create file streams.
// The XSD, XML, and EXE must be in the same directory.
xmlFileName = strAppPath + "\\Tools.xml";
FileStream FsXML = new FileStream(xmlFileName,FileMode.Open);

// Load the data into the DataSet.
XmlTextReader xtrXML = new XmlTextReader(FsXML);
toolDS.ReadXml(xtrXML);
xtrXML.Close();
FsXML.Close();

// Get a DataTable to conveniently use for binding.
DataTable dt = toolDS.Tables["Tools"];

//Bind the ListBox to the tools
lstTools.DataSource = dt;
lstTools.DisplayMember = "Tool";
}

My xml file...

<?xml version="1.0" encoding="utf-8" ?>
<Tools>
<Tool>Tool #1</Tool>
<Tool>Tool #2</Tool>
.
.
.
</Tools>

Anyone have any idea why it's working in one form but not this one?
 
A

Aaron

Since no one can figure this out, myself included, can anyone tell me
how to simply delete a node from xml where the value of the node is a
string I pass in? Once again, my xml structure is very basic:

<?xml version="1.0" encoding="utf-8" ?>
<Tools>
<Tool>Tool #1</Tool>
<Tool>Tool #2</Tool>
 
S

Sergey Bogdanov

Have you tried to extract this code into separate application? It works
as expected. I suppose that you change DataSource somewhere in your
application after DataBinding. Also make sure that
DataSet.Tables[0].Rows.Count contains 12 records.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
Yes, the code is being executed and the xml file hasn't
changed...there is 12 records in it under the Tools node.

Your code is correct. I recommend you to ensure that this code is
executed in your application and also check that Tools.xml contains at
least one <Tool> tag.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
I have some code that is exactly the same as another area of my
program which is binding an xml doc to a listbox, yet in this case, it
doesn't work...no errors, just doesn't load any data!!!

I'm using C# and here's my code...

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;
using System.Globalization;
.
.
.
DataSet toolDS;
string xmlFileName;
String strAppPath =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

public frmDelete()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

// Create a new dataset.
toolDS = new DataSet();

// Set the Locale for the DataSet,
// using the current culture as the default
toolDS.Locale = CultureInfo.CurrentCulture;

// Set file names and create file streams.
// The XSD, XML, and EXE must be in the same directory.
xmlFileName = strAppPath + "\\Tools.xml";
FileStream FsXML = new FileStream(xmlFileName,FileMode.Open);

// Load the data into the DataSet.
XmlTextReader xtrXML = new XmlTextReader(FsXML);
toolDS.ReadXml(xtrXML);
xtrXML.Close();
FsXML.Close();

// Get a DataTable to conveniently use for binding.
DataTable dt = toolDS.Tables["Tools"];

//Bind the ListBox to the tools
lstTools.DataSource = dt;
lstTools.DisplayMember = "Tool";
}

My xml file...

<?xml version="1.0" encoding="utf-8" ?>
<Tools>
<Tool>Tool #1</Tool>
<Tool>Tool #2</Tool>
.
.
.
</Tools>

Anyone have any idea why it's working in one form but not this one?
 
A

Aaron

Have you tried to extract this code into separate application? It works
as expected. I suppose that you change DataSource somewhere in your
application after DataBinding.

No, DataBinding is not changed...like I said, I'm moving on...never
have I seen such a thing where a block of code worked in one area, and
in another, it just doesn't work!
Also make sure that
DataSet.Tables[0].Rows.Count contains 12 records.

Yes it does, and the way the code is written, it really wouldn't
matter how many records I have.
Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
Yes, the code is being executed and the xml file hasn't
changed...there is 12 records in it under the Tools node.

Your code is correct. I recommend you to ensure that this code is
executed in your application and also check that Tools.xml contains at
least one <Tool> tag.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com

Aaron wrote:

I have some code that is exactly the same as another area of my
program which is binding an xml doc to a listbox, yet in this case, it
doesn't work...no errors, just doesn't load any data!!!

I'm using C# and here's my code...

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;
using System.Globalization;
.
.
.
DataSet toolDS;
string xmlFileName;
String strAppPath =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

public frmDelete()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

// Create a new dataset.
toolDS = new DataSet();

// Set the Locale for the DataSet,
// using the current culture as the default
toolDS.Locale = CultureInfo.CurrentCulture;

// Set file names and create file streams.
// The XSD, XML, and EXE must be in the same directory.
xmlFileName = strAppPath + "\\Tools.xml";
FileStream FsXML = new FileStream(xmlFileName,FileMode.Open);

// Load the data into the DataSet.
XmlTextReader xtrXML = new XmlTextReader(FsXML);
toolDS.ReadXml(xtrXML);
xtrXML.Close();
FsXML.Close();

// Get a DataTable to conveniently use for binding.
DataTable dt = toolDS.Tables["Tools"];

//Bind the ListBox to the tools
lstTools.DataSource = dt;
lstTools.DisplayMember = "Tool";
}

My xml file...

<?xml version="1.0" encoding="utf-8" ?>
<Tools>
<Tool>Tool #1</Tool>
<Tool>Tool #2</Tool>
.
.
.
</Tools>

Anyone have any idea why it's working in one form but not this one?
 
A

Aaron

Has anyone ever done this before...I can't believe no one out there
hasn't done a simple removal of a node based on a text string???

Since no one can figure this out, myself included, can anyone tell me
how to simply delete a node from xml where the value of the node is a
string I pass in? Once again, my xml structure is very basic:

<?xml version="1.0" encoding="utf-8" ?>
<Tools>
<Tool>Tool #1</Tool>
<Tool>Tool #2</Tool>
.
.
.
</Tools>

So for instance, I'd like to delete the tool node that matches "Tool
#2", what code in C# do I need to do this. I found this code floating
around on the internet, but it only deletes a node by the name of the
node...seems kinda generic, but totally useless to me since I need to
delete the node based on the value of the node itself.

XmlNode newNode;
XmlNode currNode;

//we give the node we want to delete -> the selected tool
XmlNodeList insNodeLst = docXML.GetElementsByTagName("child1");

//Remove all nodes
insNodeLst[0].RemoveAll();
//Remove the Node itself
insNodeLst[0].ParentNode.RemoveChild(insNodeLst[0]);




Your code is correct. I recommend you to ensure that this code is
executed in your application and also check that Tools.xml contains at
least one <Tool> tag.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
S

Sergey Bogdanov

Okey, try to use instead of:

DataTable dt = toolDS.Tables["Tools"];

//Bind the ListBox to the tools
lstTools.DataSource = dt;
lstTools.DisplayMember = "Tool";

this code snippet:

DataTable dt = toolDS.Tables[0];

//Bind the ListBox to the tools
lstTools.DataSource = dt;
lstTools.DisplayMember = "Tool_Text";


Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
S

Sergey Bogdanov

There are several ways to accomplish it. I'll show only two of them:

// Assume that XML:
// <Tools>
// <Tool>Tool #1</Tool>
// <Tool>Tool #2</Tool>
// </Tools>
//
// and xmlFileName is the path to this XML file


1. XmlDocument and child nodes iteration:

private void RemoveNodeByInnerText(XmlNode current, string value)
{
foreach(XmlNode node in current.ChildNodes)
{
if (node.InnerText == value)
{
current.RemoveChild(node);
break;
}
}
}

XmlDocument doc = new XmlDocument();
doc.Load(xmlFileName);
RemoveNodeByInnerText(doc.DocumentElement, "Tool #2");
doc.Save(xmlFileName);

2. DataTable and Select, Remove methods

FileStream FsXML = new FileStream(xmlFileName, FileMode.Open);
DataSet toolDS = new DataSet();

// Load the data into the DataSet.
XmlTextReader xtrXML = new XmlTextReader(FsXML);
toolDS.ReadXml(xtrXML);
xtrXML.Close();
FsXML.Close();

DataTable dt = toolDS.Tables[0];
DataRow [] dr = dt.Select("Tool_Text='Tool #2'");
if (dr.Length > 0) dt.Rows.Remove(dr[0]);
toolDS.AcceptChanges();
toolDS.WriteXml(xmlFileName);


Hope this help,
Sergey Bogdanov
http://www.sergeybogdanov.com
Has anyone ever done this before...I can't believe no one out there
hasn't done a simple removal of a node based on a text string???

Since no one can figure this out, myself included, can anyone tell me
how to simply delete a node from xml where the value of the node is a
string I pass in? Once again, my xml structure is very basic:

<?xml version="1.0" encoding="utf-8" ?>
<Tools>
<Tool>Tool #1</Tool>
<Tool>Tool #2</Tool>
.
.
.
</Tools>

So for instance, I'd like to delete the tool node that matches "Tool
#2", what code in C# do I need to do this. I found this code floating
around on the internet, but it only deletes a node by the name of the
node...seems kinda generic, but totally useless to me since I need to
delete the node based on the value of the node itself.

XmlNode newNode;
XmlNode currNode;

//we give the node we want to delete -> the selected tool
XmlNodeList insNodeLst = docXML.GetElementsByTagName("child1");

//Remove all nodes
insNodeLst[0].RemoveAll();
//Remove the Node itself
insNodeLst[0].ParentNode.RemoveChild(insNodeLst[0]);




Your code is correct. I recommend you to ensure that this code is
executed in your application and also check that Tools.xml contains at
least one <Tool> tag.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
A

Aaron

Wow, thanks much...that worked perfectly and I didn't even realize it
was that easy...thanks for the help!

There are several ways to accomplish it. I'll show only two of them:

// Assume that XML:
// <Tools>
// <Tool>Tool #1</Tool>
// <Tool>Tool #2</Tool>
// </Tools>
//
// and xmlFileName is the path to this XML file


1. XmlDocument and child nodes iteration:

private void RemoveNodeByInnerText(XmlNode current, string value)
{
foreach(XmlNode node in current.ChildNodes)
{
if (node.InnerText == value)
{
current.RemoveChild(node);
break;
}
}
}

XmlDocument doc = new XmlDocument();
doc.Load(xmlFileName);
RemoveNodeByInnerText(doc.DocumentElement, "Tool #2");
doc.Save(xmlFileName);

2. DataTable and Select, Remove methods

FileStream FsXML = new FileStream(xmlFileName, FileMode.Open);
DataSet toolDS = new DataSet();

// Load the data into the DataSet.
XmlTextReader xtrXML = new XmlTextReader(FsXML);
toolDS.ReadXml(xtrXML);
xtrXML.Close();
FsXML.Close();

DataTable dt = toolDS.Tables[0];
DataRow [] dr = dt.Select("Tool_Text='Tool #2'");
if (dr.Length > 0) dt.Rows.Remove(dr[0]);
toolDS.AcceptChanges();
toolDS.WriteXml(xmlFileName);


Hope this help,
Sergey Bogdanov
http://www.sergeybogdanov.com
Has anyone ever done this before...I can't believe no one out there
hasn't done a simple removal of a node based on a text string???

Since no one can figure this out, myself included, can anyone tell me
how to simply delete a node from xml where the value of the node is a
string I pass in? Once again, my xml structure is very basic:

<?xml version="1.0" encoding="utf-8" ?>
<Tools>
<Tool>Tool #1</Tool>
<Tool>Tool #2</Tool>
.
.
.
</Tools>

So for instance, I'd like to delete the tool node that matches "Tool
#2", what code in C# do I need to do this. I found this code floating
around on the internet, but it only deletes a node by the name of the
node...seems kinda generic, but totally useless to me since I need to
delete the node based on the value of the node itself.

XmlNode newNode;
XmlNode currNode;

//we give the node we want to delete -> the selected tool
XmlNodeList insNodeLst = docXML.GetElementsByTagName("child1");

//Remove all nodes
insNodeLst[0].RemoveAll();
//Remove the Node itself
insNodeLst[0].ParentNode.RemoveChild(insNodeLst[0]);




On Sun, 16 Jan 2005 10:07:56 +0200, Sergey Bogdanov


Your code is correct. I recommend you to ensure that this code is
executed in your application and also check that Tools.xml contains at
least one <Tool> tag.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
I

Ilya Tumanov [MS]

It does not work because you've made wrong assumptions on how XML schema
would be inferred.

You thought you going to get a table called "Tools" with a column called
"Tool".

That's not the case. Here's the real result:

--------------------------- DataSet ----------------------
DataSet: 'Tools' << You get a DataSet called "Tools"
----------------------- Tables -----------------------
DataTable@0: 'Tool' << You have a table called "Tool" in this
DataSet
------------------- Columns ----------------------
SimpleContent Column@0: 'Tool_Text' 'String' Nullable << You have
a column mapped as SimpleContent auto named "Tool_Text" by inference engine.
----------------- Child Tables -------------------
----------------- Parent Tables ------------------
--------------------- Rows -----------------------
Added: [current: Tool_Text=Tool #1] << And you have some rows in
table "Tool".
Added: [current: Tool_Text=Tool #2]
2 rows in this table
--------------------------------------------------

This is the very reason why handcrafting of the XML and inference should
_never_ be used.
If you want certainty, design schema as required and never edit XML
manually, let DataSet write it for you.

If you willing to take the risk, you can change your XML as follows:

<?xml version="1.0" encoding="utf-8" ?>
<DataSetName>
<Tools>
<Tool>Tool #1</Tool>
</Tools>
<Tools>
<Tool>Tool #2</Tool>
</Tools>
</DataSetName>

That would result in a schema you originally expected:

--------------------------- DataSet ----------------------
DataSet: 'DataSetName'
----------------------- Tables -----------------------
DataTable@0: 'Tools'
------------------- Columns ----------------------
Element Column@0: 'Tool' 'String' Nullable
----------------- Child Tables -------------------
----------------- Parent Tables ------------------
--------------------- Rows -----------------------
Added: [current: Tool=Tool #1]
Added: [current: Tool=Tool #2]
2 rows in this table
--------------------------------------------------

If you choose that path, be extremely careful.
Be advice inference would loose all typing information. It's also very slow
and memory intense.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: Aaron <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
Subject: Databinding not working
Message-ID: <[email protected]>
X-Newsreader: Forte Agent 2.0/32.640
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 68
X-Trace: nghlfjfcphhogbnddjokkcehldoennbfgeoedhedpcljfdfjcljnjpekkincgmjchkimpnlhogea
pfnphclklgfjdgedfoplkfckcejaehphmpdkmcekiphcllgeejdeegcmjhhibopfakdb
NNTP-Posting-Date: Sat, 15 Jan 2005 19:34:33 MST
Date: Sat, 15 Jan 2005 20:33:53 -0600
Path: cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.s
ul.t-online.de!t-online.de!news.glorb.com!newsfeed-east.nntpserver.com!nntps
erver.com!chi1.usenetserver.com!news.usenetserver.com!newsfeed.telusplanet.n
et!newsfeed.telus.net!hwmnpeer01.phx!hwmedia!hw-poster!fe05.lga.POSTED!53ab2
750!not-for-mail
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:68802
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I have some code that is exactly the same as another area of my
program which is binding an xml doc to a listbox, yet in this case, it
doesn't work...no errors, just doesn't load any data!!!

I'm using C# and here's my code...

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;
using System.Globalization;
.
.
.
DataSet toolDS;
string xmlFileName;
String strAppPath =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetN
ame().CodeBase);

public frmDelete()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

// Create a new dataset.
toolDS = new DataSet();

// Set the Locale for the DataSet,
// using the current culture as the default
toolDS.Locale = CultureInfo.CurrentCulture;

// Set file names and create file streams.
// The XSD, XML, and EXE must be in the same directory.
xmlFileName = strAppPath + "\\Tools.xml";
FileStream FsXML = new FileStream(xmlFileName,FileMode.Open);

// Load the data into the DataSet.
XmlTextReader xtrXML = new XmlTextReader(FsXML);
toolDS.ReadXml(xtrXML);
xtrXML.Close();
FsXML.Close();

// Get a DataTable to conveniently use for binding.
DataTable dt = toolDS.Tables["Tools"];

//Bind the ListBox to the tools
lstTools.DataSource = dt;
lstTools.DisplayMember = "Tool";
}

My xml file...

<?xml version="1.0" encoding="utf-8" ?>
<Tools>
<Tool>Tool #1</Tool>
<Tool>Tool #2</Tool>
.
.
.
</Tools>

Anyone have any idea why it's working in one form but not this one?
 

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

Top