Bora Bogdan
Bora Bogdan

Reputation: 67

Linq data base insertion error

So i have the following method:

  public void dataSchreiben(Messdaten data)
    {
        try
        {
            var ctxMess = new MessdatenDataContext();
            var messSchreiben = new Messdaten()
            {

                stromschwSoll = data.stromschwSoll,  
                stromscwIst = data.stromscwIst, 

                stromMaxSoll = data.stromMaxSoll, 
                stromIst = data.stromIst,       

                drehzahl = data.drehzahl,       

                sollwerDrehmMaxon = data.sollwerDrehmMaxon,
                istwertDrehmMaxonDR1 = data.istwertDrehmMaxonDR1,
                istwertDrehmMaxonDR2 = data.istwertDrehmMaxonDR2,

                rueckdrehmSollImLauf = data.rueckdrehmSollImLauf,
                ruedrehmIstImLaufDR1 = data.ruedrehmIstImLaufDR1,
                ruedrehmIstImLaufDR2 = data.ruedrehmIstImLaufDR2,

                ruedrehmSollLosbrechen = data.ruedrehmSollLosbrechen,
                ruedrehmIstLosbrDR1 = data.ruedrehmIstLosbrDR1,
                ruedrehmIstLosbrDR2 = data.ruedrehmIstLosbrDR2,

                sollwVerdrspGetriebe = data.sollwVerdrspGetriebe,
                istwVerdrspGetriebe = data.istwVerdrspGetriebe,

                sollVerdrehspGeber = data.sollVerdrehspGeber,
                istwVerdrehspGeber = data.istwVerdrehspGeber,

                serialNummer = "Succes" 


            };
            ctxMess.Messdaten.InsertAllOnSubmit(messSchreiben);
            try
            {
                ctxMess.SubmitChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                ctxMess.SubmitChanges();
            }

        }catch {
            MessageBox.Show("Schreiben error !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

I have a database, that contains 21 fields(an index autoincremetable, and a timestamp).Messdaten data is a structure containing the fields below.The InsertAllOnSubmit gives back this error:

the type arguments of System.Data.Linq InsertAllOnSubmit (System.Collections.IEnumerable ) -. methods can not be derived by inference from the syntax. Enter the type arguments explicitly

Also, my variables are of double type, while in the database they are saved as float. Could this generate the problem or is it something else? I searched on google, but no solution came up.

public struct Messdaten                 //a structure containing all the elements of the measurements
    {

        public double stromschwSoll;
        public double stromscwIst;

        public double stromMaxSoll;
        public double stromIst;

        public double drehzahl;

        public double sollwerDrehmMaxon;
        public double istwertDrehmMaxonDR1;
        public double istwertDrehmMaxonDR2;

        public double rueckdrehmSollImLauf;
        public double ruedrehmIstImLaufDR1;
        public double ruedrehmIstImLaufDR2;

        public double ruedrehmSollLosbrechen;
        public double ruedrehmIstLosbrDR1;
        public double ruedrehmIstLosbrDR2;

        public double sollwVerdrspGetriebe;
        public double istwVerdrspGetriebe;

        public double sollVerdrehspGeber;
        public double istwVerdrehspGeber;

        public string serialNummer;
    }
    public Messdaten aktMessung;

And the database declaration:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Messdaten")]
public partial class Messdaten : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _Index;

    private System.Nullable<System.DateTime> _TimeStamp;

    private System.Nullable<double> _StromschwSollLastlos;

    private System.Nullable<double> _StromschwIstLastlos;

    private System.Nullable<double> _StromMaxSollLastlos;

    private System.Nullable<double> _StromMaxIstLastlos;

    private System.Nullable<double> _DrehzahlMaxonmotor;

    private System.Nullable<double> _SollwertDrehmMaxon;

    private System.Nullable<double> _IstwertDrehmMaxonDR1;

    private System.Nullable<double> _IstwertDrehmMaxonDR2;

    private System.Nullable<double> _RueckdrehmSollImLauf;

    private System.Nullable<double> _RuedrehmIstImLaufDR1;

    private System.Nullable<double> _RuedrehmIstImLaufDR2;

    private System.Nullable<double> _RuedrehmSollLosbrechen;

    private System.Nullable<double> _RuedrehmIstLosbrDR1;

    private System.Nullable<double> _RuedrehmIstLosbrDR2;

    private System.Nullable<double> _SollwVerdrspGetriebe;

    private System.Nullable<double> _IstwVerdrspGetriebe;

    private System.Nullable<double> _SollVerdrehspGeber;

    private System.Nullable<double> _IstwVerdrehspGeber;

    private string _SerialNummer;

Errors: the best match for the overloaded System.Data.Linq.Table InsertOnSubmit (STW_PruefStand.Messdaten -. has not some invalid method

1 - argument: can not convert from 'STW_PruefStand.Form1.Messdaten "in" "STW_PruefStand.Messdaten"

Upvotes: 1

Views: 197

Answers (2)

Andrei
Andrei

Reputation: 56716

InsertAllOnSubmit is used for collections of objects to be inserted in the DB. Here you are dealing with just one object, so the correct method to use is InsertOnSubmit:

ctxMess.Messdaten.InsertOnSubmit(messSchreiben);

Update. After question update, new problem seems to be related to the naming. Both struct and context class have names Messdaten, which is misleading. For instance, this line:

var messSchreiben = new Messdaten()

instantiates object of type STW_PruefStand.Form1.Messdaten (the struct), while according to the logic of the method it should be context class. Try specifying the namespace explicitly:

var messSchreiben = new STW_PruefStand.Messdaten()
{
...

Upvotes: 3

Christian Phillips
Christian Phillips

Reputation: 18769

You should be using InsertOnSubmit(), since you are only adding one object.

See MSDN links InsertOnSubmit() & InsertAllOnSubmit() for differences.

Upvotes: 0

Related Questions