Checking for null field in C# when opening the form

S

Sailor

Hello,

First I have to say I am new to InfoPath and I am not a programmer at all.
I was able to get this C# code to work to query AD to populate form fields
from the main data source (based on the current user). Problem is, I need to
first check if the field is null or not...to make sure that these fields are
only saved the FIRST time the form is filled out. This condition must be
done in the code not the form but I can't figure it out for the life of me.
I've tried everything. I have included the code I have now, which does not
work (no error, but doesn't keep the fields from populating when opened a
second time).

I'm grateful for any help!


public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
// Write your code here.
// Create an XPathNavigator to walk the main data source
// of the form.
XPathNavigator xnMyForm = this.CreateNavigator();
XmlNamespaceManager ns = this.NamespaceManager;
XPathNavigator LastNameNav =
xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:LastName", ns);
if (LastNameNav == null)
{

try
{
// Get the user name of the current user.
string userName = this.Application.User.UserName;

// Create a DirectorySearcher object using the user name
// as the LDAP search filter. If using a directory other
// than Exchange, use sAMAccountName instead of
mailNickname.
DirectorySearcher searcher = new DirectorySearcher(
"(mailNickname=" + userName + ")");

// Search for the specified user.
SearchResult result = searcher.FindOne();

// Make sure the user was found.
if (result == null)
{
MessageBox.Show("Error finding user: " + userName);
}
else
{
// Create a DirectoryEntry object to retrieve the
collection
// of attributes (properties) for the user.
DirectoryEntry employee = result.GetDirectoryEntry();

// Assign the specified properties to string
variables.
string FirstName = employee.Properties[
"givenName"].Value.ToString();
string LastName =
employee.Properties["sn"].Value.ToString();
string Mail =
employee.Properties["mail"].Value.ToString();
string Location = employee.Properties[
"physicalDeliveryOfficeName"].Value.ToString();
string Title =
employee.Properties["title"].Value.ToString();
string Phone = employee.Properties[
"telephoneNumber"].Value.ToString();
string Department = employee.Properties[
"department"].Value.ToString();

// The manager property returns a distinguished name,
// so get the substring of the common name following
"CN=".
//string ManagerName = employee.Properties[
// "manager"].Value.ToString();
// ManagerName = ManagerName.Substring(
//3, ManagerName.IndexOf(",") - 3);


// Set the fields in the form.

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:FirstName", ns)
.SetValue(FirstName);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:LastName", ns)
.SetValue(LastName);
//xnMyForm.SelectSingleNode("/my:myFields/my:Alias",
ns)
//.SetValue(userName);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:Email", ns)
.SetValue(Mail);

//xnMyForm.SelectSingleNode("/my:myFields/my:Manager", ns)
//.SetValue(ManagerName);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpLocation/my:Office", ns)
.SetValue(Location);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:Title", ns)
.SetValue(Title);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:phone", ns)
.SetValue(Phone);

//xnMyForm.SelectSingleNode("/my:myFields/my:Department", ns)
//.SetValue(Department);

// Clean up.
xnMyForm = null;
searcher.Dispose();
result = null;
employee.Close();
}

}
catch (Exception ex)
{
MessageBox.Show("The following error occurred: " +
ex.Message.ToString());
throw;
}

}
}
}
 
S

S.Y.M. Wong-A-Ton

If the code is populating the fields now despite the current check it means
that LastNameNav is null and thus the XPath expression for it might be
incorrect.

Otherwise you could try the following

if (LastNameNav != null && LastNameNav.Value == "")

instead of

if (LastNameNav == null)

---
S.Y.M. Wong-A-Ton
http://www.bizsupportonline.net


Sailor said:
Hello,

First I have to say I am new to InfoPath and I am not a programmer at all.
I was able to get this C# code to work to query AD to populate form fields
from the main data source (based on the current user). Problem is, I need to
first check if the field is null or not...to make sure that these fields are
only saved the FIRST time the form is filled out. This condition must be
done in the code not the form but I can't figure it out for the life of me.
I've tried everything. I have included the code I have now, which does not
work (no error, but doesn't keep the fields from populating when opened a
second time).

I'm grateful for any help!


public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
// Write your code here.
// Create an XPathNavigator to walk the main data source
// of the form.
XPathNavigator xnMyForm = this.CreateNavigator();
XmlNamespaceManager ns = this.NamespaceManager;
XPathNavigator LastNameNav =
xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:LastName", ns);
if (LastNameNav == null)
{

try
{
// Get the user name of the current user.
string userName = this.Application.User.UserName;

// Create a DirectorySearcher object using the user name
// as the LDAP search filter. If using a directory other
// than Exchange, use sAMAccountName instead of
mailNickname.
DirectorySearcher searcher = new DirectorySearcher(
"(mailNickname=" + userName + ")");

// Search for the specified user.
SearchResult result = searcher.FindOne();

// Make sure the user was found.
if (result == null)
{
MessageBox.Show("Error finding user: " + userName);
}
else
{
// Create a DirectoryEntry object to retrieve the
collection
// of attributes (properties) for the user.
DirectoryEntry employee = result.GetDirectoryEntry();

// Assign the specified properties to string
variables.
string FirstName = employee.Properties[
"givenName"].Value.ToString();
string LastName =
employee.Properties["sn"].Value.ToString();
string Mail =
employee.Properties["mail"].Value.ToString();
string Location = employee.Properties[
"physicalDeliveryOfficeName"].Value.ToString();
string Title =
employee.Properties["title"].Value.ToString();
string Phone = employee.Properties[
"telephoneNumber"].Value.ToString();
string Department = employee.Properties[
"department"].Value.ToString();

// The manager property returns a distinguished name,
// so get the substring of the common name following
"CN=".
//string ManagerName = employee.Properties[
// "manager"].Value.ToString();
// ManagerName = ManagerName.Substring(
//3, ManagerName.IndexOf(",") - 3);


// Set the fields in the form.

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:FirstName", ns)
.SetValue(FirstName);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:LastName", ns)
.SetValue(LastName);
//xnMyForm.SelectSingleNode("/my:myFields/my:Alias",
ns)
//.SetValue(userName);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:Email", ns)
.SetValue(Mail);

//xnMyForm.SelectSingleNode("/my:myFields/my:Manager", ns)
//.SetValue(ManagerName);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpLocation/my:Office", ns)
.SetValue(Location);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:Title", ns)
.SetValue(Title);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:phone", ns)
.SetValue(Phone);

//xnMyForm.SelectSingleNode("/my:myFields/my:Department", ns)
//.SetValue(Department);

// Clean up.
xnMyForm = null;
searcher.Dispose();
result = null;
employee.Close();
}

}
catch (Exception ex)
{
MessageBox.Show("The following error occurred: " +
ex.Message.ToString());
throw;
}

}
}
}
 
S

Sailor

Thank you so much! Changing as you suggested was the ticket. I appreciate
your help.

S.Y.M. Wong-A-Ton said:
If the code is populating the fields now despite the current check it means
that LastNameNav is null and thus the XPath expression for it might be
incorrect.

Otherwise you could try the following

if (LastNameNav != null && LastNameNav.Value == "")

instead of

if (LastNameNav == null)

---
S.Y.M. Wong-A-Ton
http://www.bizsupportonline.net


Sailor said:
Hello,

First I have to say I am new to InfoPath and I am not a programmer at all.
I was able to get this C# code to work to query AD to populate form fields
from the main data source (based on the current user). Problem is, I need to
first check if the field is null or not...to make sure that these fields are
only saved the FIRST time the form is filled out. This condition must be
done in the code not the form but I can't figure it out for the life of me.
I've tried everything. I have included the code I have now, which does not
work (no error, but doesn't keep the fields from populating when opened a
second time).

I'm grateful for any help!


public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
// Write your code here.
// Create an XPathNavigator to walk the main data source
// of the form.
XPathNavigator xnMyForm = this.CreateNavigator();
XmlNamespaceManager ns = this.NamespaceManager;
XPathNavigator LastNameNav =
xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:LastName", ns);
if (LastNameNav == null)
{

try
{
// Get the user name of the current user.
string userName = this.Application.User.UserName;

// Create a DirectorySearcher object using the user name
// as the LDAP search filter. If using a directory other
// than Exchange, use sAMAccountName instead of
mailNickname.
DirectorySearcher searcher = new DirectorySearcher(
"(mailNickname=" + userName + ")");

// Search for the specified user.
SearchResult result = searcher.FindOne();

// Make sure the user was found.
if (result == null)
{
MessageBox.Show("Error finding user: " + userName);
}
else
{
// Create a DirectoryEntry object to retrieve the
collection
// of attributes (properties) for the user.
DirectoryEntry employee = result.GetDirectoryEntry();

// Assign the specified properties to string
variables.
string FirstName = employee.Properties[
"givenName"].Value.ToString();
string LastName =
employee.Properties["sn"].Value.ToString();
string Mail =
employee.Properties["mail"].Value.ToString();
string Location = employee.Properties[
"physicalDeliveryOfficeName"].Value.ToString();
string Title =
employee.Properties["title"].Value.ToString();
string Phone = employee.Properties[
"telephoneNumber"].Value.ToString();
string Department = employee.Properties[
"department"].Value.ToString();

// The manager property returns a distinguished name,
// so get the substring of the common name following
"CN=".
//string ManagerName = employee.Properties[
// "manager"].Value.ToString();
// ManagerName = ManagerName.Substring(
//3, ManagerName.IndexOf(",") - 3);


// Set the fields in the form.

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:FirstName", ns)
.SetValue(FirstName);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:LastName", ns)
.SetValue(LastName);
//xnMyForm.SelectSingleNode("/my:myFields/my:Alias",
ns)
//.SetValue(userName);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:Email", ns)
.SetValue(Mail);

//xnMyForm.SelectSingleNode("/my:myFields/my:Manager", ns)
//.SetValue(ManagerName);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpLocation/my:Office", ns)
.SetValue(Location);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:Title", ns)
.SetValue(Title);

xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:phone", ns)
.SetValue(Phone);

//xnMyForm.SelectSingleNode("/my:myFields/my:Department", ns)
//.SetValue(Department);

// Clean up.
xnMyForm = null;
searcher.Dispose();
result = null;
employee.Close();
}

}
catch (Exception ex)
{
MessageBox.Show("The following error occurred: " +
ex.Message.ToString());
throw;
}

}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top