473,322 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Null Values in ItemUpdating Event

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();
}
May 25 '06 #1
3 3407
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")...

May 25 '06 #2
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" wrote:
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")...

May 25 '06 #3
I have solved the problem, you simply set ConvertEmptyStringToNull="False",
in all of the TemplateFields of the DetailsView.

"clickon" wrote:
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" wrote:
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")...

May 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marcus | last post by:
Hello, quick question about MySQL storing NULL values... Say I have a textbox called $_POST and a variable $var. if(empty($_POST)) $var = NULL; else $var = $_POST; Disregarding...
26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
1
by: Ben Reese | last post by:
When you bind a text box to a field that contains null values the text box displays an empty string. Great! My Database field is an integer and I want an empty text box to represent a NULL...
8
by: manning_news | last post by:
Using SQL2000. According to Books Online, the avg aggregrate function ignores null values. ((3+3+3+3+Null)/5) predictably returns Null. Is there a function to ignore the Null entry, adjust the...
3
by: JOEP | last post by:
What do I need to do to allow an append query to post null values to records in a field of the destination table? Basically I want to allow records with null values to post to the table. The append...
6
by: doncee | last post by:
I have a list box that is generated on a form by way of a Parameter Query. Problem is whenever I try to refer to the list box, i.e., to update the underlying table, I am getting a "null" value...
0
by: =?iso-8859-2?Q?=A3ukasz_Led=F3chowski?= | last post by:
Hi! I am using FormView with SqlDataSource. The problem is that during ItemUpdating event of FormView, NewValues table is empty. OldValues has all values of fields bound on template. What can be...
2
by: Matt MacDonald | last post by:
Hi all, I'm having an issue that's driving me crazy. I have a datatable that I need to display all the information for on a page. So, instead of manually coding every single column to a label, I...
1
by: deepakthegeek | last post by:
I have a listview control in which I had replaced all the ItemTemplate labels with TextBoxs. I also have a command button on the page, when the button is clicked I am able to retreive the text in all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.