Question

I'm new to C# and SQL Server so please bear with me if I miss something that seems obvious.

I'm trying to write a program which receives data data from a LAN and stores it onto a local database. There will also be a GUI for displaying some of the data. I am using VS2010 and SQL Server Compact v3.5. I have been able to create a windows forms application which connects to the database and displays/updates tables and so I believe the database is connected properly.

I have a background worker which receives data from the LAN and now I want the background worker to update one of the tables in the database. I have used this guide:

http://msdn.microsoft.com/en-us/library/ms233812.aspx

to create the following section of code:

           MyDataSet.MeasurementRow newMeasurementRow;
           newMeasurementRow = dataset1.Measurement.NewMeasurementRow();
           newMeasurementRow.ChannelNo = n;
           newMeasurementRow.DeviceID = DeviceID;
           newMeasurementRow.Value = f;
           newMeasurementRow.TimeStamp = DateTime.Now;

          try  
          {                                                                 
               this.dataset1.Measurement.Rows.Add(newMeasurementRow);
               this.measurementTableAdapter.Update(this.dataset1.Measurement);
               Console.WriteLine("Success ");
          }
          catch (Exception e)
          {
                Console.WriteLine("Error..... " + e.StackTrace);
                throw;            
          } 

dataset1 and measurementTableAdapter are created earlier in the code with:

    MyDataSet dataset1 = new MyDataSet();
    MyDataSetTableAdapters.MeasurementTableAdapter measurementTableAdapter =
    new MyDataSetTableAdapters.MeasurementTableAdapter(); 

MyDataSet and the table adapter classes were created by the Add New Data Source Wizard. Measurement is the name of the table in my database I want to add to.

When I run this however I get the following outputs when the Update method is called:

A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.dll

Error..... at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)

If anyone could help me interpret these errors or point me in the right direction as to what might be causing the problem It would be greatly appreciated.

Was it helpful?

Solution 2

So I still don't know what was causing the problem with my original code, however I have tried using the TableAdapter Insert method instead of update and this seems to do what I want no problem:

         private MyDataSetTableAdapters.MeasurementTableAdapter measurementTableAdapter =
         new MyDataSetTableAdapters.MeasurementTableAdapter();

         measurementTableAdapter.Insert((n+1), DeviceID, DateTime.Now, Values[n]);

OTHER TIPS

Probably best to try adding your newMeasurementRow (s) to a DataTable (create one, then add to the dataset) that is part of the DataSet. It looks like the dataadapter can't resolve the rows as simply part of the dataset, it is looking for a datatable instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top