string GetSerials = "SELECT SerialNumber from Warranty";
string TestUpdateDates = "UPDATE Warranty SET StartDate = '@StartDate', EndDate = '@EndDate' WHERE SerialNumber = '@result'";
//string TestUpdateDates2 = "UPDATE Warranty SET StartDate = cDate(Format('@StartDate', 'MM/dd/yyyy')), EndDate = cDate(Format('@EndDate', 'MM/dd/yyyy')) WHERE(SerialNumber = '@result')";
DataTable dataTable = new DataTable();
using (var conn1 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Blah\Blah\Blah\Warranty.accdb"))
using (OleDbCommand serialCommand = new OleDbCommand(GetSerials, conn1))
{
conn1.Open();
dataTable.Load(serialCommand.ExecuteReader());
foreach (DataRow row in dataTable.Rows)
{
var result = row["SerialNumber"].ToString();
WebRequest request = WebRequest.Create("urlpart1" + result + "urlpart2");
string json;
var response = request.GetResponse();
request.ContentType = "application/json; charset=utf-8";
using (var streamr = new StreamReader(response.GetResponseStream()))
using (OleDbCommand testupdateCommand = new OleDbCommand(TestUpdateDates, conn1))
using (OleDbCommand updateCommand = new OleDbCommand(UpdateDates, conn1))
using (OleDbCommand deleteCommand = new OleDbCommand(DeleteIncomplete, conn1))
{
json = streamr.ReadToEnd();
List<MyObject> list = JsonConvert.DeserializeObject<List<MyObject>>(json);
MyObject obj = list[0]; //Base Warranty
// obj -- Base Warranty
var StartDate = obj.Start;
var EndDate = obj.End;
//testupdateCommand.Parameters.Add("@StartDate", OleDbType.Date).Value = StartDate;
//testupdateCommand.Parameters.Add("@EndDate", OleDbType.Date).Value = EndDate;
testupdateCommand.Parameters.AddWithValue("@StartDate", StartDate);
testupdateCommand.Parameters.AddWithValue("@EndDate", EndDate);
testupdateCommand.Parameters.AddWithValue("@result", result);
I am trying to update my database with values from a JSON response. I want to loop through all values in the column ["SerialNumber"] and with that Serial Number, I am able to get a Start Date and End Date from the url. I want to store these values into the same Serial Number from which I used to get the url into the respective fields (StartDate, EndDate).
I have commented out some of the other ways I have tried to do this but the output I get is that only the very first SerialNumber I get is changed, it doesnt continue looping through my column with 100 values. If I add
MessageBox.Show(result); //SerialNumber
MessageBox.Show(StartDate.ToString());
MessageBox.Show(EndDate.ToString());
It does loop through via MessageBox.Show but it will not update my database this way. Why doesn't it iterate though by itself?
I am using ACCESS2013 If that is important