For Loop in C# not looping - c#

I have an issue with a Loop that doesn't actually loop. I've posted a simplified version of my code below. Basically, using the NPOI excel library, I have an excel file with data on the first and second sheet, so I need to do a loop to get through both sheets.
Below is what I have done so far, however this only works through the first sheet and then exits. It fails to increment the variable w. As you can see, there are other loops implemented in this code which function fine so I don't get it.
It's been a very long day and perhaps I'm missing something very simple. I could have it placed wrong or something. If anyone else can spot what I might be doing wrong I'd be very grateful :)
public class SalesFileProcessor : ISalesProcessor
{
public List<FTPSalesRow> ProcessSalesFile(string filename)
{
try
{
using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
{
int numberOfSheets = 2;
//Loop through sheets - does not work
for (int w = 0; w <= numberOfSheets; w++)
{
HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs);
HSSFSheet sheet = templateWorkbook.GetSheetAt(w);
HSSFRow row = null;
for (int i = 1; i <= sheet.LastRowNum; i++)
{
FTPSalesDetails t = null;
int currentColumn = 0;
try
{
ModelContainer ctn = new ModelContainer();
row = sheet.GetRow(i);
if (row == null)
{
continue;
}
t = new FTPSalesDetails
{
RowNumber = i,
InvoiceDate = GetCellValue(row.GetCell(0)),
CountrySoldIn = GetCellValue(row.GetCell(1)),
NetUnitsSold = GetCellValue(row.GetCell(2)),
Item = GetCellValue(row.GetCell(3)),
ProductCode = GetCellValue(row.GetCell(5)),
};
if (t.ProductCode == null && t.NetUnitsSold == null)
{
return null;
}
int Qty = int.Parse(t.NetUnitsSold);
for (int x = 0; x < Qty; x++)
{
ItemSale ts = new ItemSale
{
ItemID = GetItemID(t.ProductCode),
ManufacturerID = GetManufacturerID("Samsung"),
DateSold = DateTime.Now,
};
ctn.AddToItemSales(ts);
ctn.SaveChanges();
}
}
catch (IndexOutOfRangeException) { }
}
} //End Loop - the one that doesn't work
}
}
catch (IOException exp)
{
throw new FTPSalesFileProcessingException("Could not open the Sales data file", exp);
}
catch (Exception exp)
{
throw new FTPSalesFileProcessingException("An unknown eror occured during processing the file", exp);
}
return null;
}

if (t.ProductCode == null && t.NetUnitsSold == null)
{
return null;
}
I'm going to guess that this is being hit, causing your entire function to exit. If you are trying to exit out of that iteration of the for loop try a break; instead, or a continue as Mike M pointed out in the comments.

For what you say, assuming your variables are all ok that is the loop is not empty... have you checked you're not hiting this line in the first iteration?
if (t.ProductCode == null && t.NetUnitsSold == null)
{
return null;
}

Looking at the code, the only obvious thing sticking out to me is:
HSSFSheet sheet = templateWorkbook.GetSheetAt(w);
HSSFRow row = null;
for (int i = 1; i <= sheet.LastRowNum; i++)
I would guess that sheet.LastRowNum either equals 0 or 1.

Maybe the indexOutOfRangeException is thrown and thats beacuse you have only one iteration, or instead of <= you sholud use <. Does the sheet numbers start with zero ?

Related

Loop not finding elements after going to new page.StaleElementReferenceException

List<IWebElement> shittyBiz = new List<IWebElement>();
var myEles = driverGC.FindElements(By.CssSelector("div.search-result"));
for (int i = 0;i<=1000;i++){
myEles = driverGC.FindElements(By.CssSelector("div.search-result"));
foreach (IWebElement business in myEles)
{
driverGC.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
var starRating = " ";
try
{
starRating = business.FindElement(By.CssSelector("div.biz-rating > div.i-stars")).GetAttribute("title");
}
catch (OpenQA.Selenium.NoSuchElementException)
{
MessageBox.Show("No stars");
continue;
}
starRating = Regex.Replace(starRating, #"[A-Za-z\s]", string.Empty);
float stars = float.Parse(starRating);
MessageBox.Show(stars.ToString());
if (stars <= 3)
{
//shittyBiz.Add(starRating);
MessageBox.Show("Shitty");
driverGC.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
var bizName = business.FindElement(By.CssSelector(".biz-name"));
MessageBox.Show(bizName.Text);
shittyBiz.Add(bizName);
var bizLocation = business.FindElement(By.CssSelector(".secondary-attributes"));
MessageBox.Show(bizLocation.Text);
shittyBiz.Add(bizLocation);
}
else
{
driverGC.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
MessageBox.Show("Too good");
}
}
try
{
driverGC.FindElement(By.CssSelector("div.arrange_unit > a.u-decoration-none")).Click();
continue;
}
catch (OpenQA.Selenium.NoSuchElementException)
{
MessageBox.Show("No more pages");
return;
//driverGC.Quit();
}
}
I can get the program to run fine the first time, but after it uses the try at the end, to go to the next page, i get the StaleElementReferenceException error almost immediately on the starRating in the first try. I have tried everything that I can think of, but not sure why it is throwing me that error.
When the DOM is refreshed or changed the driver loses all the elements it previously located. After moving to the new page myEles is not valid any more and that's why you get StaleElementReferenceException on business when you trying to use it to locate another element. The solution is to locate myEles each iteration and keep the location using indexes with for loop
int size = 1;
for (int i = 0; i < size; ++i)
{
var myEles = driverGC.FindElements(By.CssSelector("div.search-result"));
size = myEles.Count();
driverGC.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
var starRating = " ";
try
{
starRating = myEles[i].FindElement(By.CssSelector("div.biz-rating > div.i-stars")).GetAttribute("title");
}
//...
// and don't forget to go back
driver.Navigate().Back();
}

Check if Cell is empty to break while loop

Hello I work on a method to extract data from Cells. This works but every time I reach an empty cell I get a NullBinderException.
My question is how can I prevent this?
Here is the part that makes problems:
while ((range.Cells[startpoint, cell] as Excel.Range).Value2.ToString() != null)
{
for (int i = 1; i <= numberOfColumns; i++)
{
string sValue = (range.Cells[startpoint, cell] as Excel.Range).Value2.ToString();
stringList.Add(sValue);
cell++;
}
startpoint++;
cell = 1;
}
Stuff that I tried:
range.Offset = Cant use becouse this is not an aviable Member.
IsNullOrEmpty = Makes no difference
So there is something I don't get. Any help or advise would be great and thanks for your time.
The Cell Range and/or Value2 may be null. Check these within your Where clause.
while ((range.Cells[startpoint, cell] as Excel.Range) != null && (range.Cells[startpoint, cell] as Excel.Range).Value2 != null)
{
for (int i = 1; i <= numberOfColumns; i++)
{
string sValue = (range.Cells[startpoint, cell] as Excel.Range).Value2.ToString();
stringList.Add(sValue);
cell++;
}
startpoint++;
cell = 1;
}
You can make your while loop as:-
while (! IsNull(range.Cells[startpoint, cell] as Excel.Range).Value2))
You could try catch you exception like:
try
{
while ((range.Cells[startpoint, cell] as Excel.Range).Value2.ToString() != null)
{
for (int i = 1; i <= numberOfColumns; i++)
{
string sValue = (range.Cells[startpoint, cell] as Excel.Range).Value2.ToString();
stringList.Add(sValue);
cell++;
}
startpoint++;
cell = 1;
}
}
catch(nullBinderException e)
{
//you find an empty cell
//... break? jump over and continue?
//... your logic...
}

Docx - Removing section of document

Is there a way to remove sections of a document where i can specify the beginning and ending tags?
i need a way that i can remove a section of the document by passing in both my start and end catches, (##DELETEBEGIN and ##DELETEEND)
for example i have this in my document:
Hello, welcome to this document
##DELETEBEGIN{Some values to check in the code}
Some text that will be removed if the value is true
##DELETEEND
Final Line
If you need to delete text from ##DELETEBEGIN to ##DELETEEND, where ##DELETEBEGIN is not at the beginning of a Paragraph and ##DELETEEND is not at the end of a Paragraph, this code should work.
DocX document = DocX.Load("C:\\Users\\phil\\Desktop\\text.docx");
bool flag = false;
List<List<string>> list1 = new List<List<string>>();
List<string> list2 = new List<string>();
foreach (Novacode.Paragraph item in document.Paragraphs)
{
//use this if you need whole text of a paragraph
string paraText = item.Text;
var result = paraText.Split(' ');
int count = 0;
list2 = new List<string>();
//use this if you need word by word
foreach (var data in result)
{
string word = data.ToString();
if (word.Contains("##DELETEBEGIN")) flag = true;
if (word.Contains("##DELETEEND"))
{
flag = false;
list2.Add(word);
}
if (flag) list2.Add(word);
count++;
}
list1.Add(list2);
}
for (int i = 0; i < list1.Count(); i++)
{
string temp = "";
for (int y = 0; y < list1[i].Count(); y++)
{
if (y == 0)
{
temp = list1[i][y];
continue;
}
temp += " " + list1[i][y];
}
if (!temp.Equals("")) document.ReplaceText(temp, "");
}
document.Save();
I have to give some credit to this post for looping through each word.
I think i have found a solution to this, at least it works for me, please let me know if there is anything i can do better:
the deleteCommand would be the ##DELETEBEGIN string and the deleteEndCommand would be the ##DELETEEND
private void RemoveSection(DocX doc, string deleteCommand, string deleteEndCommand)
{
try
{
int deleteStart = 0;
int deleteEnd = 0;
//Get the array of the paragraphs containing the start and end catches
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
if (doc.Paragraphs[i].Text.Contains(deleteCommand))
deleteStart = i;
if (doc.Paragraphs[i].Text.Contains(deleteEndCommand))
deleteEnd = i;
}
if (deleteStart > 0 && deleteEnd > 0)
{
//delete from the paraIndex as the arrays will shift when a paragraph is deleted
int paraIndex = deleteStart;
for (int i = deleteStart; i <= deleteEnd; i++)
{
doc.RemoveParagraphAt(paraIndex);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

How to Dispose object created in a method of a class in C#

I have a class in which I write several methods. I declare some objects in that class and Dispose them in a catch block. When I tried to apply Ruleset on it, it gives warning message of CA2000. My Code is as follow
public int ValidationExcelBal(string excelPath, string objectReferenceExcelPath)
{
DataTable dtPointList = new DataTable();
DataTable dtAlarm = new DataTable();
DataTable dtObjectReference = new DataTable();
try
{
int objectReferenceColNum = -1;
int objectReferenceAlarmColNum = -1;
objGGTAutoBindingToolDal.YomitoriExcelToDataTable(excelPath, ref dtPointList, ref dtAlarm);
objGGTAutoBindingToolDal.ObjectReferenceExcelToData(objectReferenceExcelPath, ref dtObjectReference);
#region code to find object reference column number in excel sheet and Alarm sheet
for (int i = 0; i < dtPointList.Columns.Count; i++)
{
for (int k = 0; k < dtPointList.Rows.Count; k++)
{
if (k < 4)
{
string name = dtPointList.Rows[k][i].ToString().Replace("\n", "").Replace(" ", "");
if (name == "ObjectReference")
{
objectReferenceColNum = i;
break;
}
}
}
}
//code to find colomn number of object reference field in Alarm sheet
for (int j = 0; j < dtAlarm.Columns.Count; j++)
{
string name = dtAlarm.Rows[0][j].ToString();
if (name.Equals("Object Reference"))
{
objectReferenceAlarmColNum = j;
break;
}
}
#endregion
if (objectReferenceColNum == -1 || objectReferenceAlarmColNum == -1)
{
return 1;
}
//if (Convert.ToString(dtObjectReference.Columns[0]).Contains("Bldg Name") || Convert.ToString(dtObjectReference.Columns[1]).Contains("Graphics Name") || Convert.ToString(dtObjectReference.Columns[2]).Contains("Controller Object Reference"))
//{
// return 2;
//}
return 3;
}
catch (Exception)
{
dtPointList.Dispose();
dtAlarm.Dispose();
dtObjectReference.Dispose();
throw;
}
}
and my warning is as follow
Warning 17 CA2000 : Microsoft.Reliability : In method 'GgtAutoBindingToolBal.ValidationExcelBal(string, string)', call System.IDisposable.Dispose on object 'dtPointList' before all references to it are out of scope. D:\Project\Pragati Installer\GGTAutoBindingBll\GgtAutoBindingToolBal.cs 26 GgtAutoBindingBll
The best thing to do is to replace the manual call to Dispose() with a using statement (will call Dispose() when running out of scope).
using (DataTable dtPointList = new DataTable()) {
...
}
Example:
using (DataTable dtPointList = new DataTable()) {
using (DataTable dtAlarm = new DataTable()) {
using (DataTable dtObjectReference = new DataTable()) {
// your code here, no need to call Dispose() on any of these objects
}
}
}
You call dispose only in the catch block. What if there is no exception in try block? Shouldn't it be disposed there as well? Try it and see if the warning meessage i still there. As it was mentioned in the other answer - using is your best option. This way the resource will be used in the scope of the using code block.
public int ValidationExcelBal(string excelPath, string objectReferenceExcelPath)
{
using (DataTable dtPointList = new DataTable())
{
using (DataTable dtAlarm = new DataTable())
{
using (DataTable dtObjectReference = new DataTable())
{
try
{
int objectReferenceColNum = -1;
int objectReferenceAlarmColNum = -1;
objGGTAutoBindingToolDal.YomitoriExcelToDataTable(excelPath, ref dtPointList, ref dtAlarm);
objGGTAutoBindingToolDal.ObjectReferenceExcelToData(objectReferenceExcelPath, ref dtObjectReference);
#region code to find object reference column number in excel sheet and Alarm sheet
for (int i = 0; i < dtPointList.Columns.Count; i++)
{
for (int k = 0; k < dtPointList.Rows.Count; k++)
{
if (k < 4)
{
string name = dtPointList.Rows[k][i].ToString().Replace("\n", "").Replace(" ", "");
if (name == "ObjectReference")
{
objectReferenceColNum = i;
break;
}
}
}
}
//code to find colomn number of object reference field in Alarm sheet
for (int j = 0; j < dtAlarm.Columns.Count; j++)
{
string name = dtAlarm.Rows[0][j].ToString();
if (name.Equals("Object Reference"))
{
objectReferenceAlarmColNum = j;
break;
}
}
#endregion
if (objectReferenceColNum == -1 || objectReferenceAlarmColNum == -1)
{
return 1;
}
//if (Convert.ToString(dtObjectReference.Columns[0]).Contains("Bldg Name") || Convert.ToString(dtObjectReference.Columns[1]).Contains("Graphics Name") || Convert.ToString(dtObjectReference.Columns[2]).Contains("Controller Object Reference"))
//{
// return 2;
//}
return 3;
}
catch (Exception)
{
dtPointList.Dispose();
dtAlarm.Dispose();
dtObjectReference.Dispose();
throw;
}
}
}
}
}

Index won't up of List

I'm still learning in programming
I'm trying to get customer data in list. So I can get the value from the index, but it only can get the first customer. the index won't increment, I'm still confusing, I have already move the variable for increment the index won't work, maybe my logic isn't right. here's the code, tell me where is not right..?? thank you for you help and explanation
public void getJamVSpot()
{
var listJamAwal = new List<String>();
var listJamAkhir = new List<String>();
var listNota = new List<int>();
DateTime tglSewa = dtp_tglSewa.Value.Date;
int r = 0;
String ja = String.Empty;
String jb = String.Empty;
int n = 0;
int indexUp = 0;
if (dtp_tglSewa.Value.Date == jl.getTglJadwalVspot(tglSewa, lap) && rdb_Lapangan_VSpot.Checked == true || rdb_rumputSintetis.Checked == true)
{
IEnumerator<String> jAwal = jl.getJamAwalbyDate(tglSewa, lap);
while (jAwal.MoveNext())
{
listJamAwal.Add(jAwal.Current);
}
IEnumerator<String> jAkhir = jl.getJamAkhirbyDate(tglSewa, lap);
while (jAkhir.MoveNext())
{
listJamAkhir.Add(jAkhir.Current);
}
IEnumerator<int> nota = jl.getNota(tglSewa, lap);
while (nota.MoveNext())
{
listNota.Add(nota.Current);
}
ja = listJamAwal[indexUp];
jb = listJamAkhir[indexUp];
n = listNota[indexUp];
int count = jl.countNota(n);
String penyewa = jl.getNamaPenyewa(n);
String no_kontak = jl.getNomorKontak(n);
String status = jl.getStatusSewa(n);
for (int i = 1; i <= count; i++)
{
foreach (DataGridViewRow row in dgv_Jadwal_Sewa.Rows)
if (row.Cells[0].Value.ToString().Equals(ja))
{
r = row.Index;
row.Cells[2].Value = penyewa;
row.Cells[3].Value = no_kontak;
row.Cells[4].Value = status;
if (ja != jb)
{
ja = jl.getJamAkhirbyJamAwal(ja);
row.Cells[2].Value = penyewa;
row.Cells[3].Value = no_kontak;
row.Cells[4].Value = status;
//dgv_Jadwal_Sewa.Rows[r].Selected = true;
}
break;
}
}
} indexUp++;
}
When you increment the indexUp variable you aren't using it anymore.
In your code you are just recovering the first item (0), doing some stuff with this value (in the loops) and exits.
For example, you can wrap your stuff with this loop:
for (int indexUp = 0; indexUp < listJamAwal.Count; indexUp++)
{
ja = listJamAwal[indexUp];
jb = listJamAkhir[indexUp];
n = listNota[indexUp];
int count = jl.countNota(n);
String penyewa = jl.getNamaPenyewa(n);
String no_kontak = jl.getNomorKontak(n);
String status = jl.getStatusSewa(n);
for (int i = 1; i <= count; i++)
{
foreach (DataGridViewRow row in dgv_Jadwal_Sewa.Rows)
{
if (row.Cells[0].Value.ToString().Equals(ja))
{
r = row.Index;
row.Cells[2].Value = penyewa;
row.Cells[3].Value = no_kontak;
row.Cells[4].Value = status;
if (ja != jb)
{
ja = jl.getJamAkhirbyJamAwal(ja);
row.Cells[2].Value = penyewa;
row.Cells[3].Value = no_kontak;
row.Cells[4].Value = status;
//dgv_Jadwal_Sewa.Rows[r].Selected = true;
}
break;
}
}
}
}
There are two problems with how you access the items:
You assign the variables outside the loop. That will get the values that the index points to at that moment, and changing the index variable later doesn't change what's assigned to the variables. You have to assign the variables inside the loop, except the count variable of course which you need before the loop starts.
You are incrementing the indexUp variable after the loop, when you have no use for it any more. You have to put that inside the loop so that it can be used in the next iteration to read new values into the variables.
I dont see any point of using first loop with "i" and then second with foreach. Its totally wrong - you are doing the same thing "count" times!
You also need to change penyewa, no_kontak, status because you are using same values inside loop
And in addition, I have never seen so confusing and unclear variable naming, you should change it if you want some else to use it :D

Categories

Resources