Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, December 28, 2012 3:46 PM
I am Adding some C# code to an infopath form I am working on. I am having issue with While loop that is gatherig data form a sharepoint data connection. some of the values are null and it is throwing a null exception error
I am trying to use try/Catch to handle the execption I can not figure out how to have the catch go back to the next varible to continue the loop.
I also tryied doing a try/catch on all the varibles and use a goto to move to the next element.
But then the varible value ends up with a error does not exist in the current context error.
and help would be appriciated.
First Try
public void CTRL13_5_Clicked(object sender, ClickedEventArgs e)
{
XPathNavigator secDSNav = DataSources["File Layout Container"].CreateNavigator();
// Retrieve the rows of the secondary data source
XPathNodeIterator rows = secDSNav.Select(
"/xml/rs:data/z:row", NamespaceManager);
// Loop through the rows of the secondary data source and fill the repating table
try
{
while (rows.MoveNext())
{
string FieldName = rows.Current.SelectSingleNode(
"@ows_Field_Name", NamespaceManager).Value;
string DataType = rows.Current.SelectSingleNode(
"@ows_Data_Type", NamespaceManager).Value;
string StartPosition = rows.Current.SelectSingleNode(
"@ows_Start_Position", NamespaceManager).Value;
string EndPosition = rows.Current.SelectSingleNode(
"@ows_End_Position", NamespaceManager).Value;
string Position = rows.Current.SelectSingleNode(
"@ows_Positon", NamespaceManager).Value;
string Length = rows.Current.SelectSingleNode(
"@ows_Length", NamespaceManager).Value;
// Add the item to the repeating table
AddItem(FieldName, DataType, StartPosition, EndPosition, Position, Length);
// AddItem(FieldName, DataType, StartPosition, EndPosition, Length);
}//end loop
}//End Try
catch(NullReferenceException)
{
System.Windows.Forms.MessageBox.Show("Somthing Went Wrong");
}//End catch
// Remove the first empty item from the repeating table
DeleteFirstEmptyItem();
}
Second try Try/catch every varible.
public void CTRL13_5_Clicked(object sender, ClickedEventArgs e)
{
XPathNavigator secDSNav = DataSources["File Layout Container"].CreateNavigator();
// Retrieve the rows of the secondary data source
XPathNodeIterator rows = secDSNav.Select(
"/xml/rs:data/z:row", NamespaceManager);
// Loop through the rows of the secondary data source and fill the repating table
string FName = FeildName;
try
{
while (rows.MoveNext())
{
try
{
string FieldName = rows.Current.SelectSingleNode(
"@ows_Field_Name", NamespaceManager).Value;
}
catch (NullReferenceException)
{
}
datatype:
try
{
string DataType = rows.Current.SelectSingleNode(
"@ows_Data_Type", NamespaceManager).Value;
}
catch (NullReferenceException)
{
goto startpostion;
}
startpostion:
try
{
string StartPosition = rows.Current.SelectSingleNode(
"@ows_Start_Position", NamespaceManager).Value;
}
catch
{
goto endposition;
}
end
{
string EndPosition = rows.Current.SelectSingleNode(
"@ows_End_Position", NamespaceManager).Value;
}
catch (NullReferenceException)
{
goto position;
}
{
string Position = rows.Current.SelectSingleNode(
"@ows_Positon", NamespaceManager).Value;
}
catch(NullReferenceException)
{
goto length;
}
length:
try
{
string Length = rows.Current.SelectSingleNode(
"@ows_Length", NamespaceManager).Value;
}
catch(NullReferenceException)
{
}
// Add the item to the repeating table
AddItem(FieldName, DataType, StartPosition, EndPosition, Position, Length);
// AddItem(FieldName, DataType, StartPosition, EndPosition, Length);
}//end loop
}//End Try
catch(NullReferenceException)
{
System.Windows.Forms.MessageBox.Show("Somthing Went Wrong");
}//End catch
// Remove the first empty item from the repeating table
DeleteFirstEmptyItem();
}
All replies (8)
Saturday, December 29, 2012 4:40 AM ✅Answered | 1 vote
Don't fix your code by catching the exceptions. Exceptions have lots of overhead and should be reserved for exceptional cases. Fix your code by preventing them from being raised in the first place.
Looking at one of your lines of code:
string FieldName = rows.Current.SelectSingleNode("@ows_Field_Name", NamespaceManager).Value;
- rows isn't null or you wouldn't get beyond the start of the loop : while(rows.MoveNext())
- rows.Current shouldn't be null if rows.MoveNext() returned true.
- NamespaceManger doesn't change inside the loop. Verify that it's not null using the debugger.
And the only other possibility is the most likely one... SelectSingleNode will return null if the node does not exist. To correct this, check the result before you dereference it to get the value.
Longhand this would look something like this:
var node = rows.Current.SelectSingleNode("@ows_Field_Name", NamespaceManager);
string FieldName = (null != node) ? node.Value : string.Empty;
However since you're doing this a lot, you might want to create an extension method for XPathNavigator (the return type of SelectSingleNode)
internal static class Extensions
{
internal static string ValueOrEmpty(this XPathNavigator node)
{
if (null == node)
return string.Empty;
return node.Value;
}
}
Then you can change your code to use the extension
string FieldName = rows.Current.SelectSingleNode("@ows_Field_Name", NamespaceManager).ValueOrEmpty();
Disclaimer, the assumption here is that missing values are acceptable and normal. If they are indeed errors, you still need to treat them as exceptions.
This signature unintentionally left blank.
Friday, December 28, 2012 4:13 PM | 1 vote
You should use the try/catch like this:
while(...)
{
try
{
//your code here
}
catch (Exception ex)
{
Console.WriteLine(ex.Message)
}
}
You only need one try/catch block inside your while loop. It is pointless to try to filter the exception type using "NullReferenceException". You can get the exception type from ex after the exception is thrown.
Dan Randolph - My Code Samples List
Friday, December 28, 2012 5:10 PM | 1 vote
Please check the following example
class Program { static void Main(string[] args) { int j = 20; for (int i = 0; i < 10; i++) { try { if (j % i < 10) { Console.WriteLine("Hai"); } } catch (DivideByZeroException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } } Console.ReadLine(); }
With Thanks and Regards
Sambath Raj.C
click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
Happy Programming!
Friday, December 28, 2012 5:21 PM
Thanks for the reply. I found that the issue is {"Object reference not set to an instance of an object."}. this is due to a null value. My real issue now is how to have the catch block start the next VAR. If it breaks at StartPostion it continues on to the EndPostion Var.
Saturday, December 29, 2012 11:14 AM
Theoretically, if you cannot prevent some exceptions that occur in third-party code and want to ignore them, then you can consider this construct:
string FieldName;
try
{
* FieldName = . . . .;*
}
catch( NullReferenceException )
{
* FieldName = string.Empty; // or ‘FieldName = null’*
}
Now the error ‘does not exist in the current context’ will not occur.
Monday, December 31, 2012 2:50 PM
Thank you Nick
I started testing this morning. Rows is never null. Where do i put the internal static Class
Code @ ? I am really still very new to this.
Thanks
James
James Gifford
Monday, December 31, 2012 3:44 PM
Nick
I also tryied your First sugestion and I was able to make that work.
XPathItem nodeFieldName = rows.Current.SelectSingleNode("@ows_Field_Name", NamespaceManager); string FieldName = (null != nodeFieldName) ? nodeFieldName.Value : string.Empty;
It works like a charm. !
James Gifford
Tuesday, January 1, 2013 12:55 PM
Great to hear. To answer your follow up question, I'd first suggest reading up on extension methods. They can be quite powerful when used properly. (But like any other code construct, are just sauce on spaghetti when used improperly:)
To use the one I gave you, add a class file to your project called (for example) Extensions.cs, then replace the class definition with the supplied code.
Happy coding.
This signature unintentionally left blank.