Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

C# + Windows Mobile problem

datalogidatalogi Member Posts: 4
Hi,

I'm trying to save DataSet to an XML-file, but I can't get it working.
Can you help me, please?
The code is listed below.

When I start the Mobile Editor, I can press the buttons but nothing happens...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace XMLjaDataSer
{
public partial class Form1 : Form
{
private DataSet _CustomersDS = new DataSet("Customers");
public Form1()
{
InitializeComponent();
}

private void CreateDataSetButton_Click(object sender, EventArgs e)
{
DataTable infoDataTable = new DataTable("infoDataTable"); // create a datatable for our dataset

DataColumn newCol = new DataColumn(); // Create a DataColumn for our DataTabe
newCol.DataType = System.Type.GetType("System.String"); // Make the Column type a string

newCol.ColumnName = "Names"; // Give the Column a name

infoDataTable.Columns.Add(newCol); // Add the new Column to the DataTable

_CustomersDS.Tables.Add(infoDataTable); // Add the DataTable to the DataSet

MessageBox.Show("Done");
}

private void FillDataSetButton_Click(object sender, EventArgs e)
{
DataRow newDataRow = _CustomersDS.Tables["infoDataTable"].NewRow(); //Create a Datarow
newDataRow["Names"] = "Donald Duck";// Add a name to the Column
_CustomersDS.Tables["infoDataTable"].Rows.Add(newDataRow); // Add the row to the DataSet

newDataRow = _CustomersDS.Tables["infoDataTable"].NewRow(); //Create a Datarow
newDataRow["Names"] = "Homer Simpson"; // Add a name to the Column
_CustomersDS.Tables["infoDataTable"].Rows.Add(newDataRow); // Add the row to the DataSet

newDataRow = _CustomersDS.Tables["infoDataTable"].NewRow(); //Create a Datarow
newDataRow["Names"] = "Bart Simpson"; // Add a name to the Column
_CustomersDS.Tables["infoDataTable"].Rows.Add(newDataRow); // Add the row to the DataSet

MessageBox.Show("Done");
}

private void ShowContentsButton_Click(object sender, EventArgs e)
{
// loop through each row and display each item in the Name Column
foreach (DataRow currDR in _CustomersDS.Tables["infoDataTable"].Rows)
{
MessageBox.Show(currDR["Names"].ToString());
}
}

private void SaveDataSetButton_Click(object sender, EventArgs e)
{

_CustomersDS.WriteXml("Comics.xml"); // this will just save the data see below to see more info

MessageBox.Show("DataSet Saved");

System.IO.FileStream myFileStream = new System.IO.FileStream("Comics.xml", System.IO.FileMode.Create);
System.Xml.XmlTextWriter myXmlWriter = new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
_CustomersDS.WriteXml(myXmlWriter, XmlWriteMode.WriteSchema);
myXmlWriter.Close();

}

private void AddDataButton_Click(object sender, EventArgs e)
{
if (_CustomersDS.Tables["infoDataTable"] != null)
{
DataRow newDataRow = _CustomersDS.Tables["infoDataTable"].NewRow(); // Create a datarow
newDataRow["Names"] = AddDataTextBox.Text; // Add a name to the column from the test box
_CustomersDS.Tables["infoDataTable"].Rows.Add(newDataRow); // Add the row to the DataSet
_CustomersDS.AcceptChanges();
MessageBox.Show("Done");
}
}

private void BindButton_Click(object sender, EventArgs e)
{
DataTable infoDataTable = _CustomersDS.Tables["infoDataTable"];
DisplayListBox.DataSource = infoDataTable; // Bind to the list box
DisplayListBox.DisplayMember = "Names"; // Set the column to bind to.
}


}
}


Sign In or Register to comment.