C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
C# + Windows Mobile problem Posted by datalogi on 28 Mar 2009 at 11:06 AM
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.
}


}
}





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.