I want to run a foreach loop as soon as i > x
I have tried i while loop before foreach
Do loop
But the foreach still runs even if i has been equal
I can’t figure out how I should run it
I need it to run 9999 times, then do some stuff, and then start over with the next 9999 and so on
I have updated with the whole code
I need to go true all rows in the datatable with batchsize 9999 at the time and then go run restsharp and then go to next 9999
When all the rows in the datatable is done i need to go up to sql query and run the next query, and then new datatable and the same batchsize 9999
Please help
This is my code
protected void run_Click1(object sender, ImageClickEventArgs e)
{
var body = "";
string[] evenNums = new string[3];
evenNums[0] = "2";
evenNums[1] = "3";
evenNums[2] = "4";
var daytime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss", CultureInfo.GetCultureInfo("sv-SE"));
var day = DateTime.Now.ToString("yyyyMMdd", CultureInfo.GetCultureInfo("sv-SE"));
foreach (string Site in evenNums)
{
body = "{"requestId": "144x25","items": [";
using (SqlConnection con = new SqlConnection())
{
string conn_str = ConfigurationManager.ConnectionStrings["SqlConnection1"].ConnectionString;
SqlConnection conn = new SqlConnection(conn_str);
string query = "SELECT [number],[surname],[forename],[emailAddress],[taxIdentifier] FROM[Customer] WITH(NOLOCK) WHERE[CustomerID] IN(SELECT[CustomerID] FROM[Visit] WITH(NOLOCK) WHERE[GamingDate] Between convert(Date, DATEADD(DAY, -365, GETDATE())) AND convert(Date, getdate())) AND(EmailAddress IS NOT NULL) AND(ContactTypes & 1 = 1) AND(HomePropertyID = " + Site + ") /* 2= Malmö 3=Göteborg 4=Stockholm */ AND(LEN(TaxIdentifier) = '12')";
SqlCommand comm = new SqlCommand(query, conn);
conn.Open();
comm.ExecuteNonQuery();
DataTable dt1 = new DataTable();
using (SqlDataAdapter adapter = new SqlDataAdapter(comm))
{
adapter.Fill(dt1);
}
string newFileName = "C:\temp\" + ort + "" + day + "FromNeon_Cleaned.csv";
string clientHeader = "number" + "," + ""surname"" + "," + ""forename"" + "," + ""repsonseId"" + "," + ""responseTime"" + Environment.NewLine;
File.WriteAllText(newFileName, clientHeader);
int batchSize = 2;
bool willBreak = false;
for (int i = 0; i < batchSize; i++)
{
for (int j = 0; j < dt1.Rows.Count; j++)
{
DataRow dtRow = dt1.Rows[j];
if (i == j)
{
break; // in there what you want as I understand
willBreak = true;
}
var number = dtRow.ItemArray[0];
var surname = dtRow.ItemArray[1];
var forename = dtRow.ItemArray[2];
var emailAddress = dtRow.ItemArray[3];
string taxidentifier = (string)dtRow.ItemArray[4];
//string taxidentifier = "2211221143";
if (Personnummer.Valid(taxidentifier))
{
body += "{"itemId": "" + number + "","subjectId": "" + taxidentifier + ""},";
}
body = body.Remove(body.Length - 1);
body += "]}";
}
if (willBreak) break;
}
var url = "https://marketing.spelpaus.se/api/marketing-subjectid/mw41yZpBFC";
var client = new RestClient(url);
var request = new RestRequest(url, Method.Post);
request.AddHeader("authorization", "vGrpkoNEWHpMuQVrIKVGfsfx2l7SLbsSfQ7PbISjMyiK545ezHW5BzsfPgJq0HA6");
request.AddHeader("Content-Type", "application/json");
//var bodyy = JsonConvert.SerializeObject(body);
request.AddBody(body, "application/json");
RestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK)
{
var obj = JsonConvert.DeserializeObject<dynamic>(response.Content);
var alloweditemsdata = (obj.allowedItemIds);
var responseId = (obj.responseId);
//var responseId1= Convert.ToInt32(responseId.value);
dt1.PrimaryKey = new DataColumn[] { dt1.Columns["number"] };
foreach (int alloweditem in alloweditemsdata)
{
DataRow Drw = dt1.Rows.Find(alloweditem);
var numberresult = Drw.ItemArray[0];
var surnameresult = Drw.ItemArray[1];
var forenameresult = Drw.ItemArray[2];
var emailAddressresult = Drw.ItemArray[3];
string taxidentifierresult = (string)Drw.ItemArray[4];
string clientDetails = " " + numberresult + ","" + surnameresult + "","" + forenameresult + "","" + emailAddressresult + "","" + responseId + "","" + daytime + """ + Environment.NewLine;
File.AppendAllText(newFileName, clientDetails, Encoding.Unicode);
}
}
}
}
}
2
Answers
You have 2 nested for-loops, the outer with loop variable
i
and the inner with loop variablej
. But you nowhere actually usei
, except to do thebreak
.The result is as follows:
j
-loop at first breaks immediately when j==0, because the value ofi
is 0.j
-loop restarts because of the outeri
-loop, now it breaks when j==1, because now the value ofi
is 1.i
-loop ends, because it has reached thebatchsize
value.This is really weird and looks like a mistake. I suspect you do not need the
i
-loop at all, so I suggest you remove it, and instead do something likeif (j == batchsize) { break; }
inside thej
-loop.