Ive been working on a program that allows me to updload data from my sql server DB into a SOAP service. I can successfully upload the data into the service. The problem is that the same row is being inserted. The following is a part of my code: `
List<service1.SDataRow> ArrayOfSData = new List<ne_service_demo2.service1.SDataRow>();
//data to be uploaded.
service1.SDataRow row1 = new service1.SDataRow();
foreach (DataRow dRow in dTable.Rows)
{
row1.SensorTimeStamp = dRow["SensorTimeStamp"].ToString();
row1.SensorID = dRow["SensorID"].ToString();
row1.PercentFull = dRow["PercentFull"].ToString();
row1.Temp = dRow["Temp"].ToString();
row1.TempReading = dRow["TempReading"].ToString();
row1.Sig = dRow["Sig"].ToString();
row1.SignalReading = dRow["SignalReading"].ToString();
row1.Noi = dRow["Noi"].ToString();
row1.NoiseReading = dRow["NoiseReading"].ToString();
row1.Cou = dRow["Cou"].ToString();
row1.CountReading = dRow["CountReading"].ToString();
row1.NewPercentFull = dRow["NewPercentFull"].ToString();
row1.Current_Gallons_In_Tank = dRow["Current_Gallons_In_Tank"].ToString();
ArrayOfSData.Add(row1);
}
service1.SensorData sd = new service1.SensorData();
sd.API_ACCESS_KEY = "7e070c1981ccc368483a207801be17aa17b2334c";
sd.AccountCode = "0000000";
sd.SData = ArrayOfSData.ToArray();
// Asynchronous call
ws.PostSensorDataCompleted += WebSeviceResult;
ws.PostSensorDataAsync(sd);`
As I loop through my DB table, all data goes to the appropriate fields or columns. Again, the problem is that I get the same row x amount of times. I know the problem is “row1” because it is constantly being updated with new values while still keeping the list count. However, I can not make row1 a list type because it must be of SDataRow type since that is the class that links my service to the columns of my DB. I also tried using the List.Clear() method to clear out my list within my loop but did not work. When I tried clearing the list after every iteration I would get an error saying “Item has already been added.” So in theory the clear() method should work, its the row1 that is causing problems. Does anyone know of a work around or a way to make
You are creating an instance of the SDataRow outside the loop. Inside the loop, you are setting the properties of that same instance (overwriting them each iteration), then adding that same instance to the list.
Change that part of the code to this:
So you need to create new instances inside the foreach loop