How to access same dataset from different methods?

D

deko

This may be an elementary question.

The Build error I get is: "...frmMain.cs(964): The type or namespace name
'xdd' could not be found..."

I create a dataset when frmMain opens:

public frmMain()
{
InitializeComponent();
string xmlSchema = (@"C:\Projects.xsd");
string xmlFile = (@"C:\Projects.xml");
//create XmlDataDocument and load schema
XmlDataDocument xdd = new XmlDataDocument();
xdd.DataSet.ReadXmlSchema(xmlSchema);
// load xml data
XmlTextReader reader = new XmlTextReader(xmlFile);
reader.MoveToContent(); // move reader root node
xdd.Load(reader);
}

I have a button on frmMain that, when clicked, should update the dataset.

private void btnAddCfg_Click(object sender, System.EventArgs e)
{
if (this.txtCfgNam.Text.ToString().Length > 0)
{
DataTable cfg = xdd.DataSet.Tables["Configuration"]; //<<== error
received here **
cfg.Rows[0]["ConfigName"] = "test";
}
}

How do I make the dataset available to all methods in the form? Am I
creating the Dataset in the right place?

Thanks in advance.
 
D

deko

How do I make the dataset available to all methods in the form? Am I
creating the Dataset in the right place?

I was creating the dataset in the wrong place. But now the question is how
to save the changes made to the XmlDataDocument - error rec'd on last line

public class frmMain : System.Windows.Forms.Form
{
string xmlSchema = (@"C:\Projects.xsd");
string xmlFile = (@"C:\Projects.xml");
//[code omitted]
XmlDataDocument xdd = new XmlDataDocument();

public frmMain()
{
xdd.DataSet.ReadXmlSchema(xmlSchema);
XmlTextReader xrd = new XmlTextReader(xmlFile);
xrd.MoveToContent();
xdd.Load(xrd);
}

private void btnAddCfg_Click(object sender, System.EventArgs e)
{
DataTable someTbl = xdd.DataSet.Tables["Configuration"];
DataRow newCfg = someTbl.NewRow();
newCfg["ConfigName"] = "NEW CONFIGURATION";
prjTbl.Rows.Add(newCfg);
dsDebug.DebugWriteLine(xdd.DataSet);
//xdd.Save(xmlFile); //does not work // <<== * * ERROR HERE
//error rec'd says can't save while file is open
//... but there must be a way to persist the changes ...
}
}
 

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

Similar Threads


Top