views:

30

answers:

1

Hi all, I have implemented a small Windows Service which runs every 9 minutes and writes the data which comes througth a webservice to the db.

Do do the db work I use Linq To SQL

using (var db = new DataClasses1DataContext())
            {
                var currentWeather = this.GetWeatherData();

                //////TODO Add the data correct

                var newEntry = new WeatherData()
                {
                    test = currentWeather.dateGenerated.ToShortTimeString()
                };

                //var test = db.WeatherDatas.First();

                db.WeatherDatas.InsertOnSubmit(newEntry); // this throws Invalid Operation Exception
                db.SubmitChanges();
            }

Why does it throw this exception? the same codeblock in a console programm runs nice

alt text

+1  A: 

Have you set up the connection string in app.Config correctly?

IIRC, the default constructor on an L2S DataContext reads the connection string from the config file. If the connection string points to the the wrong database (or one that doesn't exist) you may very well receive an exception.

This could also explain why this piece of code works when executed in a different context.

Mark Seemann
thats it ... my dear the primary key was lost in the datacontext of the service... crap shit
Markus