[C#] Save table

yesterdaygaming 101 Oct 09, 2009 at 13:24

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()

            Datacollection = new DataSet("Alledata");

            Categories = new DataTable();
            Categories.TableName = "Categories";
            //Column 1
            col1 = Categories.Columns.Add();
            col1.AllowDBNull = true;
            col1.MaxLength = 5;
            col1.ColumnName = "ID";
            //Column 2
            col2 = Categories.Columns.Add();
            col2.AllowDBNull = true;
            col2.MaxLength = 20;
            col2.ColumnName = "Name";
            //Column 3
            col3 = Categories.Columns.Add();
            col3.AllowDBNull = true;
            col3.MaxLength = 100;
            col3.ColumnName = "Description";
            //Column 4
            col4 = Categories.Columns.Add();
            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?

2 Replies

Please log in or register to post a reply.

ddboarm 101 Oct 10, 2009 at 05:27

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…

alphadog 101 Oct 10, 2009 at 12:59

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

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.