Null Values in ItemUpdating Event

G

Guest

I have a DetailsView control and when it get's updated i send an email
containing the new details. I have used the ItemUpdating event handler for
the DetailsView and i am populating the MailMessage.Body property with a
string variable which i build up using something like

String strMessageBody = "Field1 = " + e.NewValues["Field1"].ToString() +
"\nField2 = " + e.NewValues["Field1"].ToString();

This works fine apart from when one of the fields i am refering to is blank.
What i expected to happen is the e.NewValues["Field1"].ToString would give
me a blank string. However it seems that if the value coming from the
DetailsView is null for Field1 then e.NewValues["Field1"] doesn't exist.

Is there an easy way round this, i am putting the values of about 10 fields
into one string variable, i want to avoid 10

if (e.NewValues["Field1"] != null)
{
strMessageBody = strMessageBody + "Field1 = " +
e.NewValues["Field1"].toString();
}
 
F

Flinky Wisty Pomm

If you just want to avoid duplicated code, why not move it into another
method?

public string getField(IOrderedDictionary values, string key)
{
return values.ContainsKey(key) ?
string.Format("{0} = {1}", key, values[key]) :
string.Empty;
}

then it's a simple case of string strMessageBody = getField("field1") +
getField("field2")...
 
G

Guest

It's not so much code duplication i am tring to avoid as having to do a load
of comparisons to see if a particular value is set or not. The problem with
passing e.NewValues("SomeField") to a function/method is that if SomeField is
set to null in the DetailsViews control then rather than there being
e.NewValues("SomeField") with a value of null, it simply does not exist. You
get a "Object reference not set to an instance of an object" if you try to do
something such as e.NewValues("SomeField").ToString().
 
G

Guest

I have solved the problem, you simply set ConvertEmptyStringToNull="False",
in all of the TemplateFields of the DetailsView.

clickon said:
It's not so much code duplication i am tring to avoid as having to do a load
of comparisons to see if a particular value is set or not. The problem with
passing e.NewValues("SomeField") to a function/method is that if SomeField is
set to null in the DetailsViews control then rather than there being
e.NewValues("SomeField") with a value of null, it simply does not exist. You
get a "Object reference not set to an instance of an object" if you try to do
something such as e.NewValues("SomeField").ToString().


Flinky Wisty Pomm said:
If you just want to avoid duplicated code, why not move it into another
method?

public string getField(IOrderedDictionary values, string key)
{
return values.ContainsKey(key) ?
string.Format("{0} = {1}", key, values[key]) :
string.Empty;
}

then it's a simple case of string strMessageBody = getField("field1") +
getField("field2")...
 

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

Members online

No members online now.

Forum statistics

Threads
474,007
Messages
2,570,266
Members
46,865
Latest member
AveryHamme

Latest Threads

Top