0
votes

Greetings,

I'm working in windows application using C#.

I have typed-dataset called packetsDBDataSet and it has table adapter called packetsTableAdapter with method to insert data called InsertPackets().

when I want to insert new data I used a code that I used before with asp.net page and it was working ok but not I'm getting error.

here is the code:

public packetsDBDataSetTableAdapters.packetsTableAdapter ds = new packetsDBDataSetTableAdapters.packetsTableAdapter();

public packetsDBDataSet.packetsDataTable insert = ds.InsertPackets(); 

and here is the error:

Error 1 A field initializer cannot reference the non-static field, method, or property 'Packets.Form1.ds' C:\Users\Ali\Documents\Visual Studio 2008\Projects\Packets-3\Packets\Packets\Form1.cs 26 59 Packets

I already included to my project: using Packets; using Packets.packetsDBDataSetTableAdapters;

please advice to solve this problem.

Update :

I also tried :

public packetsDBDataSetTableAdapters.packetsTableAdapter ds = new packetsDBDataSetTableAdapters.packetsTableAdapter();

ds.InsertPackets("1","2","3");

and I'm getting this error:

Error 1 Invalid token '(' in class, struct, or interface member declaration C:\Users\Ali\Documents\Visual Studio 2008\Projects\Packets-3\Packets\Packets\Form1.cs 28 29 Packets

1
Public fields are bad practice.SLaks

1 Answers

1
votes

These lines are called field initializers; they declare a field and initialize it to some value.

As the error says, a field initializer cannot reference instance members of the class (because they execute before the class is fully constructed).

Because it references the ds instance member, you need to move insert = ds.InsertPackets(); to the constructor.