# [C#] Save table

2 replies to this topic

### #1yesterdaygaming

New Member

• Members
• 2 posts

Posted 09 October 2009 - 01:24 PM

Hello, now This is my code of form 1 (only got 1 form)

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

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public DataTable Categories;
public DataTable Subcategories;
public DataColumn col1;
public DataColumn col2;
public DataColumn col3;
public DataColumn col4;
public DataColumn col5;
public DataSet Datacollection;

public Form1()
{
InitializeComponent();

Datacollection = new DataSet("Alledata");

Categories = new DataTable();
Categories.TableName = "Categories";
//Column 1
col1.AllowDBNull = true;
col1.MaxLength = 5;
col1.ColumnName = "ID";
//---------------------
//Column 2
col2.AllowDBNull = true;
col2.MaxLength = 20;
col2.ColumnName = "Name";
//---------------------
//Column 3
col3.AllowDBNull = true;
col3.MaxLength = 100;
col3.ColumnName = "Description";
//---------------------
//Column 4
col4.AllowDBNull = true;
col4.MaxLength = 20;
col4.ColumnName = "Owner";
//---------------------

dataGridView1.DataSource = Categories;

}

}
}


This works for me. But if i compile the game, run it it doesn't save data if you enter. How can i save the data?

### #2ddboarm

New Member

• Members
• 1 posts

Posted 10 October 2009 - 05:27 AM

Using just DataSet and DataTable, you do have the option of saving in 2 ways. The first, and easiest, involves saving the DataSet/DataTable to an XML format. I don't recall the method name off the top of my head. But check MSDN, it's there.

The second method for saving the data is to actually connect to an SQL datasource. It is a lot more involved...

DevMaster Staff

• Moderators
• 1716 posts

Posted 10 October 2009 - 12:59 PM

Of course it doesn't automagically save the data! Where did you ask the computer to do so?

You have to realize computers are like highly literal idiot-savants. They can do lots of wonderous things very fast... as long as you explicitly tell them to do it.

In your case, nowhere do you instruct the computer to save the contents in the Dataset.

As ddboarm indicates, the question is how do you want to save the data? If you just want to save it to a file, then you have to code it out. Probably you wan to use the builtin WriteXml() (and ReadXml() to load it back in) functions. Google for some tuts.

Or, you might want to have different output, like a text file. Here's an example of how to do it.

If you want to save to a database, this gets more complicated.
Hyperbole is, like, the absolute best, most wonderful thing ever! However, you'd be an idiot to not think dogmatism is always bad.

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users