Adding columns to dataset in c#

c#, dataset

Solution

I assume that you have the code as it is posted. So compiler complaints `Method must have a return type` and `Invalid token '.' in class, struct, or interface member declaration`.

You should create a `Method` and wrap your code in it, It can be instance method, constructor or static method or static constructor.

static DataSet ds_input = new DataSet();
static DataSet ds_output = new DataSet();

private static void InitializeMyDataSet()
{
    ds_output.Tables.Add(new DataTable() );
    ds_output.Tables[0].Columns.Add("column_1", typeof(string));
    ds_output.Tables[0].Columns.Add("column_2", typeof(string));
    ds_output.Tables[0].Columns.Add("column_4", typeof(string));
    ds_output.Tables[0].Columns.Add("column_3", typeof(string));
}

Then call `InitializeMyDataSet();` where you need.

Hope this helps.

Problem

I am trying to execute the following code: ``` static DataSet ds_input; static DataSet ds_output; ds_output.Tables.Add(new DataTable() ); ds_output.Tables[0].Columns.Add("column_1", typeof(string)); ds_output.Tables[0].Columns.Add("column_2", typeof(string)); ds_output.Tables[0].Columns.Add("column_4", typeof(string)); ds_output.Tables[0].Columns.Add("column_3", typeof(string)); ``` Seems simple to me but somehow it is giving me following errors: When initializing a new instance in `Add(new DataTable())`: Method must have a return type. When adding columns to `ds_output.Tables[0]`: Invalid token '.' in class, struct, or interface member declaration Update #1: I had written the above code in the beginning of the class as follows: ``` namespace Bulk_Verification { public partial class Form1 : Form { static DataSet ds_input; static DataSet ds_output; //ds_output.Tables.Add(new DataTable()); ds_output.Tables[0].Columns.Add("column_1", typeof(string)); ds_output.Tables[0].Columns.Add("column_2", typeof(string)); ds_output.Tables[0].Columns.Add("column_4", typeof(string)); ds_output.Tables[0].Columns.Add("column_3", typeof(string)); ``` After suspecting that this might be the problem, i shifted these lines just before they are supposed to be used: ``` public static DataSet verify(DataSet ds_input) { ds_output.Tables.Add(new DataTable()); ds_output.Tables[0].Columns.Add("column_1", typeof(string)); ds_output.Tables[0].Columns.Add("column_2", typeof(string)); ds_output.Tables[0].Columns.Add("column_4", typeof(string)); ds_output.Tables[0].Columns.Add("column_3", typeof(string)); ``` This is also the only method that returns a value (a customized dataset i am trying to create). Having the postion changed, now i get the following compiler error when i run the application using debugger, on `ds_output.Tables.Add(new DataTable());`: Object reference not set to an instance of an object.

Original source