I am trying to get the value from the next item, for example, if the next item is null, or doesnt exist, it will proceed to printing. The main problem i am having lies only at this part
Main Question :
try
{
numCheck = productList[j+1]["Number"].InnerText;
}
catch (Exception ex)
{
numCheck = string.Empty;
}
if (numCheck != null)
{
try
{
mainNumber = Data.getFullItemDetail(productList[j + 1]["Number"].InnerText);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
I did a check before i tried if else just to make sure the value exist, however, even after it detects that it has a value, this line will throw an error NullReference
mainNumber = Data.getFullItemDetail(productList[j + 1]["Number"].InnerText);
FULL Code just in case:
XmlDocument xml = new XmlDocument();
xml.LoadXml(productItems);
XmlNodeList productList = xml.SelectNodes("/Products/Product");
for (int j = 0; j < productList.Count; j++)
{
itemCount++;
int i = j + 1;
string item = productList[j]["Number"].InnerText;
number = Data.getFullItemDetail(item);
try
{
#region mainItem
if (number == 0)
{
itemCount++;
itemName = productList[j]["Name"].InnerText;
try
{
numCheck = productList[i]["Number"].InnerText;
}
catch (Exception ex)
{
numCheck = string.Empty;
}
if (numCheck != null)
{
try
{
mainNumber = Data.getFullItemDetail(productList[i]["Number"].InnerText);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
if (mainNumber == 0)
{
foreach (string condimentItem in condiment)
{
condimentText += condimentItem.ToString() + "\n";
}
string text = condimentText + itemName + "\n" + employeeNum + "\n" + terminalNum + "\n";
//print here, because the next item is another main item.
};
try
{
p.Print();
}
catch (Exception ex)
{
throw new Exception("Exception Occured While Printing", ex);
}
}
else
{
MessageBox.Show("Error");
}
}
else
{
foreach (string condimentItem in condiment)
{
condimentText += condimentItem.ToString() + "\n";
}
string text = condimentText + itemName + "\n" + employeeNum + "\n" + terminalNum + "\n";
}
}
#endregion
#region CondimentItem
else if (number == 4)
{
condiment.Add(productList[j]["Name"].InnerText);
try
{
numCheck = productList[i]["Number"].InnerText;
}
catch (Exception ex)
{
numCheck = string.Empty;
}
//Check next item.
if (numCheck == null)
{
foreach (string condimentItem in condiment)
{
condimentText += condimentItem.ToString() + "\n";
}
string text = condimentText + itemName + "\n" + employeeNum + "\n" + terminalNum + "\n";
//print here, because the next item is another main item.
}
else
{
mainNumber = Data.getFullItemDetail(productList[i]["Number"].InnerText);
if (mainNumber == 0)
{
foreach (string condimentItem in condiment)
{
condimentText += condimentItem.ToString() + "\n";
}
string text = condimentText + itemName + "\n" + employeeNum + "\n" + terminalNum + "\n";
//print here, because the next item is another main item.
}
}
}
#endregion
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Edited : Data.getFullItemDetail only returns 1 int value.
public static int getFullItemDetail(string number)
{
int num = Convert.ToInt32(number);
int numReturn;
using (SqlConnection conn = getConnectionSAP())
{
SqlCommand comm = new SqlCommand("select belongcond from ------ where number = " + num, conn);
conn.Open();
SqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
num = Convert.ToInt32(dr["-------"]);
}
comm.Clone();
comm.Dispose();
}
num = Convert.ToInt32(num.ToString().Substring(0, 1));
return num;
}